Skip to content

Default a compiled executable to production mode - #41506

Open
robobun wants to merge 5 commits into
mainfrom
robobun/47c1050e/compile-serve-production-default
Open

robobun wants to merge 5 commits into
mainfrom
robobun/47c1050e/compile-serve-production-default

Conversation

@robobun

@robobun robobun commented Sep 6, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • A bun build --compile executable serves the development error page by default. Bun.serve() reports development: true, and an uncaught error in a route handler returns a 50 KB text/html page with the message, the stack (/$bunfs/root/app:7:11), and the source line. Same on 1.3.14, 1.4.0, 1.4.2, and canary.
  • The --compile help text says "Implies --production", but nothing sets production for a compiled executable. ServerConfig::from_js (src/runtime/server/ServerConfig.rs:639-645) only leaves development mode when the runtime NODE_ENV is production or vm.transpiler.options.production is set. The standalone boot sets neither.

Fix

  • apply_standalone_runtime_flags (src/bun.js.rs) now calls set_production(true) on the transpiler and resolver options unless NODE_ENV or BUN_ENV in the process env is development or test. This runs before configure_defines, so an explicit NODE_ENV still applies through the existing path. development: true in Bun.serve() still wins. Workers in the executable go through the same hook.
  • Visible effects: Bun.serve() defaults to development: false, and the dotenv loader picks .env.production instead of .env.development. Both match what NODE_ENV=production ./app does today.
  • set_production on bun_bundler::BundleOptions becomes pub so the runtime crate can call it. ServerConfig.rs is unchanged.
  • Verified: test/bundler/bundler_compile.test.ts (compile/ServeDefaultsToProduction, compile/ServeExplicitDevelopment) and test/bundler/bundler_compile_autoload.test.ts (compile/AutoloadDotenvProductionSuffix). All three fail on the stock binary and pass with this change. Also ran all of bundler_compile, bundler_compile_autoload, compile-argv, and bun-serve-html (153 pass).

Background

Notes

Repro on 1.4.3-canary:

const srv = Bun.serve({ port: 0, routes: { "/boom": () => { throw new Error("secret-stack-detail"); } }, fetch: () => new Response("nf", { status: 404 }) });
const r = await fetch(`${srv.url}boom`, { headers: { accept: "text/html" } });
console.log({ dev: srv.development, ct: r.headers.get("content-type"), leaks: (await r.text()).includes("secret-stack-detail") });

bun build --compile app.ts --outfile app && ./app printed { dev: true, ct: "text/html;charset=utf-8", leaks: true }. NODE_ENV=production ./app printed { dev: false, ct: "text/plain", leaks: false }.

A first version set the default in ServerConfig::from_js only. Review moved it to the standalone boot hook so it uses the existing production flag, honours BUN_ENV, and does not duplicate the NODE_ENV parsing in configure_defines.

NODE_ENV=test keeps the non-production default so a compiled executable under a test runner behaves like bun run.

Related: #26373 (the help text promise), #32851 (runtime NODE_ENV read), #38672 (same hook, autoload-off inlining).

Self-reviewed: 2 concerns raised. The split-brain with the inlined process.env.NODE_ENV is pre-existing and tracked by #32851, so this change documents it instead of widening scope. The overlap with #38672 is a small merge in one function.


no test proof · iteration 5 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/bundler/bundler_compile.test.ts

@coderabbitai

coderabbitai Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

  • Run on-demand review

On-demand reviews are free for the next 14 days. After that, they cost $0.25 per reviewed file.

Or wait 8 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Essentials

Run ID: 197c007e-ba8d-40e8-909c-aab8907d6184

📥 Commits

Reviewing files that changed from the base of the PR and between d316760 and ae117b5.

📒 Files selected for processing (7)
  • docs/bundler/executables.mdx
  • packages/bun-types/serve.d.ts
  • src/bun.js.rs
  • src/bundler/options.rs
  • test/bundler/bundler_compile.test.ts
  • test/bundler/bundler_compile_autoload.test.ts
  • test/bundler/expectBundled.ts

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the claude label Sep 6, 2026
@robobun

robobun commented Sep 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: reproduced on 1.4.3-canary (compiled exe serves the dev error page by default, NODE_ENV=production ./app does not). Fix is in apply_standalone_runtime_flags. Tests: compile/ServeDefaultsToProduction, compile/ServeExplicitDevelopment, compile/AutoloadDotenvProductionSuffix.

CI: the new tests and every compile lane pass on each run. The remaining red is unrelated to this diff and changes from run to run (test-crypto-dh-leak.js on x64-asan, which also fails on main, and retried flakes on Windows and Alpine lanes). Ready for review.

@robobun

robobun commented Sep 6, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 6:27 AM PT - Sep 6th, 2026

❌ @robobun, your commit ae117b5 has 18 failures in Build #111139 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 41506

That installs a local version of the PR into your bun-41506 executable, so you can run:

bun-41506 --bun

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread docs/bundler/executables.mdx Outdated
Comment thread test/bundler/bundler_compile.test.ts
Comment thread src/bun.js.rs Outdated

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread test/bundler/bundler_compile.test.ts

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

A bun build --compile executable never set the production flag on the
transpiler, so Bun.serve defaulted to development mode and served the dev
error page (message, stack, source) for an uncaught route error, and the
dotenv loader picked .env.development. Set production in
apply_standalone_runtime_flags unless NODE_ENV or BUN_ENV is development
or test. configure_defines still honours an explicit NODE_ENV afterwards,
and development: true in Bun.serve still wins.
@robobun
robobun force-pushed the robobun/47c1050e/compile-serve-production-default branch from fbf7137 to ae117b5 Compare September 6, 2026 10:43

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants