SSR build emits two mutually-importing router chunks; module-scope reads across them fail and every route 500s #8195
SSR build emits two mutually-importing router-* chunks; module-scope reads across them fail and every route 500s#
Summary#
In a TanStack Start + Nitro SSR build, rolldown emits two common chunks that import each other:
router-<hash>.mjs imports Sport from router-<hash>2.mjs
router-<hash>2.mjs imports router_exports from router-<hash>.mjs
Whichever chunk evaluates first sees the other's bindings uninitialised. Any module-scope expression that reads an imported binding then throws during module evaluation, which fails the entire server bundle — so every route returns 500, not just the one containing the read.
There is no circular import in the source. I built the module graph for all 531 source modules and it contains zero circular groups. The cycle is introduced by chunking alone.
vite build exits 0. The failure only appears when the built server handles its first
request, so nothing in a typical CI pipeline sees it.
Versions#
| vite | 8.2.1 |
| rolldown | 1.2.4 |
| nitro | 3.0.260610-beta |
| @tanstack/react-start | 1.168.44 |
| @tanstack/react-router | 1.170.27 |
| node | 24.19.0 |
| bun | 1.3.5 |
Also reproduced on nitro-nightly@3.0.1-20260828-140537-5d83475c (2.5 months newer) — unchanged.
Reproduction#
The trigger in our app is incidental but very reliable: set any VITE_* variable that keeps a
previously tree-shaken component alive.
We have two sign-in buttons that read a credential and render null when it is absent:
const clientId = import.meta.env.VITE_APPLE_SERVICES_ID as string | undefined;
if (!clientId) return null;
With the variable unset, the component tree-shakes away entirely. With it set, the component survives, the router chunk grows past rolldown's split threshold, and the split produces the mutually-importing pair above.
# healthy
VITE_API_URL=… bun run build && bun .output/server/index.mjs
curl -s -o /dev/null -w '%{http_code}' localhost:3000/ # 200
# broken — only difference is one extra env var
VITE_API_URL=… VITE_APPLE_SERVICES_ID=io.example.signin bun run build && bun .output/server/index.mjs
curl -s -o /dev/null -w '%{http_code}' localhost:3000/ # 500
VITE_GOOGLE_CLIENT_ID alone reproduces it identically, so it is not specific to one component.
Failure#
The thrown error's stack is unusable — the frame is lost inside the request handler and reads
at values (unknown:1:1) in both minified and unminified builds. Patching Object.values in
the built entry to print a stack gives the real frame:
const __ov = Object.values;
Object.values = function (o) {
if (o == null) console.error(new Error().stack);
return __ov(o);
};
at router-<hash>.mjs:3909 // src/shared/activityInterests.ts
at moduleEvaluation (native)
The site is ordinary code — a module-scope derive from an imported enum:
// src/shared/activityInterests.ts
import { Sport } from './enums.js';
export const ALL_SPORTS: readonly Sport[] = Object.freeze(Object.values(Sport));
Sport is declared at line 339 of the other chunk. ALL_SPORTS is read at line 3909 of this
one, during its own evaluation.
Fixing that single site works — and the failure simply moves to the next module-scope read of a
cross-chunk binding. We found four distinct sites this way (Object.values, Object.entries,
and a [...spread]), all of them normal, correct code. Per-site fixes are not a viable strategy.
Why we could not work around it#
nitro({ rolldownConfig }) does reach the server bundler — output.chunkFileNames renamed
all 219 server chunks. But nothing that controls chunk membership has any effect:
Passed via nitro({ rolldownConfig: { output: … } }) | Effect |
|---|---|
chunkFileNames | applied (219 chunks renamed) |
codeSplitting: { groups: [{ name, test }] } | ignored — even test: /node_modules/ produced no chunk |
codeSplitting: false | ignored — still 148 chunks, still the mutually-importing pair |
advancedChunks: { groups: … } | ignored (deprecated; superseded whenever codeSplitting is set, which is always, since it defaults to true) |
Root-level build.rollupOptions.output.manualChunks applies to the client environment only —
it emitted a shared-*.js into the client output and changed nothing under .output/server.
environments.nitro.build.rollupOptions also had no effect, consistent with
nitro/dist/_build/vite.env.mjs:10, which sets that environment's options wholesale:
build: {
rollupOptions: ctx.bundlerConfig.rollupConfig,
rolldownOptions: ctx.bundlerConfig.rolldownConfig,
…
}
What would help#
Any one of these resolves it for us:
- Don't emit mutually-importing common chunks. A cycle between chunks derived from one acyclic graph seems like it should be preventable at the chunking layer.
- Let
codeSplitting/advancedChunksthrough to the server build, so applications can pin a leaf module set (e.g. shared enums) into its own chunk and break the cycle themselves. - Warn at build time when emitted chunks form a cycle. Even without a fix, that turns a silent, environment-dependent production outage into a build-time message.
Note for others hitting this#
vite build exits 0 and the whole test suite passes. The only reliable signal is booting the
built server and requesting one page. We added exactly that to CI:
bun .output/server/index.mjs &
curl -f localhost:$PORT/ # 500 here means the bundle failed to initialise
One request is enough, because when this happens it takes every route down at once.
schiller-manuel(GitHub profile, opens in a new tab) added labelsinformation needed
Rjpearso95(GitHub profile, opens in a new tab) closed this
Rjpearso95(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
where is the reproducer project? it's required and had you read the issue template instead of dumping your LLM output here, you would have seen that