Skip to content

fix(bundler): honor env: disable for process.env.NODE_ENV - #35954

Closed
baima365-web wants to merge 1 commit into
oven-sh:mainfrom
baima365-web:fix-env-disable-node-env
Closed

baima365-web wants to merge 1 commit into
oven-sh:mainfrom
baima365-web:fix-env-disable-node-env

Conversation

@baima365-web

Copy link
Copy Markdown

What does this PR do?

Bun.build({ env: "disable" }) was still folding process.env.NODE_ENV (dot form) to the build-time value, while the bracket form process.env["NODE_ENV"] stayed dynamic. The CLI bun build --env disable already left both forms dynamic.

Problem

// e.mjs
console.log(JSON.stringify({ dot: process.env.NODE_ENV, bracket: process.env["NODE_ENV"] }));

// build.mjs
await Bun.build({ entrypoints: ["./e.mjs"], target: "node", env: "disable", outdir: "./dist" });

$ NODE_ENV=production node dist/e.js
{"dot":"development","bracket":"production"}

Fix

Check env_behavior before injecting NODE_ENV define in defines_from_transform_options. When env_behavior is Disable, skip the NODE_ENV and BUN_ENV defines entirely.

Fixes #35952

Bun.build({ env: "disable" }) was still folding process.env.NODE_ENV to
the build-time value. The bracket form process.env["NODE_ENV"] stayed
dynamic but dot form was incorrectly inlined.

Fix: Check env_behavior before injecting NODE_ENV define.

Fixes oven-sh#35952

@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.

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

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The bundler now avoids injecting process.env.NODE_ENV and process.env.BUN_ENV when dotenv behavior is disabled.

Changes

Environment variable inlining

Layer / File(s) Summary
Gate implicit environment defines
src/bundler/options.rs
Adds process.env.NODE_ENV and process.env.BUN_ENV to user_defines only when dotenv behavior is not disabled.

Suggested reviewers: jarred-sumner

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The change addresses env:disable in options.rs, but the linked issue also required JSBundler parsing, falsy values, and tests that aren't shown. Add the JSBundler env parsing fix for disable/falsy values and include API/CLI tests covering both backends.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the bundler env-disable fix.
Description check ✅ Passed The description covers the problem and fix clearly; the verification section is omitted but the rest is sufficient.
Out of Scope Changes check ✅ Passed No unrelated or out-of-scope changes are evident from the summarized diff.

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/bundler/options.rs`:
- Around line 921-925: Update the environment-injection condition in
defines_from_transform_options to use the local behavior value instead of the
undefined self.env_behavior reference, comparing it against
api::DotEnvBehavior::disable. Preserve the existing NODE_ENV and BUN_ENV define
insertion behavior when dotenv handling is enabled.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 563d4b96-fe24-41f2-b576-3b3bb18b7016

📥 Commits

Reviewing files that changed from the base of the PR and between 44f6469 and a7176e5.

📒 Files selected for processing (1)
  • src/bundler/options.rs

Comment thread src/bundler/options.rs
Comment on lines +921 to +925
// Only inject NODE_ENV if env_behavior is not Disable
if self.env_behavior != DotEnv::Behavior::Disable {
user_defines.get_or_put_value(b"process.env.NODE_ENV", quoted_node_env.clone())?;
user_defines.get_or_put_value(b"process.env.BUN_ENV", quoted_node_env)?;
}

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.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu
file="$(pwd)/src/bundler/options.rs"
rg -n 'pub fn defines_from_transform_options|self\.env_behavior|let mut behavior|DotEnvBehavior::disable' "$file"

Repository: oven-sh/bun

Length of output: 487


🏁 Script executed:

#!/bin/bash
set -eu
file="$(pwd)/src/bundler/options.rs"

echo "Function signature and dotenv behavior handling:"
sed -n '815,930p' "$file" | cat -n | sed 's/^/options.rs /'

echo
echo "Top-level module/type aliases around this file:"
rg -n '^pub (use|mod) |type DotEnv =|enum DotEnvBehavior|struct DotEnv|api::' "$file" | sed -n '1,80p'

Repository: oven-sh/bun

Length of output: 10670


Fix the out-of-scope self reference.

defines_from_transform_options is a free function, so self.env_behavior is undefined and prevents compilation. Use the local behavior value and the existing api::DotEnvBehavior::disable variant.

Proposed fix
-        // Only inject NODE_ENV if env_behavior is not Disable
-        if self.env_behavior != DotEnv::Behavior::Disable {
+        // Only inject NODE_ENV and BUN_ENV when dotenv behavior is not Disable
+        if behavior != api::DotEnvBehavior::disable {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Only inject NODE_ENV if env_behavior is not Disable
if self.env_behavior != DotEnv::Behavior::Disable {
user_defines.get_or_put_value(b"process.env.NODE_ENV", quoted_node_env.clone())?;
user_defines.get_or_put_value(b"process.env.BUN_ENV", quoted_node_env)?;
}
// Only inject NODE_ENV and BUN_ENV when dotenv behavior is not Disable
if behavior != api::DotEnvBehavior::disable {
user_defines.get_or_put_value(b"process.env.NODE_ENV", quoted_node_env.clone())?;
user_defines.get_or_put_value(b"process.env.BUN_ENV", quoted_node_env)?;
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/bundler/options.rs` around lines 921 - 925, Update the
environment-injection condition in defines_from_transform_options to use the
local behavior value instead of the undefined self.env_behavior reference,
comparing it against api::DotEnvBehavior::disable. Preserve the existing
NODE_ENV and BUN_ENV define insertion behavior when dotenv handling is enabled.

@robobun

robobun commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR. This is the same bug as #35952 (opened a few hours earlier, with a test for both the JS API and the CLI backend), so I'm closing this one as a duplicate and keeping #35952 as the fix. The underlying reports are #19508, #20183 and #22820.

Two notes on this diff, for reference:

  • defines_from_transform_options is a free function, so self.env_behavior does not resolve, and the enum is DotEnvBehavior rather than DotEnv::Behavior; as pushed it fails to compile (E0424 and E0433 in src/bundler/options.rs:919).
  • DotEnvBehavior::disable is also the value both bun build and Bun.build() use when no env option is given at all, so gating the NODE_ENV define on it would stop inlining process.env.NODE_ENV in every default build (edgecase/NodeEnvDefault* in test/bundler/bundler_edgecase.test.ts covers that). The CLI's --env disable maps to LoadAllWithoutInlining, which is the variant this block already checks; Bun.build: honor env: "disable" for process.env.NODE_ENV #35952 makes the JS API's "disable" map to the same variant.

@robobun robobun closed this Aug 15, 2026
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.

2 participants