Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/runtime/shell/EnvMap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ impl EnvMap {
Some(val)
}

/// Removes `key` if present, dereffing the stored key and value it owned.
pub fn remove(&mut self, key: EnvStr) {
if let Some((k, v)) = self.map.fetch_swap_remove(&key) {
k.deref();
v.deref();
}
}

pub fn clone(&self) -> EnvMap {
let new = EnvMap {
map: self.map.clone().expect("OOM"),
Expand Down
17 changes: 16 additions & 1 deletion src/runtime/shell/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,22 @@
) {
match assign_ctx {
AssignCtx::Cmd => self.cmd_local_env.insert(label, value),
AssignCtx::Shell => self.shell_env.insert(label, value),
AssignCtx::Shell => {
// POSIX: a plain assignment to a name that already carries the
// export attribute keeps it exported, so children must see the
// new value. Update the exported binding in place rather than
// shadowing it with a shell-local entry.
if let Some(existing) = self.export_env.get(label) {
existing.deref();
// Drop any shell-local entry so it can't shadow the exported
// value: `$VAR` expansion consults `shell_env` before
// `export_env`.
self.shell_env.remove(label);

Check warning on line 2154 in src/runtime/shell/interpreter.rs

View check run for this annotation

Claude / Claude Code Review

export builtin does not clear shell_env shadow (same-class sibling)

Pre-existing, same-class sibling: the `export` builtin (`src/runtime/shell/builtin/export.rs:43`) writes directly to `export_env` and never clears `shell_env`, so `FOO=a; export FOO=b; echo $FOO` still prints `a` (bash: `b`). Since f3db27c added `EnvMap::remove` specifically for this shadow class, it'd be a one-liner to fix the export builtin here too. (The `AssignCtx::Exported` arm at line 2160 has the same gap but appears to be dead code — nothing constructs it.)
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
self.export_env.insert(label, value);
} else {
self.shell_env.insert(label, value);
}
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
}
AssignCtx::Exported => self.export_env.insert(label, value),
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/js/bun/shell/bunshell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,38 @@ booga"

expect(procEnv).toEqual({ ...bunEnv, BUN_TEST_VAR: "1", FOO: "bar" });
});

// POSIX: a plain assignment to a name that already carries the export
// attribute keeps it exported, so the new value must reach child processes,
// not just the shell's own `$VAR` view.
test("reassigning an exported var updates both the shell and the child", async () => {
const env = { ...bunEnv, OUTER: "fromenv" };
const code = "process.stdout.write('child=' + (process.env.OUTER ?? '<unset>'))";
const { stdout } = await $`OUTER=changed; echo shell=$OUTER; ${BUN} -e ${code}`.env(env);
expect(stdout.toString()).toBe("shell=changed\nchild=changed");
});

test("append idiom on an exported var reaches the child", async () => {
const env = { ...bunEnv, OUTER: "fromenv" };
const code = "process.stdout.write(process.env.OUTER ?? '<unset>')";
const { stdout } = await $`OUTER="$OUTER+more"; ${BUN} -e ${code}`.env(env);
expect(stdout.toString()).toBe("fromenv+more");
});

test("bare assignment to a non-exported name stays shell-local", async () => {
const code = "process.stdout.write(process.env.NEWV ?? '<unset>')";
const { stdout } = await $`NEWV=nv; echo shell=$NEWV; ${BUN} -e ${code}`.env(bunEnv);
expect(stdout.toString()).toBe("shell=nv\n<unset>");
});

// A shell-local var that is later exported and then reassigned must not keep
// a stale shell-local entry shadowing the exported value: `$VAR` expansion
// checks shell_env before export_env.
test("bare reassignment after export clears the shell-local shadow", async () => {
const code = "process.stdout.write('child=' + (process.env.FOO ?? '<unset>'))";
const { stdout } = await $`FOO=a; export FOO=b; FOO=c; echo shell=$FOO; ${BUN} -e ${code}`.env(bunEnv);
expect(stdout.toString()).toBe("shell=c\nchild=c");
});
});

describe("cd & pwd", () => {
Expand Down
Loading