Skip to content

Commit 71036e4

Browse files
authored
fix: warn when the environment overrides a .env setting (#57)
process.loadEnvFile never overrides variables that are already set, so a stale shell export or a system-wide variable silently wins over .env. For example, a system-wide OPENAI_API_KEY for OpenAI gets sent to an OPENAI_BASE_URL gateway instead of the gateway key in .env, and the only symptom is a provider authentication error. On startup, name the .env keys whose value is replaced by a different environment value. Only key names are logged, never values. Loading precedence is unchanged.
1 parent 1194b26 commit 71036e4

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎apps/server/src/config.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
import { existsSync } from "node:fs";
1+
import { existsSync, readFileSync } from "node:fs";
22
import { resolve } from "node:path";
3+
import { parseEnv } from "node:util";
34

4-
if (existsSync(".env")) process.loadEnvFile(".env");
5+
/** .env keys whose file value loses to a different value already set in the environment. */
6+
export function shadowedEnvKeys(
7+
file: Record<string, string | undefined>,
8+
env: Record<string, string | undefined> = process.env,
9+
): string[] {
10+
return Object.keys(file).filter((key) => env[key] !== undefined && env[key] !== file[key]);
11+
}
12+
13+
if (existsSync(".env")) {
14+
// loadEnvFile never overrides existing variables. A stale shell or system-wide value
15+
// (for example OPENAI_API_KEY) would otherwise silently replace the .env setting.
16+
const shadowed = shadowedEnvKeys(parseEnv(readFileSync(".env", "utf8")));
17+
process.loadEnvFile(".env");
18+
if (shadowed.length)
19+
console.warn(
20+
`[OpenMuse] Using ${shadowed.join(", ")} from the environment instead of .env. ` +
21+
(shadowed.length === 1
22+
? "Unset it to use the .env value."
23+
: "Unset them to use the .env values."),
24+
);
25+
}
526
process.env.DO_NOT_TRACK ??= "1";
627
process.env.COPILOTKIT_TELEMETRY_DISABLED ??= "true";
728

‎tests/config.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import assert from "node:assert/strict";
22
import { test } from "node:test";
3-
import { assertApiDeploymentConfig, type Config } from "../apps/server/src/config.ts";
3+
import {
4+
assertApiDeploymentConfig,
5+
type Config,
6+
shadowedEnvKeys,
7+
} from "../apps/server/src/config.ts";
48

59
const sampleConfig: Config = {
610
mode: "sample",
@@ -46,3 +50,10 @@ test("every API mode accepts a non-empty Intelligence key", () => {
4650
);
4751
}
4852
});
53+
54+
test("environment variables that override a different .env value are reported by name", () => {
55+
const file = { OPENAI_API_KEY: "sk-or-file", MODEL: "openai/gpt-5", PORT: "8787", EMPTY: "" };
56+
const env = { OPENAI_API_KEY: "sk-proj-system", MODEL: "openai/gpt-5", EMPTY: "set" };
57+
assert.deepEqual(shadowedEnvKeys(file, env), ["OPENAI_API_KEY", "EMPTY"]);
58+
assert.deepEqual(shadowedEnvKeys(file, {}), []);
59+
});

0 commit comments

Comments
 (0)