From 181abde2ffa8f1f68b6c540a5600346116cb5145 Mon Sep 17 00:00:00 2001 From: Marat Boshernitsan Date: Thu, 12 Jun 2025 17:17:29 -0700 Subject: Reduce coupling between core and cli packages (#961) Co-authored-by: Marat Boshernitsan --- packages/cli/src/config/config.ts | 49 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'packages/cli/src/config/config.ts') diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 2f989883..3ee03c82 100644 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -9,16 +9,20 @@ import { hideBin } from 'yargs/helpers'; import process from 'node:process'; import { Config, - loadEnvironment, loadServerHierarchicalMemory, setGeminiMdFilename as setServerGeminiMdFilename, getCurrentGeminiMdFilename, ApprovalMode, ContentGeneratorConfig, + GEMINI_CONFIG_DIR as GEMINI_DIR, } from '@gemini-cli/core'; import { Settings } from './settings.js'; import { getEffectiveModel } from '../utils/modelCheck.js'; import { ExtensionConfig } from './extension.js'; +import * as dotenv from 'dotenv'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import * as os from 'node:os'; // Simple console logger for now - replace with actual logger if available const logger = { @@ -196,6 +200,13 @@ export async function loadCliConfig( fileFilteringAllowBuildArtifacts: settings.fileFiltering?.allowBuildArtifacts, checkpoint: argv.checkpoint, + proxy: + process.env.HTTPS_PROXY || + process.env.https_proxy || + process.env.HTTP_PROXY || + process.env.http_proxy, + cwd: process.cwd(), + telemetryOtlpEndpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT, }); } @@ -259,3 +270,39 @@ async function createContentGeneratorConfig( return config; } + +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(os.homedir(), GEMINI_DIR, '.env'); + if (fs.existsSync(homeGeminiEnvPath)) { + return homeGeminiEnvPath; + } + const homeEnvPath = path.join(os.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 }); + } +} -- cgit v1.2.3