fix: env.ts 实时读取 ~/.openclaw/.env,不缓存
- 移除 loaded 标志,每次都从文件读取 - 移除 loadedKeys 追踪 - 直接覆盖 process.env,不判断是否已存在 - 确保文件值永远是最新的
This commit is contained in:
parent
06cd08d833
commit
e5944a75a2
24
src/env.ts
24
src/env.ts
|
|
@ -4,29 +4,20 @@ import * as os from 'os';
|
|||
|
||||
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 {
|
||||
if (loaded) return;
|
||||
loaded = true;
|
||||
applyEnvFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Force re-read ~/.openclaw/.env. Overwrites keys that were originally loaded
|
||||
* from .env, but still won't touch keys set externally (e.g. shell export).
|
||||
* Force re-read ~/.openclaw/.env.
|
||||
* Same as loadGlobalEnv — always reads fresh.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
@ -53,10 +44,7 @@ function applyEnvFile(): void {
|
|||
value = value.slice(1, -1);
|
||||
}
|
||||
|
||||
// don't overwrite explicitly set env vars
|
||||
if (process.env[key] === undefined) {
|
||||
// Always use file value — real-time read, no caching
|
||||
process.env[key] = value;
|
||||
loadedKeys.add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue