Skip to content

fix(code-splitting): fold already-loaded side-effectful libraries into eager entries - #10645

Merged
hyfdev merged 13 commits into
rolldown:mainfrom
JoviDeCroock:repro/side-effectful-barrel-not-folded
Aug 17, 2026
Merged

hyfdev merged 13 commits into
rolldown:mainfrom
JoviDeCroock:repro/side-effectful-barrel-not-folded

Conversation

@JoviDeCroock

@JoviDeCroock JoviDeCroock commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Note

This PR's reproduction was written by a human, the fix was written by an LLM and then reviewed by a human.

Fixes #9963

When a side-effectful library is imported by an eager entry and by lazy consumers that are only reachable through that entry, Rolldown no longer extracts it into a separate shared chunk. Instead, it folds the library into the eager entry, where its side effect is guaranteed to run before the lazy consumers load.

I only noticed later on that there was already an issue for this 😅

The fix:

  • Removes phantom dependency cycles caused by re-export records.
  • Computes “already loaded” status from emitted chunk imports.
  • Allows re-export-only barrels to merge without falsely widening a strict entry signature.

The PR purposefully splits between reproduction and fix in case y'all want something different than the reproduction can be immediately useful to y'all.

@JoviDeCroock JoviDeCroock changed the title Repro/side effectful barrel not folded fix(code-splitting): fold already-loaded side-effectful libraries into eager entries Aug 7, 2026
@JoviDeCroock
JoviDeCroock force-pushed the repro/side-effectful-barrel-not-folded branch from c261c90 to f6feed9 Compare August 7, 2026 14:33
@codspeed

codspeed Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing JoviDeCroock:repro/side-effectful-barrel-not-folded (0396a87) with main (b26b65d)

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 self-assigned this Aug 8, 2026
@JoviDeCroock
JoviDeCroock force-pushed the repro/side-effectful-barrel-not-folded branch 2 times, most recently from aac81c3 to 036a4ba Compare August 11, 2026 05:05
@hyfdev
hyfdev force-pushed the repro/side-effectful-barrel-not-folded branch from 9dd33b6 to a2603b4 Compare August 16, 2026 18:09
@netlify

netlify Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs ready!

Name Link
🔨 Latest commit 0396a87
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a82f2ff02c72a00082fa24d
😎 Deploy Preview https://deploy-preview-10645--rolldown-rs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@hyfdev

hyfdev commented Aug 16, 2026

Copy link
Copy Markdown
Member

I gave a rough look. There're some code complexity issues due to history code architecture, but the feature of this PR itself is positive. Will merge this before this week's release. I'll take it from here and push some fixes.

Thank you for your efforts!

hyfdev added 8 commits August 17, 2026 16:06
…ady-loaded fold

An entry chunk imports the canonical symbol of every live resolved export it
serves, even when no included statement of the entry references it. The
predicted static import graph only derived targets from load_dependencies, so
the cycle check missed these edges and could approve a fold whose emitted
chunk graph is cyclic, exposing uninitialized bindings at runtime.

Derive entry-export service targets from the entry's full resolved_exports
(plus re-exported ESM wrapper inits), gated by the same symbol liveness that
emission applies. The gate keeps the prediction exact in both directions:
these targets also feed entry reachability, which must not claim chunks the
entry never loads.
can_merge_without_changing_entry_signature stopped at canonical_ref_for, but
consumers are served canonical_ref_resolving_namespace. A re-export whose
canonical ref is owned outside the merged set while its namespace alias lands
on a CJS module inside it slipped past as "owned elsewhere", although the
merge does move that namespace into the strict entry chunk. Resolve the alias
before deciding.
…nst emitted edges

The already-loaded fold decides on a prediction of the static imports emission
will produce, and nothing kept the two in sync: a new emission edge source
silently weakens the fold's cycle check. Reconcile them in debug builds right
where the final edges are derived, mirroring the existing chunk-level
prediction assert.

The contract is one-and-a-half-sided. Every emitted edge must be predicted, or
the cycle check ran blind. A predicted edge that is not emitted is tolerated
only when every module target behind it is side-effect free: pure
over-prediction only makes the cycle check more conservative and carries no
execution-order claim, and constant inlining makes it unavoidable.
…ty and cycle graphs

The adversarial review of the entry-export service fix found two ways the
single predicted graph could still diverge from emission. Service imports hang
on the entry (possibly facade) chunk, but the prediction attached them to
whichever atom hosts the entry module, so other entries' closures could
inherit edges their chunks never emit. And the fold reads symbol liveness
before the namespace-extraction and facade-elimination replays grow it, so an
export dead at decision time can still emit an import the cycle check never
saw.

Both consumers now get the approximation that is safe for them. The cycle
graph attaches service targets to the hosting atom unconditionally and without
a liveness gate: an over-approximation, immune to later liveness growth, and
sound because facades have zero static in-degree. The reachability graph
attaches them liveness-gated and only when the entry module's atom belongs to
exactly its own entry: growth is monotone, so gated edges are guaranteed
emitted, and shared atoms no longer leak service edges into other entries'
closures.

