prefer to load gemini-specific .env file from .gemini folder when it exists there (#697)

This commit is contained in:
Olcan 2025-06-02 14:16:48 -07:00 committed by GitHub
parent 7f20425c98
commit 8365c8f954
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import { WebFetchTool } from '../tools/web-fetch.js';
import { ReadManyFilesTool } from '../tools/read-many-files.js'; import { ReadManyFilesTool } from '../tools/read-many-files.js';
import { MemoryTool, setGeminiMdFilename } from '../tools/memoryTool.js'; import { MemoryTool, setGeminiMdFilename } from '../tools/memoryTool.js';
import { WebSearchTool } from '../tools/web-search.js'; import { WebSearchTool } from '../tools/web-search.js';
import { GEMINI_CONFIG_DIR as GEMINI_DIR } from '../tools/memoryTool.js';
export enum ApprovalMode { export enum ApprovalMode {
DEFAULT = 'default', DEFAULT = 'default',
@ -204,13 +205,22 @@ export class Config {
function findEnvFile(startDir: string): string | null { function findEnvFile(startDir: string): string | null {
let currentDir = path.resolve(startDir); let currentDir = path.resolve(startDir);
while (true) { 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'); const envPath = path.join(currentDir, '.env');
if (fs.existsSync(envPath)) { if (fs.existsSync(envPath)) {
return envPath; return envPath;
} }
const parentDir = path.dirname(currentDir); const parentDir = path.dirname(currentDir);
if (parentDir === currentDir || !parentDir) { 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'); const homeEnvPath = path.join(os.homedir(), '.env');
if (fs.existsSync(homeEnvPath)) { if (fs.existsSync(homeEnvPath)) {
return homeEnvPath; return homeEnvPath;

View File

@ -43,21 +43,31 @@ fi
# if GEMINI_SANDBOX is not set, try to source .env in case set there # 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) # 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 if [ -z "${GEMINI_SANDBOX:-}" ]; then
current_dir=$(pwd) current_dir=$(pwd)
dot_env_sourced=false dot_env_sourced=false
while [ "$current_dir" != "/" ]; do 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" source "$current_dir/.env"
dot_env_sourced=true dot_env_sourced=true
break break
fi fi
current_dir=$(dirname "$current_dir") current_dir=$(dirname "$current_dir")
done done
# if .env is not found in any ancestor directory, try ~/.env as fallback # if .env is not found in any ancestor directory, try home as fallback
if [ "$dot_env_sourced" = false ] && [ -f "$HOME/.env" ]; then if [ "$dot_env_sourced" = false ]; then
source "$HOME/.env" if [ -f "$HOME/.gemini/.env" ]; then
dot_env_sourced=true source "$HOME/.gemini/.env"
dot_env_sourced=true
elif [ -f "$HOME/.env" ]; then
source "$HOME/.env"
dot_env_sourced=true
fi
fi fi
fi fi