Why doesn't Bun.spawn see the env var I just set?
🌱 seedlingplanted tended #bun#terminal#lessons
Written for Anyone writing Bun scripts or scheduled job runners that load keys from a .env file at runtime and then start child processes.
Run these four lines on your own machine first. Paste them into a file probe.ts and type bun probe.ts:
process.env.PROBE = "set-at-runtime";
const sh = ["sh", "-c", 'printf %s "${PROBE:-<missing>}"'];
console.log("spawnSync:", Bun.spawnSync(sh).stdout.toString());
console.log("with env: ", Bun.spawnSync(sh, { env: process.env }).stdout.toString());
On Bun 1.4.4 the first line prints <missing> and the second prints set-at-runtime. Same variable, same command, two answers. A variable you assign to process.env at runtime never reaches the child, unless you hand it over yourself.
The main numbers I measured on the morning of 1 November on Bun 1.4.4, once on my Mac and once on the Linux VM that serves this very garden. Later that day I measured 11 older Bun versions on the Mac. On the morning of 15 November I ran the snippet above on 1.4.4 again: unchanged.
Tiếng Việt: Vì sao Bun.spawn chẳng thấy biến môi trường mình vừa gán?.
Seven ways to start a child, no two alike
I measured seven ways to start a child, and they don't agree. The script changes process.env in three ways, then asks a child what it sees. On Bun 1.4.4 the result is identical on macOS and on Linux. Try the Bun version and the kind of change; a pink cell is a child still holding the old copy:
| how the child is started | does it see the change? |
|---|---|
Bun.spawn(cmd) | no |
Bun.spawnSync(cmd) | no |
Bun.spawn(cmd, { env: process.env }) | yes |
Bun.spawn(cmd, { env: { ...process.env } }) | yes |
Bun.$`…` | yes |
node:child_process spawnSync | yes |
node:child_process execSync | yes |
What surprised me most were the last two rows. On 1.4.4, node:child_process sees everything, so if you come from Node you'll assume Bun.spawn does too, and that's exactly the trap. Click over to 1.3.14 and those two rows miss as well.
Changing or deleting a variable misses too
I tried two more cases, to be sure it isn't only new variables.
- Start with
OLR_PRE=boot, then setprocess.env.OLR_PRE = "changed". ABun.spawnSyncchild printsboot. - Delete
HOMEwithdelete process.env.HOME. The child still sees the oldHOME, untouched.
So every change to process.env after startup fails to reach the child, unless you pass env. Adding, changing, deleting: all three. This kind of bug is hard to spot because it's silent. Nothing throws; there's just an empty variable on the other side.
Bun keeps two copies of the environment
On the morning of 1 November I wrote that I didn't understand this part. That evening I read Bun's source and its tracker, and the answer was sitting right there.
Bun.spawn reads the old copy. One lives in the native core, loaded exactly once at startup. The other is process.env on the JavaScript side, and an assignment writes only to that one. Call Bun.spawn without env and the core uses its own copy. Bun.$ and node:child_process take process.env from the JavaScript side and pass it down, so they see the truth.
Bun.which shares the same root. I added a directory to process.env.PATH at runtime, put a tool in it, and asked Bun.which. It returned null on all 12 versions I measured. On 1.4.4, Bun.spawnSync(["tool"]) also throws, saying the command isn't found.
This trap is old, and someone is working on it. I measured 12 versions from 1.0.0 to 1.4.4: Bun.spawn without env misses on all 12. node:child_process misses up to 1.3.14 and gets it right from 1.4.0. An issue from May 2024 was already complaining about PATH; it was closed on 25 July 2026 with no fix attached, and on 15 November Bun.which on 1.4.4 still returns null. Patch #34972, open since July, names the disease: "inherit live process.env instead of the startup snapshot". #40571 is a draft that fixes the root, making an assignment write down to the real environment. As of 15 November neither has been merged.
The only alarm that could still send
I hit this in a scheduled job runner on Bun that mails people new matches for what they signed up for. The runner loads the mail key from a config file into process.env, then starts each job with Bun.spawn.
Every morning an alarm landed: "every send failed". The alarm ran inside the parent process, so it saw the key and mailed just fine. The mailing job ran in a child, with no key at all. So the only thing in the world that could send mail was the mail complaining that no mail could be sent. I had to read that part a few times before I believed it.
The fix is one line; prevention takes a bit more
Wherever you call Bun.spawn or Bun.spawnSync and want the child to see the current environment, pass it in explicitly:
Bun.spawn(cmd, { env: process.env });
The job runner now passes env where it starts each job, and the mail went out again the next morning. It also gained a small check job that runs exactly as a child does, and reports whether it can see the key. The old alarm stood in the parent, so it saw a different world from the job it was guarding. A check you can trust has to stand where the thing it checks stands.
I also went through this garden's own scripts. Six files call Bun.spawn, but none assigns to process.env at runtime, so nothing misses yet. Once the patch lands I'll still keep env: process.env, because it was right on every version I measured from 1.0.30 on.
It's a bit like the time pkill -f cat closed every app: a harmless-looking command doing exactly what it was written to do, which just isn't what I thought it did.
What I still don't know
- I don't know which patch will be merged, or when. When it happens, code that happens to lean on the old behaviour will change silently.
- I haven't measured on Windows. The patch notes say Windows takes its own path, so these results may not hold there.
- I haven't traced which change in 1.4.0 fixed
node:child_process.
Write papa dragon a letter
If this note stirred something, write to me here. A letter reaches only me and never shows up on the page by itself. If one is lovely and you allow it, I may quote it under the note, without your name.
Works from a terminal too: curl -d 'msg=…' olragon.com/garden/bun-spawn-env-en/letter