Skip to content

fix: initialize re-exported wrapped ESM modules at the entry chunk - #10567

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/entry-reexport-wrapped-esm-init
Aug 3, 2026
Merged

graphite-app[bot] merged 1 commit into
mainfrom
fix/entry-reexport-wrapped-esm-init

Conversation

@hyfdev

@hyfdev hyfdev commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Fixes #10543.

The issue is that after wrapping, rolldown should output the entry chunk with a init_xxx call to ensure the execution of the wrapped module.

// @mui_material_styles.js — before
import { …, v as StyledEngineProvider } from "./styles-BcPZjlv2.js";
export { StyledEngineProvider, … };   // nothing ever runs the module body that assigns `v`

// after
import { …, v as StyledEngineProvider, y as init_StyledEngineProvider } from "./styles-6TFHfymC.js";
init_StyledEngineProvider();          // evaluates the wrapped module first
export { StyledEngineProvider, … };

--edited

While this PR does fix the issue, I feel that the complexity around "wrappers" are increases significantly, we need to find a way to abstract them.

@netlify

netlify Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

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

@hyfdev
hyfdev marked this pull request as ready for review July 31, 2026 13:17
@hyfdev
hyfdev requested a review from IWANABETHATGUY as a code owner July 31, 2026 13:17
Copilot AI review requested due to automatic review settings July 31, 2026 13:17
@hyfdev
hyfdev requested a review from shulaoda as a code owner July 31, 2026 13:17

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.

@shulaoda

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 81e5ab2ae9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@codspeed

codspeed Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing fix/entry-reexport-wrapped-esm-init (36fd548) with main (347cb3b)

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. ↩

@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 writeup, and the "no new chunk-graph edges" argument holds up — I traced the enumeration against collect_depended_symbols (compute_cross_chunk_links.rs:636-651) and the non-strict arm of add_depended_symbol_with_wrapped_esm_init (:758-769), and every call this renders is backed by an import that already exists on main. Also confirmed the fixture red→green locally (AssertionError: undefined == 'v18' without the change), and the full integration suite comes back with zero snapshot churn beyond the three you committed.

One thing I'd want before merge: internal-docs/code-splitting/design.md needs updating in this same change. The "Trigger placement" table opens with "Every site that can run a wrapped module, in one place", and this adds one that isn't listed — entry-chunk tail, off-strict, for re-exported wrapped modules. A new row would do it.

While you're in there, the rejected alternative from your PR description deserves to land in that doc too. "Let the metadata collector keep cross-chunk targets" is the obvious first thing anyone will try — I'd have guessed the same, on the reasoning that the resulting chunk cycle is harmless because calls only happen inside wrappers post-evaluation. Your finding that off-strict output is full of eager top-level init calls, so the cycle inverts evaluation order and you get init_identifier is not a function, is exactly the kind of thing that's expensive to rediscover. Right now it only exists in a PR description.

On the ordering point Codex raised: I couldn't make it bite. Three attempts — entry body reading the re-exported binding, namespace access, and entry body observing a global the wrapped module sets — all self-heal, because referencing the module (or the module having any side effect at all) re-includes the forwarding chain that carries init_w(). The shape only exists when the wrapped module is side-effect-free and unreferenced by the entry body, and referencing it is precisely what fixes it. Not a regression either way, since main never calls it at all. Might still be worth a sentence in the doc comment on why the tail rather than the prologue — prologue is more ESM-faithful, but runs a cross-chunk wrapper body before this chunk's own module bodies, which is unsafe if the two chunks form a cycle. That's the non-obvious half of the decision and it's currently unrecorded.

hyfdev added a commit that referenced this pull request Jul 31, 2026
Address review on #10567:

- The resolved-exports walk that finds the ESM-wrapped modules behind an
  entry's re-exported bindings now exists once
  (collect_entry_reexported_wrapper_inits); off-strict registration
  consumes it with no name table to import every wrapper, and both
  emission sites consume it with the chunk's assigned names to call
  exactly the reachable ones — the registration-covers-emission
  invariant is structural instead of two hand-synced enumerations.
- Filter the export map before sorting: entries whose exports resolve to
  no wrapped module no longer pay for sorting thousands of names; only
  the qualifying survivors are sorted (name order for deterministic
  dedup, then execution order).
- An in-chunk wrapped entry skips same-chunk owners at the tail: wrap
  propagation wrapped its whole static graph, so those are already
  initialized in-chunk and the calls were pure duplicates (this restores
  the edge_case_issue2793 snapshot to main byte-for-byte). Same-chunk
  owners stay in the prelude and facade paths, where no such guarantee
  exists; the rationale lives on the collector's doc comment.
- Document why the TLA await arm cannot currently fire: non-esm formats
  reject top-level await at scan time, and under esm both wrap causes
  are blocked from combining with TLA (require into a TLA subtree is a
  REQUIRE_TLA build error; the splitting-disabled dynamic-import cause
  is single-chunk, where a statement-position or metadata init always
  runs first). Kept for parity with wrapped_esm_init_call_expr.

hyfdev commented Aug 3, 2026 •

Copy link
Copy Markdown
Member Author

Merge activity

…10567)

Fixes #10543.

The issue is that after wrapping, rolldown should output the entry chunk with a `init_xxx` call to ensure the execution of the wrapped module.

```js
// @mui_material_styles.js — before
import { …, v as StyledEngineProvider } from "./styles-BcPZjlv2.js";
export { StyledEngineProvider, … };   // nothing ever runs the module body that assigns `v`

// after
import { …, v as StyledEngineProvider, y as init_StyledEngineProvider } from "./styles-6TFHfymC.js";
init_StyledEngineProvider();          // evaluates the wrapped module first
export { StyledEngineProvider, … };
```

--edited

While this PR does fix the issue, I feel that the complexity around "wrappers" are increases significantly, we need to find a way to abstract them.
@graphite-app
graphite-app Bot force-pushed the fix/entry-reexport-wrapped-esm-init branch from 36fd548 to 7aa4bf0 Compare August 3, 2026 05:44
@graphite-app
graphite-app Bot merged commit 7aa4bf0 into main Aug 3, 2026
35 checks passed
@graphite-app
graphite-app Bot deleted the fix/entry-reexport-wrapped-esm-init branch August 3, 2026 05:50
@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.

[Bug]: Dep optimizer: wrapped-ESM init_* never emitted when a facade entry only re-exports from a shared chunk (MUI StyledEngineProvider)

4 participants