Skip to content

fix(build): do not assign rollup lib chunks to plugin-only loaded modules - #5104

Merged
pi0 merged 1 commit into
mainfrom
fix/rollup-orphan-lib-chunks
Nov 10, 2026
Merged

pi0 merged 1 commit into
mainfrom
fix/rollup-orphan-lib-chunks

Conversation

@pi0x

@pi0x pi0x commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #4471

With Rollup (rollup builder, or Vite 7), nitro:externals calls this.resolve(), and the commonjs plugin's resolveId then this.load()s the target to check if it is CJS. When nitro externalizes that import, the loaded module stays in Rollup's graph with no importers and never gets its imports linked.

Rollup calls manualChunks for every module in the graph, so our _libs/<pkg> assignment put these orphans in a chunk, and rendering one that has both a direct export and a re-export crashed with Cannot read properties of null (reading 'getVariableForExportName'). #4471 hits this with @sentry/tanstackstart-react, which is externalized since d7a3447 added the import condition.

The fix: a shared libManualChunks (rollup builder and the Vite 7 path) skips modules that are not entries and have no importers, dynamic importers or implicitlyLoadedAfterOneOf. Orphans are never included in output, so no code is dropped. The only output change is that an empty, unimported _libs/<pkg>.mjs that such apps used to get is no longer emitted.

Testing:

  • New unit test in test/unit/chunks.test.ts reproduces the crash with plain Rollup (fails without the fix).
  • The issue's repro (Vite 7.3.6 + TanStack Start + Sentry plugin) builds and serves 200. Sentry is externalized and traced to .output/server/node_modules/@sentry, same as on Vite 8.
  • NITRO_BUILDER=rollup test suite passes.

Notes:

  • Packages like @sentry/tanstackstart-react now end up externalized and traced on Vite 7 (previously a crash; before d7a3447 they were bundled). This matches what Vite 8 already produces. Sentry instrumentation at runtime was not verified, since the repro never calls Sentry.init.
  • The orphan is still loaded by the commonjs probe; this only stops assigning it to a chunk. The underlying behavior (Rollup's assignManualChunks passing modules that are never included) could be reported to Rollup separately.
  • A user rollupConfig.output.manualChunks overrides ours in the rollup builder, so such configs are not covered. On the Vite 7 path ours still takes precedence, as before.
  • Rolldown and rspack paths are unchanged.

🤖 Generated with AI assistant

…ules

Modules loaded via `this.load()` (e.g. commonjs probing an import that
`nitro:externals` then externalizes) stay in Rollup's graph unlinked;
assigning them to a `_libs` chunk crashed rendering (#4471).
@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Nov 10, 2026 6:45pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 60fbf39

@pi0
pi0 marked this pull request as ready for review November 10, 2026 18:55
@pi0
pi0 self-requested a review as a code owner November 10, 2026 18:55
@pi0
pi0 merged commit b153030 into main Nov 10, 2026
16 of 17 checks passed
@pi0
pi0 deleted the fix/rollup-orphan-lib-chunks branch November 10, 2026 18:57

This branch was successfully deployed

1 active deployment
Preview — 60fbf395 Deployed Nov 10, 2026 by vercel[bot]
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: nitro env build crashes with getVariableForExportName when @sentry/vite-plugin is present (regression after 2026-05-07 nightly)

2 participants