Skip to content

fix(code-splitting): materialize consumer-local barrel routing when the order-wrap plan is empty - #10544

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/strict-order-consumer-local-empty-plan
Jul 31, 2026
Merged

graphite-app[bot] merged 1 commit into
mainfrom
fix/strict-order-consumer-local-empty-plan

Conversation

@hyfdev

@hyfdev hyfdev commented Jul 30, 2026

Copy link
Copy Markdown
Member

With strictExecutionOrder, a CJS multi-entry build whose entries require() ESM forwarders into one shared re-export-only barrel over CJS leaves emits chunks that require() each other in a cycle and crash at startup:

TypeError: require_barrel.init_barrel is not a function

Fixes #10515. Regression from #10488, shipped in v1.2.1.

Problem

Consumer-local re-export routing is computed in three places: the pre-chunk placement probe, the on-demand projector probes, and real lowering. The probes run consumer_local_reexport_plan unconditionally, and its eligibility never reads the order-wrap plan — but real lowering sits behind the plan.is_empty() short-circuit in apply_order_wraps.

When every reachable ESM module is already interop-wrapped (the CJS entries require() the forwarders), the order-wrap plan is empty. Chunk placement has already routed each barrel leaf to only its consuming entry's chunk, while emission keeps the barrel's monolithic interop body referencing the per-entry require_* wrappers — so the emitted chunks require each other in a startup cycle.

The on-demand carrier force-add cannot rescue this: an interop-wrapped barrel never passes is_order_wrap_eligible, so the final plan can stay empty even when the wrap-all plan is not. That also rules out fixing this on the probe side — the probe runs before chunking, while the on-demand plan is only computed from the chunk graph afterwards.

Fix

Compute the consumer-local plan before the short-circuit and take the fast path only when both the order-wrap plan and the consumer-local plan are empty. Real lowering then materializes exactly the routes placement consumed, in both strict modes, and lower_order_state receives the precomputed plan instead of recomputing it.

One exception keeps an existing contract: with code splitting disabled and at most one entry, placement has a single reachability bit, so the probe's routes moved nothing, and an inert on-demand plan keeps its promise of flag-off-equivalent output (pinned by cjs_reexport_barrel_code_splitting_disabled from #10514). The disabled option alone is not a sufficient key — only dynamic entries are dropped in disabled mode, so a plugin-emitted chunk still adds a second entry whose per-entry placement consumes the probe's routes — hence the exception is keyed on entry count as well.

Test

  • cjs_reexport_barrel_probe_plan_divergence: the seven-file reproduction from strictExecutionOrder: pre-chunk consumer-local barrel routing without matching lowering emits a startup-crashing CJS chunk cycle #10515, wrap-all + on-demand variants. Crashes on current main, passes with the fix; the barrel becomes an empty routing waypoint and each entry initializes only its own leaf through a per-record memoized carrier.
  • cjs_reexport_barrel_on_demand_inert_plan: adds a plain-ESM third entry so the wrap-all plan is non-empty while the on-demand plan stays empty — the shape a probe-side gate could not fix. On current main the wrap-all variant passes and the on-demand variant crashes; both pass with the fix.
  • No existing snapshot changes — every previously covered configuration emits byte-identical output.

@netlify

netlify Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 7221254
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a6c731be3887a00088e2e9a

@hyfdev
hyfdev marked this pull request as ready for review July 30, 2026 13:22
Copilot AI review requested due to automatic review settings July 30, 2026 13:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codspeed

codspeed Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing fix/strict-order-consumer-local-empty-plan (161ed54) with main (78d6f14)

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@hyfdev
hyfdev force-pushed the fix/strict-order-consumer-local-empty-plan branch from 0366bf6 to ee2f15c Compare July 30, 2026 16:14

@IWANABETHATGUY IWANABETHATGUY left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice write-up in the description — I traced the three-way split (probe / projector / real
lowering) and the fix reads right to me. Couple of things I checked independently and they
hold up: consumer_local_reexport_plan never reads input.plan, so the probe and lowering
really do reach the same decision, and hoisting the call above the wrapper loop in
lower_order_state is behaviorally a no-op since that loop only touches
insert_order_wrapper / set_reexport_init_transparent. I also convinced myself the
exception is earning its keep: in the on-demand variant of
cjs_reexport_barrel_code_splitting_disabled the barrel is WrapKind::Esm, so the plan is
empty but it still owns a carrier — pull the guard and that snapshot flips.

