Skip to content

Commit d61cee7

Browse files
committed
fix(env-preload): later layer can override earlier (was: first wins)
The old guard `if (process.env[key] !== undefined) continue` skipped any key that already existed in process.env at the time of the check. After ~/.jitsu/.env.local loaded, every shared key was in process.env, so the repo-level <repo>/.env.local could never override it — backwards from the documented "later wins" precedence. Snapshot the shell's original env keys at startup; only those are untouchable. Keys set by a previous file load are overwritable, so the order is now: shell > <repo>/.env.local > ~/.jitsu/.env.local Verified end-to-end: repo overrides home for shared keys; home-only keys still load; shell still wins over both.
1 parent 6d2512b commit d61cee7

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

env-preload/preload-env.cjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
const fs = require("node:fs");
1616
const path = require("node:path");
1717

18+
// Snapshot keys the shell already set BEFORE we touch anything. Used by
19+
// loadEnvFile to decide what's untouchable (shell wins) vs what a later
20+
// file is allowed to override (an earlier file's value).
21+
const shellEnvKeys = new Set(Object.keys(process.env));
22+
1823
function findRepoRoot(start) {
1924
let dir = path.resolve(start);
2025
while (true) {
@@ -39,7 +44,10 @@ function loadEnvFile(filePath) {
3944
const m = /^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(line);
4045
if (!m) continue;
4146
const key = m[1];
42-
if (process.env[key] !== undefined) continue;
47+
// Only the shell's original env is untouchable. A key set by an earlier
48+
// file (e.g. ~/.jitsu/.env.local) gets overwritten here, which is the
49+
// documented "later wins" precedence.
50+
if (shellEnvKeys.has(key)) continue;
4351
let value = m[2];
4452
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
4553
value = value.slice(1, -1);

0 commit comments

Comments
 (0)