The debug assert follows: entry chunks predict service imports through their
entry module wherever it is hosted, plus the facade edge; the runtime
carve-out narrows from a whole chunk to runtime-requested items; the soft
direction also consults merge-populated field edges; chunks without modules
are out of scope, since a husk left by facade elimination or absorption gets
no emission edges at all.
…prediction

The follow-up review bounded two costs that stay by design: a dead entry
re-export can conservatively veto a fold through the ungated cycle-graph
service edges (missed optimization, taken over trusting a liveness snapshot
the replays may outgrow), and the base prediction's ambiguous branch still
reads decision-time statement inclusion — a narrower, pre-existing skew of the
same shape. Scope the immunity claim accordingly.
…g modules

The entry read its shared value through a helper module; importing it directly
pins the same fold, the same missed service edge, and the same cycle. Verified
by neutralizing the service-target prediction: the fixture fails through the
reconciliation assert before the runtime crash.
…irection

The rollup compat suite found the soft direction over-reaching: constant
inlining can kill the only symbol import of a chunk whose modules are
side-effect-annotated, leaving the base prediction with an edge emission never
derives (rollup@chunking form@namespace-reexport-side-effect-cache — where
main's own output already orphans that side-effect chunk; the divergence is
main-parity, not introduced by the prediction).

Base-prediction over-predictions filter the same load_dependencies edges the
pre-prediction bits reachability trusted, so they cannot be worse than shipped
behavior and need no assert. What does need one is the entry-export service
extension: it exceeds load_dependencies, nothing but its liveness gate keeps
entry reachability truthful, and the soft direction now checks exactly that
gate against emission.
@hyfdev
hyfdev enabled auto-merge (squash) August 17, 2026 11:42

@hyfdev hyfdev 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.

Thank you!

@hyfdev
hyfdev merged commit 982e344 into rolldown:main Aug 17, 2026
34 of 35 checks passed
@rolldown-guard rolldown-guard Bot mentioned this pull request Aug 19, 2026
shulaoda added a commit that referenced this pull request Aug 19, 2026
## [1.2.5] - 2026-08-19

### 🚀 Features

