From 6c216379e8b636d9b90c77c431c32d826c81e0b4 Mon Sep 17 00:00:00 2001 From: Thomas Hardy Date: Fri, 1 May 2026 16:32:55 -0400 Subject: [PATCH] fix - do not error when no default file path found --- packages/envconfig/src/envconfig-toml.ts | 29 +++++++++++++----------- packages/test/src/test-envconfig.ts | 8 +++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/envconfig/src/envconfig-toml.ts b/packages/envconfig/src/envconfig-toml.ts index b93a2d28a..12cfd833c 100644 --- a/packages/envconfig/src/envconfig-toml.ts +++ b/packages/envconfig/src/envconfig-toml.ts @@ -181,9 +181,9 @@ function strictValidateTomlStructure(parsed: TomlTable): void { function getFallbackConfigData(envProvider: Record): string | undefined { // configSource was not set - fallback to TEMPORAL_CONFIG_FILE, then the default file path - let filePath = envProvider['TEMPORAL_CONFIG_FILE']; + const filePath = envProvider['TEMPORAL_CONFIG_FILE'] ?? getDefaultConfigFilePath(envProvider); if (filePath === undefined) { - filePath = getDefaultConfigFilePath(); + return undefined; } return readFileSync(filePath, { encoding: 'utf-8' }); } @@ -294,43 +294,46 @@ function envVarToBool(envVar?: string): boolean | undefined { const DEFAULT_CONFIG_FILE_PROFILE = 'default'; const DEFAULT_CONFIG_FILE = 'temporal.toml'; -function getDefaultConfigFilePath(): string { - const configDir = getUserConfigDir(); +function getDefaultConfigFilePath(envProvider: Record): string | undefined { + const configDir = getUserConfigDir(envProvider); + if (configDir === undefined) { + return undefined; + } const configPath = path.join(configDir, 'temporalio', DEFAULT_CONFIG_FILE); return configPath; } -function getUserConfigDir(): string { +function getUserConfigDir(envProvider: Record): string | undefined { const platform = os.platform(); switch (platform) { case 'win32': { - const dir = process.env.APPDATA; + const dir = envProvider['APPDATA']; if (!dir) { - throw new Error('%AppData% is not defined'); + return undefined; } return dir; } case 'darwin': { - const dir = process.env.HOME; + const dir = envProvider['HOME']; if (!dir) { - throw new Error('$HOME is not defined'); + return undefined; } return path.join(dir, 'Library', 'Application Support'); } default: { // Unix/Linux - let dir = process.env.XDG_CONFIG_HOME; + let dir = envProvider['XDG_CONFIG_HOME']; if (!dir) { - const home = process.env.HOME; + const home = envProvider['HOME']; if (!home) { - throw new Error('neither $XDG_CONFIG_HOME nor $HOME are defined'); + return undefined; } dir = path.join(home, '.config'); } else if (!path.isAbsolute(dir)) { - throw new Error('path in $XDG_CONFIG_HOME is relative'); + return undefined; } return dir; } diff --git a/packages/test/src/test-envconfig.ts b/packages/test/src/test-envconfig.ts index 58e58287a..1029f975c 100644 --- a/packages/test/src/test-envconfig.ts +++ b/packages/test/src/test-envconfig.ts @@ -360,6 +360,14 @@ test('Load profiles from non-existent file', (t) => { t.deepEqual(conf.profiles, {}); }); +test('Load default config with missing user config dir', (t) => { + const conf = loadClientConfig({ overrideEnvVars: {} }); + t.deepEqual(conf, { profiles: {} }); + + const profile = loadClientConfigProfile({ overrideEnvVars: {} }); + t.deepEqual(profile, {}); +}); + test('Load all profiles with overridden file path', (t) => { withTempFile(TOML_CONFIG_BASE, (filepath) => { const conf = loadClientConfig({ overrideEnvVars: { TEMPORAL_CONFIG_FILE: filepath } });