Skip to content

[Bug]: output.banner with a shebang can produce an empty bundle with entry file already has a shebang #9024

@elecmonkey

Description

@elecmonkey

Reproduction link or steps

elecmonkey/rolldown-banner-shebang-reproduction

What is expected?

Rolldown may should just throw an error, or preserve the full bundle body and produce invalid output, similar to postBanner. When using postBanner, the output becomes:

#!/usr/bin/env node
#!/usr/bin/env node
//#region cli.js
console.log("hello from cli");
//#endregion

The second line is invalid syntax, and the CLI emits a warning: [DUPLICATE_SHEBANG] Warning: Both the code and postBanner contain shebang in "with-banner.js". This will cause a syntax error.

Rolldown could emit a warning for the duplicate shebang situation, but I think it should not generate an empty bundle containing only a shebang line.

What is actually happening?

Rolldown first generates output containing two shebang lines, then during a later processing step the bundle body is lost. The final emitted file collapses to only:

#!/usr/bin/env node

with the rest of the generated bundle missing.

Based on instrumenting the current rolldown codebase, the generated chunk is still intact immediately after render, but it already contains two shebang lines. The bundle body disappears later during minify_chunks, which calls EcmaCompiler::dce_or_minify. That path reparses the already-generated chunk using Parser::new(...).parse().program without checking parse errors first. Because duplicate shebangs produce invalid JavaScript, the reparsing step appears to recover into a broken AST, and the subsequent codegen emits only the first shebang line. So the problem seems to be a combination of two issues:

  1. output.banner can introduce a duplicate shebang when the source already has one.
  2. The later minify/DCE path reparses that invalid output without handling parse errors safely.

System Info

System:
    OS: macOS 15.7.3
    CPU: (8) arm64 Apple M2
    Memory: 153.69 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.4.1 - /Users/elecmonkey/.vite-plus/bin/node
    Yarn: 1.22.22 - /Users/elecmonkey/.nvm/versions/node/v24.4.1/bin/yarn
    npm: 11.4.2 - /Users/elecmonkey/.vite-plus/bin/npm
    pnpm: 10.33.0 - /Users/elecmonkey/.nvm/versions/node/v24.4.1/bin/pnpm
    bun: 1.3.3 - /Users/elecmonkey/.bun/bin/bun
    Deno: 2.6.5 - /opt/homebrew/bin/deno
  Browsers:
    Chrome: 146.0.7680.178
    Edge: 146.0.3856.97
    Firefox: 136.0.4
    Safari: 26.2
  npmPackages:
    rolldown: 1.0.0-rc.13 => 1.0.0-rc.13

Any additional comments?

No response

Metadata

Metadata

Labels

No labels
No labels

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions