fix: env.ts 实时读取 ~/.openclaw/.env,不缓存

- 移除 loaded 标志,每次都从文件读取
- 移除 loadedKeys 追踪
- 直接覆盖 process.env,不判断是否已存在
- 确保文件值永远是最新的
This commit is contained in:
ivanberry 2026-03-20 20:15:17 +08:00
parent 06cd08d833
commit e5944a75a2
1 changed files with 7 additions and 19 deletions

View File

@ -4,29 +4,20 @@ import * as os from 'os';
const GLOBAL_ENV_PATH = path.join(os.homedir(), '.openclaw', '.env'); const GLOBAL_ENV_PATH = path.join(os.homedir(), '.openclaw', '.env');
/** Keys that were loaded from .env (not pre-existing in process.env) */
const loadedKeys = new Set<string>();
let loaded = false;
/** /**
* Load ~/.openclaw/.env into process.env (once, won't overwrite explicitly set env vars). * Load ~/.openclaw/.env into process.env.
* Always reads fresh from disk no caching.
* File values always override process.env.
*/ */
export function loadGlobalEnv(): void { export function loadGlobalEnv(): void {
if (loaded) return;
loaded = true;
applyEnvFile(); applyEnvFile();
} }
/** /**
* Force re-read ~/.openclaw/.env. Overwrites keys that were originally loaded * Force re-read ~/.openclaw/.env.
* from .env, but still won't touch keys set externally (e.g. shell export). * Same as loadGlobalEnv always reads fresh.
*/ */
export function reloadGlobalEnv(): void { export function reloadGlobalEnv(): void {
// Clear values we previously loaded so applyEnvFile can overwrite them
for (const key of loadedKeys) {
delete process.env[key];
}
loadedKeys.clear();
applyEnvFile(); applyEnvFile();
} }
@ -53,10 +44,7 @@ function applyEnvFile(): void {
value = value.slice(1, -1); value = value.slice(1, -1);
} }
// don't overwrite explicitly set env vars // Always use file value — real-time read, no caching
if (process.env[key] === undefined) {
process.env[key] = value; process.env[key] = value;
loadedKeys.add(key);
}
} }
} }