- bench: record per-suite peak memory in the node benchmark (#10704) by @IWANABETHATGUY
- binding: allocation-tracking global allocator behind the `tracking_allocator` feature (#10703) by @IWANABETHATGUY
- add armv7 android (armv7-linux-androideabi) support (#10691) by @shulaoda
- add `NAMESPACE_CONFLICT` warning for conflicting star re-exports (#7452) by @AliceLanniste

### 🐛 Bug Fixes

- rolldown_plugin_vite_reporter: avoid ANSI erase-line escape in non-TTY output (#10692) by @shulaoda
- rolldown_plugin_vite_resolve: preserve Yarn PnP virtual importer (#10591) by @Freakazo
- exclude hash placeholders from case-insensitive filename deconfliction (#10590) by @Nic-Polumeyv
- code-splitting: fold already-loaded side-effectful libraries into eager entries (#10645) by @JoviDeCroock
- dev: flush re-emitted assets when their content changes (#10637) by @btea
- renamer: rename nested `require`/`__filename`/`__dirname` bindings in CJS output (#10655) by @marcoroth
- silence two wasm-only warnings (#10668) by @IWANABETHATGUY

### 🚜 Refactor

- skip case folding for filenames with hash placeholders (#10689) by @hyfdev
- code-splitting: remove redundant synthetic statement owner (#10503) by @hyfdev
- renamer: move the cjs check into `rename_bindings_shadowing_cjs_ambient_names` (#10672) by @IWANABETHATGUY

### 📚 Documentation

- agents: point the never-edit list at the binding files that exist (#10719) by @melbinjp

### 🧪 Testing

- deconflict: pin renaming the external process binding away from the global (#10702) by @hyfdev
- dev: expect a hot update across a circular import (#10700) by @hyfdev

### ⚙️ Miscellaneous Tasks

- deps: upgrade oxc to 0.146.0 (#10707) by @Boshen
- deps: update rust crates (#10684) by @renovate[bot]
- deps: update npm packages (#10685) by @renovate[bot]
- build the benchmark comparison window from the JSON lines (#10701) by @IWANABETHATGUY
- wasi: retry the Node Test step to absorb the shared-dlmalloc flake (#10699) by @hyfdev
- append benchmark results to storage as JSON lines (#10682) by @IWANABETHATGUY
- deps: update github actions (#10683) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to v0.28.2 (#10679) by @renovate[bot]
- deps: update dependency rolldown-plugin-dts to v0.28.1 (#10674) by @renovate[bot]

### ❤️ New Contributors

* @melbinjp made their first contribution in [#10719](#10719)
* @Freakazo made their first contribution in [#10591](#10591)
* @marcoroth made their first contribution in [#10655](#10655)

Co-authored-by: shulaoda <[email protected]>
jfolcini pushed a commit to jfolcini/agaric that referenced this pull request Sep 2, 2026
…udget (#4591)

vite 8.2.2 resolves rolldown ~1.2.4, which lands 1.2.5 and its
'fold already-loaded side-effectful libraries into eager entries'
change (rolldown/rolldown#10645). On this bundle that merges ~53 shared
chunks into index and grows it 238 kB -> 275 kB gzip, 8.3% over the
#750 budget; 1.2.6 does not revert it. Pin the resolver to 1.2.4 with
an exact npm override, record it in the overrides table, and track the
removal trigger in #4591.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FF4YdrnLoJH1bpWVuYzcbc
Signed-off-by: Claude <[email protected]>
jfolcini pushed a commit to jfolcini/agaric that referenced this pull request Sep 2, 2026
…pdates (#4588)

* chore(npm): bump the lint-and-build group across 1 directory with 3 updates

Bumps the lint-and-build group with 3 updates in the / directory: [oxfmt](https://github.com/oxc-project/oxc/tree/HEAD/npm/oxfmt), [oxlint](https://github.com/oxc-project/oxc/tree/HEAD/npm/oxlint) and [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite).


Updates `oxfmt` from 0.64.0 to 0.65.0
- [Release notes](https://github.com/oxc-project/oxc/releases)
- [Changelog](https://github.com/oxc-project/oxc/blob/main/npm/oxfmt/CHANGELOG.md)
- [Commits](https://github.com/oxc-project/oxc/commits/oxfmt_v0.65.0/npm/oxfmt)

Updates `oxlint` from 1.79.0 to 1.80.0
- [Release notes](https://github.com/oxc-project/oxc/releases)
- [Changelog](https://github.com/oxc-project/oxc/blob/main/npm/oxlint/CHANGELOG.md)
- [Commits](https://github.com/oxc-project/oxc/commits/oxlint_v1.80.0/npm/oxlint)

Updates `vite` from 8.2.1 to 8.2.2
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v8.2.2/packages/vite)

---
updated-dependencies:
- dependency-name: oxfmt
  dependency-version: 0.65.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: lint-and-build
- dependency-name: oxlint
  dependency-version: 1.80.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: lint-and-build
- dependency-name: vite
  dependency-version: 8.2.2
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: lint-and-build
...

Signed-off-by: dependabot[bot] <[email protected]>

* build(deps): hold rolldown at 1.2.4 so the index chunk stays within budget (#4591)

vite 8.2.2 resolves rolldown ~1.2.4, which lands 1.2.5 and its
'fold already-loaded side-effectful libraries into eager entries'
change (rolldown/rolldown#10645). On this bundle that merges ~53 shared
chunks into index and grows it 238 kB -> 275 kB gzip, 8.3% over the
#750 budget; 1.2.6 does not revert it. Pin the resolver to 1.2.4 with
an exact npm override, record it in the overrides table, and track the
removal trigger in #4591.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FF4YdrnLoJH1bpWVuYzcbc
Signed-off-by: Claude <[email protected]>

* build: accept the larger entry chunk from rolldown 1.2.5 and re-baseline the bundle budgets

Drops the exact rolldown 1.2.4 override from the previous commit. The
maintainer's call: this is a local app, so the +37 kB gzip that rolldown
1.2.5's entry fold adds to index is not worth holding a resolver back
for. Budgets re-measured from the 1.2.5 build with the usual 10%
headroom (index 274620 B gzip -> 302082 B budget).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FF4YdrnLoJH1bpWVuYzcbc
Signed-off-by: Claude <[email protected]>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Claude <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Claude <[email protected]>
hyfdev added a commit that referenced this pull request Nov 19, 2026
…hunks (#11753)

With `preserveEntrySignatures: false`, the default for Vite client builds, an unused re-export in an entry can run a lazy chunk at startup.

The chunk's `import()` result also gets an internal export.

This PR keeps the chunk lazy and its namespace limited to the module's own exports.

```js
// main.js
export { q } from './w.js';
console.log('entry runs');
globalThis.loadD = () => import('./d.js');

// w.js
export function q() { return 'q'; }

// d.js
import { q } from './w.js';
console.log('d runs', q());
export const d = 1;
```

On `main`, startup prints `d runs q` before `entry runs`, and `Object.keys(await loadD())` is `['d', 't']`.

With this PR, startup prints only `entry runs` and the keys are `['d']`, as with Node and Rollup 4.68.2.

Rolldown registered cross-chunk imports for every export of an entry, even when `preserveEntrySignatures: false` drops them.

It now skips them in that case, unless the entry is also dynamically imported or `preserveModules` is on.

As on `main`, it still keeps an export's import when the export's module, or anything it statically imports, has side effects.

Side effects behind a `sideEffects: false` package count too.

A lazy chunk with those side effects then still runs at startup.

The `already_loaded_entry_reexport_service_edge` fixture from #10645 now has one chunk fewer, and its runtime checks still pass.
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]: common chunk executed by the entrypoint is not merged in the entrypoint

2 participants