Skip to content

Commit 2cac343

Browse files
authored
Fix - provide default (empty) client config when default user config dir does not exist (temporalio#2266)
* Fix - provide default (empty) client config when default user config dir does not exist * Restore conditional accidentally removed
1 parent d8e37f8 commit 2cac343

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

contrib/envconfig/client_config_load.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ func LoadClientConfig(options LoadClientConfigOptions) (ClientConfig, error) {
127127
file, _ = env.LookupEnv("TEMPORAL_CONFIG_FILE")
128128
}
129129
if file == "" {
130-
var err error
131-
if file, err = DefaultConfigFilePath(); err != nil {
132-
return ClientConfig{}, err
133-
}
130+
// Get the default config file path. If it doesn't exist, the file path will be empty.
131+
file = DefaultConfigFilePath()
134132
}
135133
// Load file, not exist is ok
136134
if b, err := os.ReadFile(file); err == nil {
@@ -239,12 +237,13 @@ func LoadClientConfigProfile(options LoadClientConfigProfileOptions) (ClientConf
239237
const DefaultConfigFileProfile = "default"
240238

241239
// DefaultConfigFilePath is the default config file path used. It is [os.UserConfigDir]/temporalio/temporal.toml.
242-
func DefaultConfigFilePath() (string, error) {
240+
// If the path does not exist, fallback to an empty file path.
241+
func DefaultConfigFilePath() string {
243242
userDir, err := os.UserConfigDir()
244243
if err != nil {
245-
return "", fmt.Errorf("failed getting user config dir: %w", err)
244+
return ""
246245
}
247-
return filepath.Join(userDir, "temporalio", "temporal.toml"), nil
246+
return filepath.Join(userDir, "temporalio", "temporal.toml")
248247
}
249248

250249
// EnvLookup abstracts environment variable lookup for [ClientConfigProfile.ApplyEnvVars]. [EnvLookupOS] is the common

contrib/envconfig/client_config_load_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package envconfig_test
22

33
import (
44
"os"
5+
"runtime"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -170,6 +171,25 @@ disable_host_verification = true`
170171
}, prof.GRPCMeta)
171172
}
172173

174+
func TestLoadDefaultConfigFileNotExist(t *testing.T) {
175+
// Unset default user config dir.
176+
switch runtime.GOOS {
177+
case "windows":
178+
t.Setenv("AppData", "")
179+
case "darwin", "ios":
180+
t.Setenv("HOME", "")
181+
case "plan9":
182+
t.Setenv("home", "")
183+
default: // Unix
184+
t.Setenv("XDG_CONFIG_HOME", "")
185+
t.Setenv("HOME", "")
186+
}
187+
// Load default config, config file does not exist
188+
config, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{})
189+
require.NoError(t, err)
190+
require.Equal(t, envconfig.ClientConfig{Profiles: make(map[string]*envconfig.ClientConfigProfile)}, config)
191+
}
192+
173193
type EnvLookupMap map[string]string
174194

175195
func (e EnvLookupMap) Environ() []string {

0 commit comments

Comments
 (0)