Skip to content

[v8] CJS interop factory shadowed when re-bundling rolldown-emitted CJS output #22176

@williamlark

Description

@williamlark

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 — TypeError

Reproduction

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

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions