fix(vite): support Bun-only dev server#4310
Conversation
|
Someone is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThis PR centralizes dev runner selection to detect Bun at runtime and adds Bun response normalization to the async dev worker. New helpers detect Bun globals and default to "bun-process" when present. Build and dev-server code refactored to use these helpers. Dev worker fetch is now async with exponential backoff environment polling and removes content-length headers for Bun Response objects. ChangesBun Dev Runner Detection and Response Normalization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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/build/vite/env.ts`:
- Around line 255-259: There are two identical implementations of
getDefaultDevRunnerName() causing duplication; extract this helper into a single
internal utility (e.g., create a new module like
internal/getDefaultDevRunnerName or utils/devRunner) that exports
getDefaultDevRunnerName, update both locations that currently define the
function (the Vite env module and the dev server module) to import and use the
shared function, and remove the local definitions so both modules call the same
exported getDefaultDevRunnerName; keep the implementation exactly as-is
(checking globalThis.Bun) and ensure the import is typed as RunnerName where
used.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 46e4149e-d443-4a57-b7c5-dc0e698a4d43
📒 Files selected for processing (4)
src/build/vite/env.tssrc/dev/server.tssrc/runtime/internal/vite/dev-worker.mjstest/unit/vite-dev-worker.test.ts
| function getDefaultDevRunnerName(): RunnerName { | ||
| return typeof (globalThis as typeof globalThis & { Bun?: unknown }).Bun === "undefined" | ||
| ? "node-worker" | ||
| : "bun-process"; | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Check env-runner for valid runner names and bun-process support
# Search for RunnerName type definition or valid runner strings
rg -n -C5 'bun-process|RunnerName|type.*Runner.*=' --type=ts node_modules/env-runner/
# Check for bun-process runner implementation
fd -e ts -e js bun node_modules/env-runner/ --exec cat {}Repository: nitrojs/nitro
Length of output: 234
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Repo-wide search for bun-process / RunnerName / env-runner
rg -n --hidden --no-ignore-vcs "bun-process" . || true
rg -n --hidden --no-ignore-vcs "RunnerName" . || true
rg -n --hidden --no-ignore-vcs "env-runner" . || true
# Identify env-runner version from package manifests/lockfiles
for f in package.json pnpm-lock.yaml package-lock.json yarn.lock; do
if [ -f "$f" ]; then
echo "---- $f ----"
rg -n "env-runner" "$f" || true
fi
doneRepository: nitrojs/nitro
Length of output: 4259
🌐 Web query:
unjs env-runner 0.1.9 RunnerName bun-process
💡 Result:
The BunProcessEnvRunner is a specific runner class provided by the unjs/env-runner library designed to execute server applications using the Bun runtime or a Node.js process [1][2]. Key details regarding the runner: Isolation: It operates by spawning a child process using Bun or Node.js [1][2]. IPC Mechanism: It utilizes Bun's spawn IPC or Node.js's fork mechanism for inter-process communication [1][2]. Usage: It is one of the available runners supported by the env-runner package, which can be selected via the --runner bun-process CLI flag or imported directly from env-runner/runners/bun-process [2]. The env-runner library is a generic environment runner that supports various runtimes and deployment contexts, including Node.js (worker threads and child processes), Bun, Deno, Cloudflare Workers (via miniflare), Vercel, and Netlify [1][3]. As of June 2026, the project continues to receive updates to its runner implementations [3][4].
Citations:
- 1: https://github.com/unjs/env-runner/blob/main/README.md
- 2: https://npmx.dev/package/env-runner
- 3: unjs/env-runner@7e47106
- 4: feat(vercel): add vercel runner unjs/env-runner#3
🌐 Web query:
env-runner@0.1.9 RunnerName "bun-process" type
💡 Result:
In the env-runner package, "bun-process" is a valid member of the RunnerName type [1][2]. It refers to a runner that executes applications in a separate process using the Bun runtime, utilizing Bun.spawn for IPC (Inter-Process Communication) or Node.js-style fork mechanisms [3][4][5]. The RunnerName type is a union of strings used to specify which environment runner should be utilized [1][2]. As of the current version, the available runners include: - node-worker [4][5] - node-process [4][5] - bun-process [4][5] - deno-process [4][5] - self [4][5] - miniflare [4][5] - vercel [1] - netlify [1] Users can select a runner via the --runner flag in the CLI or through configuration options in tools like Nitro [4][5][2]. The "bun-process" runner specifically provides isolation by running the target application as a child process with the Bun runtime [3][4].
Citations:
- 1: unjs/env-runner@7e47106
- 2: feat: use unjs/env-runner #4088
- 3: https://npmx.dev/package/env-runner
- 4: https://github.com/unjs/env-runner/blob/main/README.md
- 5: https://github.com/unjs/env-runner
Remove duplicated getDefaultDevRunnerName() helper to prevent drift.
getDefaultDevRunnerName() is defined identically in both src/build/vite/env.ts (lines 255-259) and src/dev/server.ts (lines 254-258). Extract it into a shared internal utility and import it in both places to avoid divergence.
"bun-process" is a valid RunnerName in the repo’s env-runner@^0.1.9 dependency, so no change is needed there.
🤖 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/build/vite/env.ts` around lines 255 - 259, There are two identical
implementations of getDefaultDevRunnerName() causing duplication; extract this
helper into a single internal utility (e.g., create a new module like
internal/getDefaultDevRunnerName or utils/devRunner) that exports
getDefaultDevRunnerName, update both locations that currently define the
function (the Vite env module and the dev server module) to import and use the
shared function, and remove the local definitions so both modules call the same
exported getDefaultDevRunnerName; keep the implementation exactly as-is
(checking globalThis.Bun) and ensure the import is typed as RunnerName where
used.
🔗 Linked issue
Resolves #4182
❓ Type of change
📚 Description
When Nitro dev runs inside Bun, the default
node-workerrunner still requires Node to be available. This selects thebun-processdev runner by default when Nitro itself is running under Bun, while preserving explicitdevServer.runnerandNITRO_DEV_RUNNERoverrides.The dev worker now also normalizes responses before Bun serves them by dropping explicit
content-length, avoiding duplicate header parse errors when Bun sends the response. It waits briefly for Vite to register the environment before handling the first request, which avoids a first-request race in the Bun process runner.Validation:
pnpm vitest run test/unit/vite-dev-worker.test.ts --reporter verbosepnpm fmtpnpm typecheckpnpm build --stubPATH=/tmp/nitro-no-node-bin:/usr/bin:/bin:/usr/sbin:/sbin bun run devinexamples/vite-nitro-plugin, first request returnedHTTP/1.1 200 OKandHello from virtual entry!📝 Checklist