From 8365c8f954b2732be3ec9441f8464117d4c0cb93 Mon Sep 17 00:00:00 2001 From: Olcan Date: Mon, 2 Jun 2025 14:16:48 -0700 Subject: [PATCH] prefer to load gemini-specific .env file from .gemini folder when it exists there (#697) --- packages/core/src/config/config.ts | 12 +++++++++++- scripts/sandbox_command.sh | 20 +++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 71c4a7d2..dc77208c 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -21,6 +21,7 @@ import { WebFetchTool } from '../tools/web-fetch.js'; import { ReadManyFilesTool } from '../tools/read-many-files.js'; import { MemoryTool, setGeminiMdFilename } from '../tools/memoryTool.js'; import { WebSearchTool } from '../tools/web-search.js'; +import { GEMINI_CONFIG_DIR as GEMINI_DIR } from '../tools/memoryTool.js'; export enum ApprovalMode { DEFAULT = 'default', @@ -204,13 +205,22 @@ export class 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 as fallback + // 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; diff --git a/scripts/sandbox_command.sh b/scripts/sandbox_command.sh index 7c140c61..325722f5 100755 --- a/scripts/sandbox_command.sh +++ b/scripts/sandbox_command.sh @@ -43,21 +43,31 @@ fi # if GEMINI_SANDBOX is not set, try to source .env in case set there # allow .env to be in any ancestor directory (same as findEnvFile in config.ts) +# prefer gemini-specific .env under .gemini folder (also same as in findEnvFile) if [ -z "${GEMINI_SANDBOX:-}" ]; then current_dir=$(pwd) dot_env_sourced=false while [ "$current_dir" != "/" ]; do - if [ -f "$current_dir/.env" ]; then + if [ -f "$current_dir/.gemini/.env" ]; then + source "$current_dir/.gemini/.env" + dot_env_sourced=true + break + elif [ -f "$current_dir/.env" ]; then source "$current_dir/.env" dot_env_sourced=true break fi current_dir=$(dirname "$current_dir") done - # if .env is not found in any ancestor directory, try ~/.env as fallback - if [ "$dot_env_sourced" = false ] && [ -f "$HOME/.env" ]; then - source "$HOME/.env" - dot_env_sourced=true + # if .env is not found in any ancestor directory, try home as fallback + if [ "$dot_env_sourced" = false ]; then + if [ -f "$HOME/.gemini/.env" ]; then + source "$HOME/.gemini/.env" + dot_env_sourced=true + elif [ -f "$HOME/.env" ]; then + source "$HOME/.env" + dot_env_sourced=true + fi fi fi