None of the below blocks merging as far as I'm concerned — the fix is right and this is a
shipped crash, so landing it promptly is worth something on its own. A few things I'd like to
see follow, the first one ideally as a commit on this branch:

  • The entries.len() <= 1 half of the guard isn't covered by anything. The description
    makes a good case that it's load-bearing (emitted chunk → second entry → per-entry placement
    consumes the routes), but all five strict-order fixtures with codeSplitting: false are
    single-entry, so deleting that clause passes CI while quietly restoring the #10515 cycle for
    multi-entry non-splitting builds. That's exactly the kind of clause someone simplifies away
    in six months. Cheapest fix I can see is a codeSplitting: false config variant on
    cjs_reexport_barrel_probe_plan_divergence — by your own reasoning that build still has to
    route, so it should fail today with the clause removed and pass with it.

  • The new _test.mjs files only assert values. The thing that actually makes this fix
    interesting — entry-a initializes a.cjs and never touches b.cjs — lives only in the
    snapshot right now, so a future snapshot regression sails past the test. The sibling fixture
    you're citing asserts both the __events ordering and the structural difference between
    variants (assert.doesNotMatch(main, /init_pure_barrel_cjs/)); pushing to
    globalThis.__events from the two leaves and asserting per-entry lists would pin the real
    guarantee and match what the rest of the suite does.

  • The empty-plan path isn't cheap anymore. synchronous_cycle_modules (tarjan over the
    whole module graph) plus the consumer_local_reexport_plan fixpoint now run on a path that
    used to return immediately, and in on-demand mode an empty plan is the common healthy case.
    The exception branch is the annoying one: it builds the SCC and the whole
    OrderLoweringInput and then throws both away. An early return keeps that case exactly as
    cheap as it was:

    if plan.is_empty() && code_splitting_disabled && self.link_output.entries.len() <= 1 {
      // …existing fast path…
    }
    // only now build cyclic_modules + input and compute the plan
    

    (CodSpeed being flat doesn't say much here — none of the benchmarks turn on
    strictExecutionOrder.)

  • Follow-up, not for this PR: the "all three phases must agree" invariant is currently held
    up by a doc comment. The plan is pure over ModuleIdx/SymbolRef values that survive
    chunking, so pre_chunk_order_state could compute it once and hand it to both the projector
    and apply_order_wraps — that makes the invariant structural instead of documented and drops
    two redundant fixpoints and two redundant SCC passes. Worth an issue; this is the second bug
    that's come out of the three copies drifting.

  • Also follow-up: in both new snapshots barrel.js is an entire emitted chunk holding
    var init_barrel = __esmMin(() => {}) and its defineProperty export, and each entry
    carries a bare require("./barrel.js") that does nothing. Correct, just an empty file plus a
    startup require per entry. Collapsing a barrel that routing has emptied out seems worth
    tracking separately.

Nits, take or leave:

  • ConsumerLocalReexportPlan::is_empty — carriers only get pushed alongside their owner
    module, so carriers.is_empty() is already implied by modules.is_empty(). Harmless, but
    the && hints at a state that can't happen.
  • self.link_output.entries.len() counts distinct entry modules, while generate_chunks
    deliberately uses the flattened entry-point count and has a comment saying so. Unique
    modules is the right quantity here (duplicate entry points on one module produce identical
    bit sets), but a half-line note would save the next person the round trip I just made.
  • The internal-docs/code-splitting/implementation.md addition tacks another ~700 characters
    onto a paragraph that was already pretty dense. Content's right — maybe break the invariant
    out into its own short paragraph?

@hyfdev

hyfdev commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

