diff options
| author | Jack Wotherspoon <[email protected]> | 2025-07-07 01:13:13 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-07 05:13:13 +0000 |
| commit | b70fba5b09b274c8a751d3c579fc31fe847f497f (patch) | |
| tree | 3f84e4b5e0704b8c1c1676db1010b77d7fcaa2fa /packages/cli/src/config/settings.ts | |
| parent | 87a44ec4689b352a16467701e4bff9894c5afb84 (diff) | |
fix: respect env variables in .env for settings.json variable substitution (#3416)
Diffstat (limited to 'packages/cli/src/config/settings.ts')
| -rw-r--r-- | packages/cli/src/config/settings.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/packages/cli/src/config/settings.ts b/packages/cli/src/config/settings.ts index 28010a7a..93f042aa 100644 --- a/packages/cli/src/config/settings.ts +++ b/packages/cli/src/config/settings.ts @@ -7,8 +7,10 @@ import * as fs from 'fs'; import * as path from 'path'; import { homedir } from 'os'; +import * as dotenv from 'dotenv'; import { MCPServerConfig, + GEMINI_CONFIG_DIR as GEMINI_DIR, getErrorMessage, BugCommandSettings, TelemetrySettings, @@ -172,11 +174,48 @@ function resolveEnvVarsInObject<T>(obj: T): T { return obj; } +function findEnvFile(startDir: string): string | null { + let currentDir = path.resolve(startDir); + while (true) { + // prefer gemini-specific .env under GEMINI_DIR + const geminiEnvPath = path.join(currentDir, GEMINI_DIR, '.env'); + if (fs.existsSync(geminiEnvPath)) { + return geminiEnvPath; + } + const envPath = path.join(currentDir, '.env'); + if (fs.existsSync(envPath)) { + return envPath; + } + const parentDir = path.dirname(currentDir); + if (parentDir === currentDir || !parentDir) { + // check .env under home as fallback, again preferring gemini-specific .env + const homeGeminiEnvPath = path.join(homedir(), GEMINI_DIR, '.env'); + if (fs.existsSync(homeGeminiEnvPath)) { + return homeGeminiEnvPath; + } + const homeEnvPath = path.join(homedir(), '.env'); + if (fs.existsSync(homeEnvPath)) { + return homeEnvPath; + } + return null; + } + currentDir = parentDir; + } +} + +export function loadEnvironment(): void { + const envFilePath = findEnvFile(process.cwd()); + if (envFilePath) { + dotenv.config({ path: envFilePath, quiet: true }); + } +} + /** * Loads settings from user and workspace directories. * Project settings override user settings. */ export function loadSettings(workspaceDir: string): LoadedSettings { + loadEnvironment(); let userSettings: Settings = {}; let workspaceSettings: Settings = {}; const settingsErrors: SettingsError[] = []; |
