(fix): .env vars not available during runtime config reloads (healthchecks fail with MissingEnvVarError) (#12748)

* Config: reload dotenv before env substitution on runtime loads

* Test: isolate config env var regression from host state env

* fix: keep dotenv vars resolvable on runtime config reloads (#12748) (thanks @rodrigouroz)

---------

Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
Rodrigo Uroz
2026-02-09 19:31:41 -03:00
committed by GitHub
parent b40a7771e5
commit ae99e656af
3 changed files with 60 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import fs from "node:fs/promises";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { resolveStateDir } from "./paths.js";
import { withEnvOverride, withTempHome } from "./test-helpers.js";
describe("config env vars", () => {
@@ -75,4 +76,50 @@ describe("config env vars", () => {
});
});
});
it("loads ${VAR} substitutions from ~/.openclaw/.env on repeated runtime loads", async () => {
await withTempHome(async (home) => {
await withEnvOverride(
{
OPENCLAW_STATE_DIR: path.join(home, ".openclaw"),
CLAWDBOT_STATE_DIR: undefined,
OPENCLAW_HOME: undefined,
CLAWDBOT_HOME: undefined,
BRAVE_API_KEY: undefined,
OPENCLAW_DISABLE_CONFIG_CACHE: "1",
},
async () => {
const configDir = resolveStateDir(process.env, () => home);
await fs.mkdir(configDir, { recursive: true });
await fs.writeFile(
path.join(configDir, "openclaw.json"),
JSON.stringify(
{
tools: {
web: {
search: {
apiKey: "${BRAVE_API_KEY}",
},
},
},
},
null,
2,
),
"utf-8",
);
await fs.writeFile(path.join(configDir, ".env"), "BRAVE_API_KEY=from-dotenv\n", "utf-8");
const { loadConfig } = await import("./config.js");
const first = loadConfig();
expect(first.tools?.web?.search?.apiKey).toBe("from-dotenv");
delete process.env.BRAVE_API_KEY;
const second = loadConfig();
expect(second.tools?.web?.search?.apiKey).toBe("from-dotenv");
},
);
});
});
});