Addressed in 73b525e and 60121af, with two deviations worth flagging:

  • Coverage for the entries.len() <= 1 clause — the suggested codeSplitting: false variant on cjs_reexport_barrel_probe_plan_divergence can't be expressed: that fixture has two inputs, and disabled splitting with multiple inputs is rejected at option validation. The reachable shape is the emitted-chunk one, so the coverage landed as disabled_splitting_emitted_entry in the strict invariants suite: one input plus a plugin-emitted second entry over the same seven-file graph, codeSplitting: false, both strict modes, executed with Node. Verified it earns its keep: deleting the entry-count clause makes it crash with the strictExecutionOrder: pre-chunk consumer-local barrel routing without matching lowering emits a startup-crashing CJS chunk cycle #10515 cycle (require_barrel.__commonJSMin is not a function); the build really does split (entry-a.js, chunks/entry-b.js, chunks/barrel.js).

  • Behavioral pins in the new _test.mjs files — tried __events first and it can't pin this graph: observable side effects in the leaves flip their carriers to eager by design, after which every consumer correctly initializes both leaves (the run showed ['a','b'] on entry-a — the spec answer for effectful leaves, and incidentally a live check that the eager-carrier path works under an empty plan). The fixtures now pin the routing structurally instead — each leaf's body may appear only in its consuming entry's chunk, and the barrel chunk may not retain any monolithic leaf init — with a comment explaining why events can't work here. The events approach stays meaningful only with side-effect-free leaves, which the existing sibling fixtures already cover.

  • Fast path restructured as sketched: the single-entry disabled case returns before the SCC scan and the fixpoint; the shared tail is apply_inert_order_wrap_plan. Nits all taken. Agreed on both follow-ups — happy to file the issues; the shared-computation one should also fold in the third copy in analyze_execution_order's placement_state.

@hyfdev
hyfdev force-pushed the fix/strict-order-consumer-local-empty-plan branch from 60121af to 161ed54 Compare July 31, 2026 09:10

hyfdev commented Jul 31, 2026 •

Copy link
Copy Markdown
Member Author

Merge activity

  • Jul 31, 9:10 AM UTC: The merge label 'graphite: merge-when-ready' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Jul 31, 10:02 AM UTC: hyfdev added this pull request to the Graphite merge queue.
  • Jul 31, 10:10 AM UTC: Merged by the Graphite merge queue.

