fall back to ~/.env if .env is not found in current directory or any ancestors (#338)
This commit is contained in:
parent
3be8b6dc34
commit
4a0f5476c0
|
@ -8,6 +8,7 @@ import * as dotenv from 'dotenv';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
|
import * as os from 'node:os';
|
||||||
import { ToolRegistry } from '../tools/tool-registry.js';
|
import { ToolRegistry } from '../tools/tool-registry.js';
|
||||||
import { LSTool } from '../tools/ls.js';
|
import { LSTool } from '../tools/ls.js';
|
||||||
import { ReadFileTool } from '../tools/read-file.js';
|
import { ReadFileTool } from '../tools/read-file.js';
|
||||||
|
@ -97,6 +98,11 @@ function findEnvFile(startDir: string): string | null {
|
||||||
}
|
}
|
||||||
const parentDir = path.dirname(currentDir);
|
const parentDir = path.dirname(currentDir);
|
||||||
if (parentDir === currentDir || !parentDir) {
|
if (parentDir === currentDir || !parentDir) {
|
||||||
|
// check ~/.env as fallback
|
||||||
|
const homeEnvPath = path.join(os.homedir(), '.env');
|
||||||
|
if (fs.existsSync(homeEnvPath)) {
|
||||||
|
return homeEnvPath;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
currentDir = parentDir;
|
currentDir = parentDir;
|
||||||
|
|
|
@ -33,7 +33,7 @@ shift $((OPTIND - 1))
|
||||||
|
|
||||||
# if GEMINI_CODE_SANDBOX is not set, see if it is set in user settings
|
# if GEMINI_CODE_SANDBOX is not set, see if it is set in user settings
|
||||||
# note it can be string or boolean, and if missing jq will return null
|
# note it can be string or boolean, and if missing jq will return null
|
||||||
USER_SETTINGS_FILE=~/.gemini/settings.json
|
USER_SETTINGS_FILE="$HOME/.gemini/settings.json"
|
||||||
if [ -z "${GEMINI_CODE_SANDBOX:-}" ] && [ -f "$USER_SETTINGS_FILE" ]; then
|
if [ -z "${GEMINI_CODE_SANDBOX:-}" ] && [ -f "$USER_SETTINGS_FILE" ]; then
|
||||||
USER_SANDBOX_SETTING=$(jq -r '.sandbox' "$USER_SETTINGS_FILE")
|
USER_SANDBOX_SETTING=$(jq -r '.sandbox' "$USER_SETTINGS_FILE")
|
||||||
if [ "$USER_SANDBOX_SETTING" != null ]; then
|
if [ "$USER_SANDBOX_SETTING" != null ]; then
|
||||||
|
@ -45,13 +45,20 @@ fi
|
||||||
# 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)
|
||||||
if [ -z "${GEMINI_CODE_SANDBOX:-}" ]; then
|
if [ -z "${GEMINI_CODE_SANDBOX:-}" ]; then
|
||||||
current_dir=$(pwd)
|
current_dir=$(pwd)
|
||||||
|
dot_env_sourced=false
|
||||||
while [ "$current_dir" != "/" ]; do
|
while [ "$current_dir" != "/" ]; do
|
||||||
if [ -f "$current_dir/.env" ]; then
|
if [ -f "$current_dir/.env" ]; then
|
||||||
source "$current_dir/.env"
|
source "$current_dir/.env"
|
||||||
|
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 [ "$dot_env_sourced" = false ] && [ -f "$HOME/.env" ]; then
|
||||||
|
source "$HOME/.env"
|
||||||
|
dot_env_sourced=true
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# lowercase GEMINI_CODE_SANDBOX
|
# lowercase GEMINI_CODE_SANDBOX
|
||||||
|
|
Loading…
Reference in New Issue