Skip to content

fix: make exports-kind promotion independent of module discovery order - #11225

Closed
natew wants to merge 5 commits into
rolldown:mainfrom
natew:fix/deterministic-exports-kind
Closed

natew wants to merge 5 commits into
rolldown:mainfrom
natew:fix/deterministic-exports-kind

Conversation

@natew

@natew natew commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Async module discovery can change which importer first promotes an exportless module. An ESM import and a CommonJS require can then produce different wrappers for identical inputs.

Order importers by reverse deterministic execution order, with stable module IDs breaking ties. The Notify regression forces opposite discovery orders without sleeps, asserts raw byte equality, and executes each bundle to check setup runs once. It repeats with swapped source import order.

This also changes the existing import_missing_neither_es6_nor_common_js fixture: reverse execution order visits its require entry before its static import entries, promoting the exportless module to CommonJS. The updated snapshot records a memoized CommonJS wrapper and namespace interop, replacing the former ESM shim warnings. Its actual emitted output was executed: initialization runs once, require returns an empty object, static and dynamic namespaces share that default object, missing x/y remain undefined, and the fixture's invalid function calls throw TypeError. This observable namespace classification change needs maintainer review before merge.

Validation on the PR source plus CI repairs:

  • Discovery-order regression passes, including swapped source order and setup-once checks.
  • All 56 CJS compatibility cases pass.
  • Affected esbuild fixture passes with snapshot updates disabled.
  • Focused integration-target Clippy with warnings denied and workspace Rust formatting pass.
  • Fresh native JS binding from the isolated source passes the saved public seven-module barrier probe: opposite discovery orders produce identical 899-byte bundles, and setup executes once in each. Matching existing JS glue was reused with the freshly built addon explicitly selected through NAPI_RS_NATIVE_LIBRARY_PATH.

The repair preserves the existing bot commit and does not change the promotion algorithm further. Full CI remains with the CI owner; current-source independent review and broader downstream qualification remain pending. This is separate from #11206 and #11227. No downstream dependency or package publication is included.

AI assistance was used to implement and qualify this change. This PR remains a draft.

Maintenance update: merged current main e3355e0 with a normal merge, preserving the existing fix and history. Regenerated the owning fixture for the current output-comment format. The fixture passes with INSTA_UPDATE=no, and cargo test -p rolldown --test integration exports_kind_stable_across_discovery_order -- --nocapture passes, including byte equality, one setup execution, and the swapped-entry control. No promotion algorithm changes. AI assistance was used for this maintenance.

natew and others added 5 commits October 15, 2026 10:59
Sort importers by reverse exec order (entries first) instead of discovery order, so first Import vs Require promotion of ExportsKind::None is stable across async load completion. Tie-break by stable id.

Validation: cargo test exports_kind_stable_across_discovery_order fail-before 101 (Esm vs Cjs mismatch) / pass-after 0 (byte-equal, setup-once x4 via node, swapped entry); cjs_compat 53 passed, 0 failed.
Validation: current-main deterministic regression passed, 56 CJS compatibility checks passed, cargo fmt --all --check passed.
Move the load-hook constant before statements for pedantic Clippy. Reverse execution order deterministically visits the require entry before static import entries in the mixed exportless-module fixture; record the resulting CommonJS wrapper and namespace interop instead of stale ESM shim warnings.

Validation: discovery-order regression passed with byte equality and setup-once for both entry orders; 56 CJS compatibility cases passed; affected fixture passed without snapshot updates; focused integration Clippy and cargo fmt passed. Fresh source binding public barrier probe produced identical 899-byte bundles with one setup execution. Executed affected fixture output: one initialization, shared default exports, missing x/y undefined, invalid function calls throw TypeError.
Validation: regenerated and passed the affected esbuild fixture on main e3355e0 plus the existing fix. Existing discovery-order regression is being checked before push. No promotion algorithm changes.
@hyfdev

hyfdev commented Nov 11, 2026

Copy link
Copy Markdown
Member

In your entry_swapped.js, this order still makes init.js ESM in every build, while entry.js makes it CommonJS.

Since #11271, that choice also decides its top-level this, so a UMD-style init.js throws TypeError in the swapped order.

#11633 makes such a module CommonJS whenever it is required, as esbuild does, and keeps your load-order test in a shorter form.

Closing this in favor of #11633.

Thanks for working on this!

@hyfdev hyfdev closed this Nov 11, 2026
hyfdev added a commit that referenced this pull request Nov 11, 2026
…orted and required (#11633)

A file without module syntax that is both imported and required was ESM in some builds and CommonJS in others.

This PR makes it CommonJS in every build, as esbuild does.

```js
// main.js
import './umd.js'
import { value } from './reader.js'

// reader.js
exports.value = require('./umd.js').value

// umd.js
;(function (root) {
  root.value = 1
})(this)
```

Before, this bundle threw `TypeError: Cannot set properties of undefined`, because `umd.js` became ESM and its top-level `this` was `undefined`.

After, `umd.js` is CommonJS and `this` is its exports object, as in Node.

The link stage classified such a file by the first importer it visited: `import` made it ESM, `require()` made it CommonJS.

That order follows load completion, so it can change between builds: in a fixture whose two importers load in parallel, 98 of 200 builds gave ESM.

Now every such file that is required, or loaded by `import()` with code splitting disabled, becomes CommonJS before that walk.

Two snapshots change to the CommonJS wrapper.

In the esbuild-ported `import_missing_neither_es6_nor_common_js`, esbuild also makes `foo.js` CommonJS.

The test that forces both load orders comes from #11225.

Co-authored-by: Nate Wienert <[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.

2 participants