Skip to content

fix(config): prepend bun export condition under bun runtime#4229

Open
sotasan wants to merge 1 commit intonitrojs:mainfrom
sotasan:fix-vite-bun-export-condition
Open

fix(config): prepend bun export condition under bun runtime#4229
sotasan wants to merge 1 commit intonitrojs:mainfrom
sotasan:fix-vite-bun-export-condition

Conversation

@sotasan
Copy link
Copy Markdown
Contributor

@sotasan sotasan commented Apr 25, 2026

Linked Issue

Closes #4228

Description

_resolveExportConditions only pushed "node" to Vite's resolve conditions, regardless of the actual dev runtime. Under bun --bun vite dev, this caused Vite's ModuleRunner (inside the dev worker) to load srvx/dist/adapters/node.mjs and h3/dist/_entries/node.mjs. The Node adapter's NodeResponse wrapper class is then returned from h3's response pipeline to Bun.serve — which rejects it (error: Expected a Response object, but received 'NodeResponse {...}') and falls back to its default response, breaking every /_nitro/* request with a Parse Error from node:_http_client:230.

Change

Detect Bun via "Bun" in globalThis and prepend "bun" to the conditions list when running under Bun. "node" is still pushed unconditionally, so packages that only declare a "node" export condition still resolve correctly. For packages declaring both (srvx, h3) the order of the resolve conditions causes "bun" to win — picking the Bun adapter as intended.

Pattern matches the existing "Bun" in globalThis check in src/presets/vercel/utils.ts:438.

Verified

  • Patched the bundled dist/_chunks/nitro.mjs in a real Nitro + Vite app — GET /_nitro/tasks, GET/POST /_nitro/tasks/:name, and the main GET / route all return 200 under bun --bun vite dev. Scheduled tasks continue to fire.
  • pnpm lint passes.
  • pnpm typecheck passes for the modified file (other unrelated type errors are pre-existing on main).
  • pnpm build produces a clean bundle with the expected logic.
  • Pure Node setups: behavior is identical to before (the "Bun" in globalThis check is false, so the only conditions pushed are the same as main).

@sotasan sotasan requested a review from pi0 as a code owner April 25, 2026 13:11
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 25, 2026

@sotasan is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ff7ac03c-8b24-4e20-9b2c-a8cb414a1efc

📥 Commits

Reviewing files that changed from the base of the PR and between 7824ce1 and 1e0c12b.

📒 Files selected for processing (1)
  • src/config/resolvers/export-conditions.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/config/resolvers/export-conditions.ts

📝 Walkthrough

Walkthrough

The export-condition resolver now adds the "bun" condition when opts.node is true and globalThis.Bun is present; otherwise it continues to include "node". Existing deduplication and negation filtering for export conditions are unchanged.

Changes

Cohort / File(s) Summary
Bun/Node Runtime Detection
src/config/resolvers/export-conditions.ts
When opts.node is enabled, the resolver conditionally includes "bun" if globalThis.Bun exists; otherwise it adds "node". Existing deduplication and negation filtering remain unmodified.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(config): prepend bun export condition under bun runtime' follows conventional commits format with 'fix' type and descriptive scope/subject.
Description check ✅ Passed The description is directly related to the changeset, explaining the root cause, the fix, and verification steps for adding Bun export condition detection.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

When running the dev server under `bun --bun`, `_resolveExportConditions`
only pushed "node" to Vite's resolve conditions. This caused Vite's
ModuleRunner to load srvx's and h3's Node adapters inside the dev worker,
returning a srvx `NodeResponse` to `Bun.serve` — which Bun rejects, falling
back to its default response and breaking every `/_nitro/*` request with a
Parse Error.

Detect Bun via `"Bun" in globalThis` (matching the existing pattern in
`src/presets/vercel/utils.ts`) and prepend "bun" so it wins for packages
declaring both conditions, while keeping "node" for packages that only
declare "node". Pure Node setups are unchanged.

Closes nitrojs#4228
@sotasan sotasan force-pushed the fix-vite-bun-export-condition branch from 7824ce1 to 1e0c12b Compare April 25, 2026 13:14
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/config/resolvers/export-conditions.ts (1)

23-29: Consider preserving "node" alongside "bun" when Bun is detected.

Bun's module resolver applies both "bun" and "node" conditions by default for Node.js compatibility. Dropping "node" here breaks resolution for dependencies that only ship a "node" export without a "bun" entry. The dedup Set at line 33 prevents duplicates, and the vercel preset's guard at line 44 prevents double-pushes, so this is safe.

♻️ Proposed adjustment
  if (opts.node) {
    if ("Bun" in globalThis) {
-     conditions.push("bun");
+     conditions.push("bun", "node");
    } else {
      conditions.push("node");
    }
  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/config/resolvers/export-conditions.ts` around lines 23 - 29, When
opts.node is true and the runtime is Bun (detected via "Bun" in globalThis),
preserve both "bun" and "node" in the conditions array instead of replacing
"node" with "bun"; update the branch in export-conditions.ts that currently does
if (opts.node) { if ("Bun" in globalThis) { conditions.push("bun"); } else {
conditions.push("node"); } } to push both conditions when Bun is present
(conditions.push("bun"); conditions.push("node");) so dependencies that only
export "node" still resolve; keep the existing dedup Set and the vercel preset
guard behavior unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/config/resolvers/export-conditions.ts`:
- Around line 23-29: When opts.node is true and the runtime is Bun (detected via
"Bun" in globalThis), preserve both "bun" and "node" in the conditions array
instead of replacing "node" with "bun"; update the branch in
export-conditions.ts that currently does if (opts.node) { if ("Bun" in
globalThis) { conditions.push("bun"); } else { conditions.push("node"); } } to
push both conditions when Bun is present (conditions.push("bun");
conditions.push("node");) so dependencies that only export "node" still resolve;
keep the existing dedup Set and the vercel preset guard behavior unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1c1598cd-c7f5-49be-98ab-ccae8a521e42

📥 Commits

Reviewing files that changed from the base of the PR and between feebdc1 and 7824ce1.

📒 Files selected for processing (1)
  • src/config/resolvers/export-conditions.ts

@sotasan sotasan changed the title fix(config): use bun export condition under bun runtime fix(config): prepend bun export condition under bun runtime Apr 25, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 25, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4229

commit: 1e0c12b

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.

vite: dev server breaks under bun --bun runtime due to "node" export condition

1 participant