Skip to content

fix(bundler): inline Bun.env the same as process.env with --env#28560

Open
SferaDev wants to merge 1 commit intooven-sh:mainfrom
SferaDev:claude/fix-bun-env-inline
Open

fix(bundler): inline Bun.env the same as process.env with --env#28560
SferaDev wants to merge 1 commit intooven-sh:mainfrom
SferaDev:claude/fix-bun-env-inline

Conversation

@SferaDev
Copy link

@SferaDev SferaDev commented Mar 25, 2026

Fixes #20430

bun build --env=inline and bun build --env='PREFIX_*' inline process.env.<KEY> but leave Bun.env.<KEY> untouched. Since Bun.env is equivalent to process.env, both should be inlined.

The bundler's define system only registered process.env.<KEY> entries. This PR adds Bun.env.<KEY> defines alongside them in:

  • env_loader.zigcopyForDefine, both prefix and load_all paths. Uses inline for over both prefixes to avoid duplication. Also tightened the e_strings allocation to the exact worst-case bound.
  • options.zig — default NODE_ENV / BUN_ENV defines.
  • HTMLBundle.zig — production NODE_ENV define in the Bake path.

Example

test.js

console.log(Bun.env.FOO)

Before:

$ FOO=hello bun build --env=inline test.js
console.log(Bun.env.FOO);

After:

$ FOO=hello bun build --env=inline test.js
console.log("hello");

Fixes oven-sh#20430

The bundler's define system only registered `process.env.<KEY>` when
inlining environment variables via `--env=inline` or `--env='PREFIX_*'`.
`Bun.env.<KEY>` member expressions were left untouched, even though
`Bun.env` is documented as equivalent to `process.env`.

Register `Bun.env.<KEY>` defines alongside `process.env.<KEY>` in:
- `env_loader.zig` `copyForDefine` (both prefix and load_all paths)
- `options.zig` default NODE_ENV/BUN_ENV defines
- `HTMLBundle.zig` production NODE_ENV define
Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 25, 2026

Walkthrough

The PR extends environment variable inlining support to Bun.env alongside process.env during the build process. Changes enable Bun.env variants for both hardcoded (NODE_ENV, BUN_ENV) and custom environment variables to be inlined using dotenv inline mode and pattern matching, matching existing process.env behavior.

Changes

Cohort / File(s) Summary
Build defines configuration
src/bun.js/api/server/HTMLBundle.zig, src/options.zig
Extended production and development define setup to include both process.env and Bun.env variants for NODE_ENV and BUN_ENV definitions.
Environment loader
src/env_loader.zig
Modified Loader.copyForDefine to generate environment-backed string literals for both process.env.<KEY> and Bun.env.<KEY> prefixes, with corresponding allocation sizing updates for buffer and scratch space.
Bundler tests
test/bundler/bundler_env.test.ts
Added test cases validating Bun.env substitution behavior with dotenv inline mode and environment pattern matching (PUBLIC_*).
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: ensuring Bun.env is inlined alongside process.env when using the --env flag in the bundler.
Linked Issues check ✅ Passed The PR fully addresses issue #20430 by adding Bun.env inlining support alongside process.env across env_loader.zig, options.zig, and HTMLBundle.zig, with comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing Bun.env inlining behavior in the bundler across configuration, environment loading, and test files.
Description check ✅ Passed PR description clearly explains the issue, changes made across three files, and provides concrete before/after examples.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bun build env=inline or env='PUBLIC_*' not working with Bun.env

1 participant