From e5944a75a2130a63bb3be33664ebd47d05051057 Mon Sep 17 00:00:00 2001 From: ivanberry Date: Fri, 20 Mar 2026 20:15:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20env.ts=20=E5=AE=9E=E6=97=B6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=20~/.openclaw/.env=EF=BC=8C=E4=B8=8D=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 loaded 标志,每次都从文件读取 - 移除 loadedKeys 追踪 - 直接覆盖 process.env,不判断是否已存在 - 确保文件值永远是最新的 --- src/env.ts | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/env.ts b/src/env.ts index 8a38b74..25f8922 100644 --- a/src/env.ts +++ b/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(); -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) { - process.env[key] = value; - loadedKeys.add(key); - } + // Always use file value — real-time read, no caching + process.env[key] = value; } }