…he order-wrap plan is empty (#10544)

With `strictExecutionOrder`, a CJS multi-entry build whose entries `require()` ESM forwarders into one shared re-export-only barrel over CJS leaves emits chunks that `require()` each other in a cycle and crash at startup:

```
TypeError: require_barrel.init_barrel is not a function
```

Fixes #10515. Regression from #10488, shipped in v1.2.1.

## Problem

Consumer-local re-export routing is computed in three places: the pre-chunk placement probe, the on-demand projector probes, and real lowering. The probes run `consumer_local_reexport_plan` unconditionally, and its eligibility never reads the order-wrap plan — but real lowering sits behind the `plan.is_empty()` short-circuit in `apply_order_wraps`.

When every reachable ESM module is already interop-wrapped (the CJS entries `require()` the forwarders), the order-wrap plan is empty. Chunk placement has already routed each barrel leaf to only its consuming entry's chunk, while emission keeps the barrel's monolithic interop body referencing the per-entry `require_*` wrappers — so the emitted chunks require each other in a startup cycle.

The on-demand carrier force-add cannot rescue this: an interop-wrapped barrel never passes `is_order_wrap_eligible`, so the final plan can stay empty even when the wrap-all plan is not. That also rules out fixing this on the probe side — the probe runs before chunking, while the on-demand plan is only computed from the chunk graph afterwards.

## Fix

Compute the consumer-local plan before the short-circuit and take the fast path only when both the order-wrap plan and the consumer-local plan are empty. Real lowering then materializes exactly the routes placement consumed, in both strict modes, and `lower_order_state` receives the precomputed plan instead of recomputing it.

One exception keeps an existing contract: with code splitting disabled and at most one entry, placement has a single reachability bit, so the probe's routes moved nothing, and an inert on-demand plan keeps its promise of flag-off-equivalent output (pinned by `cjs_reexport_barrel_code_splitting_disabled` from #10514). The disabled option alone is not a sufficient key — only dynamic entries are dropped in disabled mode, so a plugin-emitted chunk still adds a second entry whose per-entry placement consumes the probe's routes — hence the exception is keyed on entry count as well.

## Test

- `cjs_reexport_barrel_probe_plan_divergence`: the seven-file reproduction from #10515, wrap-all + on-demand variants. Crashes on current `main`, passes with the fix; the barrel becomes an empty routing waypoint and each entry initializes only its own leaf through a per-record memoized carrier.
- `cjs_reexport_barrel_on_demand_inert_plan`: adds a plain-ESM third entry so the wrap-all plan is non-empty while the on-demand plan stays empty — the shape a probe-side gate could not fix. On current `main` the wrap-all variant passes and the on-demand variant crashes; both pass with the fix.
- No existing snapshot changes — every previously covered configuration emits byte-identical output.
@graphite-app
graphite-app Bot force-pushed the fix/strict-order-consumer-local-empty-plan branch from 161ed54 to 7221254 Compare July 31, 2026 10:04
@graphite-app
graphite-app Bot merged commit 7221254 into main Jul 31, 2026
35 checks passed
@graphite-app
graphite-app Bot deleted the fix/strict-order-consumer-local-empty-plan branch July 31, 2026 10:10
@rolldown-guard rolldown-guard Bot mentioned this pull request Aug 3, 2026
shulaoda added a commit that referenced this pull request Aug 3, 2026
## [1.2.2] - 2026-08-03

### 🚀 Features

- code-splitting: support inlining of shared deps in dynamic entries (#10526) by @nicolo-ribaudo
- plugin: time the user callbacks configured on the options (#10509) by @IWANABETHATGUY

### 🐛 Bug Fixes

- dev: force `cleanDir` off in dev mode (#10579) by @shulaoda
- dev: keep `?rolldown-lazy` proxy ids out of user `resolveId` hooks (#10580) by @btea
- initialize re-exported wrapped ESM modules at the entry chunk (#10567) by @hyfdev
- link: resolve circular star reexports as null (#10445) by @Nic-Polumeyv
- transform: transformer gate skips newer syntax that oxc can lower (#10564) by @sekyungk
- code-splitting: materialize consumer-local barrel routing when the order-wrap plan is empty (#10544) by @hyfdev
- plugin: measure plugin hooks in JavaScript, report them from Rust (#10508) by @IWANABETHATGUY
- plugin: include external dynamic imports in chunk metadata (#10557) by @hyfdev
- plugin: report plugin timings after closeBundle has run (#10521) by @IWANABETHATGUY
- browser: avoid using node builtin modules (#10542) by @sapphi-red

### 🚜 Refactor

- plugin: give the build's own clocks their own type (#10520) by @shulaoda

### 📚 Documentation

- correct stale `buildStart` comment in `ScanStageCache::merge` (#10577) by @shulaoda
- update outdated pluginutils references (#10574) by @dogledogle

### ⚡ Performance

- external: drop the runtime import from chunks that never wrap (#10517) by @IWANABETHATGUY
- plugin: clock the build only when it is being measured (#10558) by @IWANABETHATGUY

### 🧪 Testing

- vite-tests: drop the dead `@rolldown/pluginutils` pnpm override (#10575) by @IWANABETHATGUY
- transform: group the transform-target fixtures and say why each exists (#10569) by @IWANABETHATGUY
- dev: fix hmr-hot-update-hook-vite fixture on Windows (#10565) by @h-a-n-a
- dev: match Vite's renamed full-reload log (#10547) by @shulaoda

### ⚙️ Miscellaneous Tasks

- deps: update napi (#10576) by @renovate[bot]
- deps: bump `@napi-rs/wasm-runtime` to 1.2.2 (#10570) by @shulaoda
- deps: update npm packages (#10583) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to ^0.28.0 (#10588) by @renovate[bot]
- deps: update taiki-e/install-action action to v2.85.5 (#10587) by @renovate[bot]
- deps: update codspeedhq/action action to v5 (#10584) by @renovate[bot]
- deps: update github actions (#10582) by @renovate[bot]
- deps: update rust crates (#10581) by @renovate[bot]
- deps: update dependency vite-plus to v0.2.7 (#10568) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to v0.27.14 (#10531) by @renovate[bot]
- deps: update pnpm to v11.17.0 (#10571) by @renovate[bot]
- deps: update @napi-rs/wasm-runtime to 1.2.1 (#10545) by @sapphi-red

### ❤️ New Contributors

* @sekyungk made their first contribution in [#10564](#10564)
* @nicolo-ribaudo made their first contribution in [#10526](#10526)

Co-authored-by: shulaoda <[email protected]>
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.

strictExecutionOrder: pre-chunk consumer-local barrel routing without matching lowering emits a startup-crashing CJS chunk cycle

3 participants