Repository navigation
Conversation
|
Updated 6:17 AM PT - Nov 15th, 2026
❌ @robobun, your commit f4dab31 has 1 failures in
🧪 To try this PR locally: bunx bun-pr 49010That installs a local version of the PR into your bun-49010 --bun |
|
Reproduction on main f9ac4bd (release build, Linux x64): // main.js
const path = require("path");
process.env.NODE_ENV = "assigned-at-runtime";
console.log(require(path.join(process.cwd(), "from-disk.cjs")));
// from-disk.cjs (beside the executable when it runs, not in the bundle)
module.exports = [process.env.NODE_ENV, process.env["NODE_ENV"], String(process.env.BUN_ENV)].join(" ");bun build --compile --no-compile-autoload-dotenv ./main.js --outfile ./exe-off
bun build --compile ./main.js --outfile ./exe-default
./exe-off # development development development
./exe-default # assigned-at-runtime assigned-at-runtime undefined
NODE_ENV='C:\Program Files\nodejs' ./exe-off # exit 1, error: Syntax Error at defines.json:1:1With this PR
CI (build 141831) is complete: 186 jobs pass and 2 fail. Both failures are |
|
No actionable comments were generated in the recent review. WalkthroughThe change adds a setting that controls whether implicit environment defines are supplied at build time or read at runtime. Standalone runtime flags select runtime reads. New tests cover disk-loaded modules and Workers. ChangesRuntime environment defines
Possibly related PRs
Suggested reviewers: Merge Risk:
|
There was a problem hiding this comment.
Beyond the inline finding, I also checked that the new field is live on both standalone paths (run_command.rs calls apply_standalone_runtime_flags before configure_defines for the main VM, and web_worker.rs does the same for Workers via the hook), that Env is only constructed through impl Default so no struct-literal site misses implicit_defines, and that callers passing None for framework_env (src/bundler/aot/defines.rs) keep the build-time defines. The env: { KEY: undefined } run variants rely on the harness spread dropping undefined keys, which matches the existing bunEnv convention. I did not run the two new compile tests or the code-from-disk row.
Extended reasoning...
The change adds an ImplicitDefines field to bundler::options::Env, gates the NODE_ENV/BUN_ENV/process.browser define injection in src/bundler/options.rs on it, and sets it to LeftToRuntime in apply_standalone_runtime_flags for standalone executables; it touches no security-sensitive surface. The one confirmed inline finding is a behavior change (disk-loaded JSX plus React under a tsconfig react-jsx now hits jsxDEV is not a function) that the PR description itself names as a known regression, so a human needs to weigh that tradeoff rather than an automated approval.
| DotEnvBehavior::LoadAllWithoutInlining | ||
| }; | ||
| // Modules from disk read NODE_ENV, BUN_ENV and process.browser when they run, in either mode. | ||
| b.options.env.implicit_defines = bun_bundler::options::ImplicitDefines::LeftToRuntime; |
There was a problem hiding this comment.
🔴 Users whose --no-compile-autoload-dotenv executable renders React and .jsx loaded from disk, with a tsconfig "jsx": "react-jsx", now get TypeError: jsxDEV is not a function where the base build rendered. src/bun.js.rs:23 makes React read the run-time NODE_ENV, but the transpiler still picks jsxDEV from the start-time env and never applies the tsconfig jsx, because the disable arm of run_env_loader (src/bundler/transpiler.rs:673) lacks the merge the other arm does at transpiler.rs:637. Fix: when an executable leaves NODE_ENV to the runtime, choose the disk JSX runtime the way bun run and a default executable do, i.e. apply the top-level tsconfig jsx in the disable arm too, so react-jsx selects jsx, which both React builds export.
Why this was flagged
Trigger: an executable built with --no-compile-autoload-dotenv whose working directory holds a tsconfig.json with "jsx": "react-jsx", loads a .jsx file and React 18 from disk, and either the entry point sets process.env.NODE_ENV = "production" before React loads, or the process starts with NODE_ENV=production and BUN_ENV set to something else. src/bun.js.rs:23 sets implicit_defines = LeftToRuntime, so src/bundler/options.rs:963-965 injects no process.env.NODE_ENV define; React's jsx-dev-runtime.js then reads the real NODE_ENV ("production") and loads its production build, which exports no jsxDEV. The transpiler's jsx.development stays at its default true (src/options_types/jsx.rs:187), and the tsconfig jsx is never merged, because the DotEnvBehavior::disable arm at src/bundler/transpiler.rs:673-680 has no merge_tsconfig_jsx_into. The disk .jsx is emitted as jsxDEV(...) from react/jsx-dev-runtime, and the call throws TypeError: jsxDEV is not a function. On the base branch the same executable defined process.env.NODE_ENV to the quoted start-time value, so React's build and the chosen jsx runtime always agreed and the page rendered.
Verification: src/bun.js.rs:17-23 sets implicit_defines = LeftToRuntime, so src/bundler/options.rs:963-964 skips the process.env.NODE_ENV define and React from disk reads the live value, "production". The disable arm of run_env_loader (src/bundler/transpiler.rs:673-680) has no merge_tsconfig_jsx_into, so the transpiler emits jsxDEV from react/jsx-dev-runtime, which production React leaves undefined.
There was a problem hiding this comment.
The row is real. I measured it on release builds of main and of this PR. It is in Downsides and in the last row of "Rows that get worse" in the Notes.
Two details of the trigger:
- It needs
--compile-autoload-tsconfig. Without that flag no executable reads the tsconfig, and a default executable throws in the same way. - On main the executable renders because React loads its development build although
NODE_ENVisproduction. The transform isjsxDEVon main too. An executable built with.envautoload off never applies the JSX settings of the tsconfig.
I did not put the merge into the disable arm, because bun build, Bun.build and new Bun.Transpiler() run that arm too. The merge changes what a default build emits:
// tsconfig.json in the working directory: { "compilerOptions": { "jsx": "react-jsx" } }
// app.jsx: console.log(<div />);
await Bun.build({ entrypoints: ["./app.jsx"], external: ["*"] });
// today, the `disable` arm: jsxDEV("div", {}, undefined, false, undefined, this)
// with `env: "inline"`, the arm that has the merge: jsx("div", {})A merge for standalone executables alone is possible. apply_standalone_runtime_flags can apply the root tsconfig JSX when tsconfig autoload is on. I left it out of this PR because it has two costs:
- It reads the working directory and its parents at each start. A hello world built with
--compile-autoload-tsconfig --no-compile-autoload-dotenvhas 205 syscalls at start. With.envautoload on, which does that read, it has 233. - It is a second behavior change. These executables then apply the
jsxandjsxFactorysettings of the tsconfig to code from disk. On main they ignore both.
I can add it to this PR if a maintainer wants the two changes together.
…en .env autoload is off An executable built with `--no-compile-autoload-dotenv` runs its transpiler in the env mode `disable`, so that the loader reads no `.env` file. The define gate treats that mode as a build. It replaced `process.env.NODE_ENV`, `process.env.BUN_ENV` and `process.browser` in each module that the executable loads from disk, with the values of process start. A start value that is not valid inside a JSON string, such as a Windows path, stopped the executable at start with `Syntax Error at defines.json:1:1`. `options::Env` gets the field `implicit_defines`, and the gate in `defines_from_transform_options` reads it. `apply_standalone_runtime_flags` sets it to `LeftToRuntime`. The loader mode does not change. The main thread and Workers share this function.
5847d1f to
816fdc8
Compare
There was a problem hiding this comment.
Code review found no new issues
No new issues were found in this update; 1 finding from earlier reviews is still open above.
Still open from earlier reviews (1):
- 🔴
src/bun.js.rs:23—Users whose --no-compile-autoload-dotenv executable renders React and .jsx loaded from disk, with a tsconfig "jsx": "re…
If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.
Problem
--no-compile-autoload-dotenvexits 1 at start whenNODE_ENVisC:\Program Files\nodejs:error: Syntax Error at defines.json:1:1.process.env.NODE_ENV,process.env.BUN_ENVandprocess.browserare values of process start. A default executable reads the current values.apply_standalone_runtime_flags(src/bun.js.rs:17) selects the env modedisableto skip.envfiles. The define gate (src/bundler/options.rs:963) takes that mode for a build.Fix
options::Envgetsimplicit_defines, which the gate reads.apply_standalone_runtime_flagssets it toLeftToRuntime.disable: a start has 205 syscalls before and after.test/bundler/bundler_compile_autoload.test.ts(2 new cases fail on main), one row intest/aot/code-from-disk.test.ts.Background
DotEnvBehavior) selects the.envfiles the loader reads. The define gate reads the same mode.LoadAllWithoutInlining(compile: do not inline NODE_ENV into runtime-transpiled files when .env autoload is off #38672): each start then lists the working directory, 32 ms against 4 ms with 100,000 entries.Downsides
undefined, not"development", whenNODE_ENVis not set, andBUN_ENVno longer answers for it. ANODE_ENVwith literal quotes is no longer production..jsxfile from disk throwjsxDEV is not a functionwhenNODE_ENVbecomesproductionafter start, as in a default executable. One tsconfig case gets worse (Notes).Notes
Behavior
Code that an executable built with
.envautoload off loads from disk. "default" is an executable built without the flag. Each row was run on release builds of main f9ac4bd and of this PR.NODE_ENV='C:\Program Files\nodejs', or another value that is not valid inside a JSON string, also for a hello worldSyntax Error at defines.json:1:1NODE_ENVnot set"development"undefinedundefinedprocess.env.NODE_ENV = "x""x""x"delete process.env.NODE_ENVin disk codeprocess.env.NODE_ENV = process.env.NODE_ENV || "production"in disk code, not set at start"development", child processes see it"production""production"NODE_ENV=production BUN_ENV=stagingNODE_ENVreads"staging""production""production"BUN_ENV=productionaloneNODE_ENVreads"production"undefinedundefinedNODE_ENV=(empty)"development"""""process.browser = "x", then readfalse"x""x"env: { NODE_ENV: "x" }, file on disk"x""x".jsx, bunfigjsx = "react-jsx",NODE_ENV=developmentjsxDEVjsxjsxBUN_OPTIONS="--define process.env.NODE_ENV=..."Code in the bundle keeps the value of the build in every row (24 of 24 control rows equal on both builds).
Rows that get worse
NODE_ENV='"production"'with the quotes in the value,Bun.serve()in the bundle.jsxfile from disk, the entry point assignsNODE_ENV = "production"firstTypeError: jsxDEV is not a functionTypeErrorNODE_ENV=production BUN_ENV=stagingTypeErrorTypeError--compile-autoload-tsconfig, tsconfig"jsx": "react-jsx"TypeErrorjsxThe
TypeErrorneeds three things: JSX and React both come from disk, the process does not start in production mode, andNODE_ENVisproductionwhen React loads.bun runthrows in the first two JSX rows too. A bunfig.toml withjsx = "react-jsx"avoids it.The last row is the one place where this PR is worse than a default executable. The cause is older than this PR: an executable built with
.envautoload off never applies the JSX settings of a tsconfig.json in the working directory, also with--compile-autoload-tsconfig(on main ajsxFactorythere has no effect either). The merge of those settings lives in the.envarm ofTranspiler::run_env_loader. This PR does not move it, because that merge reads the working directory at each start. A hello world built with--compile-autoload-tsconfig --no-compile-autoload-dotenvhas 205 syscalls at start, and 233 with.envautoload on, which does that read.The
disablearm cannot take the merge as it is, becausebun build,Bun.buildandnew Bun.Transpiler()run that arm too. With a tsconfig"jsx": "react-jsx"in the working directory, a defaultBun.buildof a.jsxfile emitsjsxDEV. The same build withenv: "inline", which takes the arm that has the merge, emitsjsx.Designs weighed
LoadAllWithoutInliningwithdisable_default_env_files(compile: do not inline NODE_ENV into runtime-transpiled files when .env autoload is off #38672). It needs no field. Each start then walks the working directory and its parents: 28 more syscalls for a hello world at depth 6, and 32 ms against 4 ms in a directory of 100,000 entries. An in-processBun.buildof the project in that directory then resolves 0 of 3 probes, as in a default executable today. A run-time--env-filealso starts to load.Transpiler::configure_defines, between the loader and the gate, for a transpiler whose resolver has the standalone graph. It needs no field.apply_standalone_runtime_flagsthen selectsdisable, and another function changes the mode later.options::Env(this PR). The function that selects the mode of a standalone runtime also states what the gate does with it. The field takes a padding byte ofEnv, and only this function sets it.Not changed
--env-fileis still ignored by such an executable.Bun.buildof the project in the working directory still resolves tsconfigpaths, package.jsonimportsandjsxFactory: 3 of 3 probes right before and after. The same executable built with.envautoload on resolves 0 of 3.Bun.build({ env: "inline" })in such an executable still reads the.envfile of the working directory.bun buildandBun.buildoutput: 0 of 216 files changed (sha256).bun build,Bun.buildandnew Bun.Transpiler()still fail for aNODE_ENVwith a backslash. bundler: JSON-escape the NODE_ENV auto-define value #41476 fixes the quoting for them.new Bun.Transpiler(), the scanner ofbun test --changedand the bake server graph build their define table through the same gate and do not pass this function.For releases without this change
Three defines give the same result on 1.4.x. Put them in
--compile-exec-argvor inBUN_OPTIONS:They also remove the start exit. They show in
process.execArgv.Measurements
Release builds (LTO, linux-x64) of main f9ac4bd and of this PR on it, same toolchain.
strace,perf,valgrindandbloatyare not installed on the machine. The counts come from gdb (catch syscall, breakpoints on the mimalloc entry points), fromperf_event_open(instructions:u), and fromnm -S,readelf -Sandstat.catch syscall, cwd depth 6). A default executable: 7, 14, 233.LoadAllWithoutInliningwould add.mi_free: 23 -> 9 and 62 -> 48.movb $0x1,0x42d(%reg)). configure_defines in an autoload-off executable: 113,655 -> 103,298 instructions on the main thread and 109,452 -> 99,868 in a Worker. Default executable: 275,661 -> 275,698 and 101,236 -> 101,239 (run-to-run spread 70).bun run hello.js3,292,685 -> 3,299,498 instructions for the whole process (two samples of this PR differ by 6,450),bun build hello.js4,572,664 -> 4,553,610 (spread 350,000)..text(124,623,113 in both), stripped file +0 bytes (155,322,640 in both). Four functions change size, net +20 bytes inside alignment padding: the two copies of the hook +7 each,Transpiler::for_worker+42,Transpiler::configure_defines-36.bloatyis not installed:nm -S,readelf -S,stat.NODE_ENV{unset, development, production} xBUN_ENV{unset, staging} x {require .cjs, import .mjs, import .ts, disk Worker, blob Worker,BUN_OPTIONSpreload,Bun.plugincontents} x {before an assignment, after it, after a reassignment, after a delete}.env{absent, disable, inline, PUBLIC_*} x target {browser, bun, node} xNODE_ENV{unset, development, production} x {CLI,Bun.build} x {aNODE_ENVbranch, a React entry, a macro import}.undefined), cwd tsconfig JSX not merged (jsxDEV), in-process Bun.build 3 of 3 right.jsxDEV, PRjsx, default executablejsx, bun runjsx.Tests
compile/AutoloadDotenvDisabledReadsNodeEnvAtRuntime(Bun.build): an ES module and a.jsxfile on disk. Five runs: no variables,NODE_ENV=production BUN_ENV=staging,NODE_ENV='C:\Program Files\nodejs',NODE_ENV=development, and a--defineinBUN_OPTIONS. On main each of the five runs fails.compile/AutoloadDotenvDisabledReadsNodeEnvAtRuntimeWorkerCLI(bun build): a Worker whose TypeScript file is on disk and requires a CommonJS file, without and with anenvoption. The build has the autoload options of the example in the docs.test/aot/code-from-disk.test.ts, "a file from disk reads NODE_ENV, BUN_ENV and process.browser as they are when it runs": the same read in an executable built with--aot --aot-allow-code-from-disk. It fails on a release build of main. A debug build skips this file.test/aot/runner-suites.programs.txtnames the two new cases. That run builds each executable with--aotand no permission to load code from disk, so both cases fail there like the 9 cases of the file that are listed already. With a release build of this PR the run ofholdProgramsOfSuitesfor the file passes with the two lines and fails without them.run.envofexpectBundled.tsnow takesundefined, to clear aBUN_ENVof the process that runs the tests.bundler_compile_autoload.test.ts25 of 25.bundler_compile.test.ts(86),bundler_edgecase.test.ts(197),test/cli/run/env.test.ts(109),test/js/web/workers/worker.test.ts(44),test/bundler/transpiler/transpiler.test.js(258), andbun run rust:test -p bun_bundler(178).Self-review
The review raised 41 concerns. 11 of them are one finding: an earlier cut of this change was stacked on #48933 and wrote a field that #48933 no longer has. This PR carries the field itself and targets main. Also done: two executable links in the tests and not four, the AOT row, the two lines of the programs list, a failure path for the Worker helper,
BUN_ENVcleared in the runs, the rows under "Rows that get worse", and the costs of the other mode.Not done:
Other open PRs
Bun.build({ env: "disable" })and leaves these executables out. Its first revision had this field and gate. It now takes two defines out of the table after the build derived its mode from them. From a read of its diff, a runtime cannot use that: the defines are parsed before, so the start exit stays."production"until this PR merges.env = falsein executables #46257 edits another line of the same function.History
.envloading. Its text says nothing aboutNODE_ENV. fix(windows): avoid standalone worker dotenv crash #27434 moved the code into the function that Workers share.process.envduringbun runandbun test#7614 stopped the replacement forbun runandbun test, and Robustness and input-handling pass across install, shell, TLS/QUIC, HTTP/3, SQL and crypto #37669 for Workers.LoadAllWithoutInlining. The inactivity sweep closed it with no review."C:\Program Files\nodejs"and the error above.autoloadDotenv: falseand loads config files from disk.no test proof · iteration 2 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/aot/code-from-disk.test.ts