Skip to content

Runtime helper circular dependency when __exportAll is in entry chunk during code-splitting #8809

@Afee2019

Description

@Afee2019

Bug Description

When code-splitting, rolldown may place the runtime module (containing __exportAll, __commonJSMin, etc.) inside the entry chunk. If facade elimination later adds __exportAll dependency to a common chunk, that common chunk imports the helper from the entry chunk. When the entry chunk also imports from the common chunk (e.g., through static import chains), a circular dependency is created, causing the helper to be undefined at ESM evaluation time.

Reproduction

Project structure:

entry.js  →  gateway.js  →  daemon.js (barrel: export * from './impl.js')
                              ↑
completion.js  ──── dynamic import('./daemon.js')

Where entry.js also uses a CJS dependency (triggering __commonJSMin runtime helper, placing the runtime in the entry chunk).

Expected: __exportAll is imported from a standalone rolldown-runtime chunk or the chunk that defines it.
Actual: __exportAll is imported from index.js (entry chunk), creating a circular dependency:

daemon-cli-XXXX.js  →  import { __exportAll } from "./index.js"
index.js            →  import { ... } from "./daemon-cli-XXXX.js"

At runtime: TypeError: __exportAll is not a function

Root Cause

In chunk_optimizer.rs, optimize_facade_entry_chunks:

  1. The runtime module is assigned to the entry chunk during split_chunks (because only the entry's CJS path references runtime helpers)
  2. Facade elimination for dynamic entries adds __exportAll to common chunks
  3. The circular dependency check at line ~736 may be bypassed when to_temp_idx() returns None for newly created common chunks
  4. The existing code at line ~1005 only handles the case where the runtime is NOT yet assigned (module_to_chunk[runtime_module_idx].is_none()), missing the case where it's already in the entry chunk

Impact

  • rc.3 → rc.10 fixed most instances (12 → 1 in our project), but edge cases remain
  • The remaining case doesn't crash on startup but causes undefined errors when the affected dynamic import path is executed

Related Issues

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Priority

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions