-
-
Notifications
You must be signed in to change notification settings - Fork 8k
[v8] CJS interop factory shadowed when re-bundling rolldown-emitted CJS output #22176
Copy link
Copy link
Open
Open
Copy link
Labels
Description
Describe the bug
vite build silently generates broken code when bundling a CJS package whose files were themselves previously emitted by rolldown using "unbundle" mode.
The root cause is a naming collision: rolldown uses require_<filestem> as the variable name for its __commonJS factory wrappers. When a source file already contains a local variable with the same name (because it was previously emitted by rolldown), rolldown skips creating the outer factory, yet still replaces require("./file.cjs") with require_<X>() inside the wrapper body. The result is self-referencing code that crashes at runtime:
var require_greet = require_greet(); // require_greet is never defined — TypeErrorReproduction
https://github.com/williamlark/vite-rolldown-cjs-interop-collision
Steps to reproduce
Load the reproduction repo and pnpm install && pnpm build && pnpm preview
System Info
System:
OS: macOS 26.3.1
CPU: (12) arm64 Apple M3 Pro
Memory: 88.06 MB / 18.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.22.2 - /[redacted]/.nvm/versions/node/v22.22.2/bin/node
npm: 10.9.7 - /[redacted]/.nvm/versions/node/v22.22.2/bin/npm
pnpm: 10.13.1 - /[redacted]/pnpm/pnpm
Browsers:
Chrome: 146.0.7680.178
Edge: 146.0.3856.97
Firefox: 149.0
Safari: 26.3.1
npmPackages:
vite: ^8.0.5 => 8.0.5Used Package Manager
pnpm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Reactions are currently unavailable