Skip to content

fix(chunk-import-map): keep import map keys unique when chunks share inlined modules - #11226

Closed
mattlewis92 wants to merge 1 commit into
rolldown:mainfrom
mattlewis92:fix/chunk-import-map-unique-keys
Closed

mattlewis92 wants to merge 1 commit into
rolldown:mainfrom
mattlewis92:fix/chunk-import-map-unique-keys

Conversation

@mattlewis92

@mattlewis92 mattlewis92 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

chunkImportMap gives each hashed chunk a stable key. A common chunk without a facade module is keyed by its name the first time that name is seen, and by its smallest module id after that. The fallback assumes no two chunks contain the same module.

experimentalInlineCommonChunks breaks that assumption: an inlined record is copied into every carrier chunk. When three or more same-named carriers (e.g. several lib/index.js chunks) carry the same record and it sorts first in each of them, every carrier after the first gets the same key. importmap.json then has one entry for that key, the other carriers have no entry at all, and their importers load the wrong chunk. At runtime this shows up as an importer reading the wrong values, or as The requested module './x.js' does not provide an export named '…'.

Fix

Track the stable file names already handed out. When a chunk's key is taken, hash the chunk name plus all of its module ids (sorted), and if that is taken too, keep hashing in a counter until the key is unique.

  • A chunk whose key did not collide keeps exactly the key it has today, so existing import maps don't churn. Only chunks that used to share a key get a new one.
  • args.chunks is an index map in chunk order, the same order the existing first-seen-name logic already depends on, so the result is deterministic across builds.

The plugin only sees module_ids, which include inlined copies, so it can't key on a chunk's own modules. Hashing every module id is the next most specific input, and the counter covers the theoretical case of two chunks with the same name and the same module set.

Test

function/inline_common_chunks/chunk_import_map_unique_keys: three entries share three common chunks named lib, each carrying an inlined copy of common/shared.js, which sorts before every packages/*/lib/index.js. _test.mjs asserts that every hashed chunk has its own importmap.json entry, then runs the entries with a resolve hook that applies the import map the way a browser would. The fixture has snapshot: false, because the fallback keys hash absolute module ids, so they differ between checkouts.

On main the fixture fails, because two of the lib chunks share one key:

AssertionError [ERR_ASSERTION]: every hashed chunk has its own import map entry
  actual:   ['/lib-3Q6428Ic.js', '/lib-sz4olCaC.js', '/rolldown-runtime-ivKO2lFq.js']
  expected: ['/lib-3Q6428Ic.js', '/lib-53Q6ocDg.js', '/lib-sz4olCaC.js', '/rolldown-runtime-ivKO2lFq.js']

Without that assertion, the runtime check also fails on main: the baz importers resolve to the bar chunk ('a shared:foo shared:bar' instead of 'a shared:foo shared:baz'). With the fix, both pass, and the two keys that didn't collide are the same as on main.

cargo test -p rolldown --test integration shows the same results before and after, apart from the new fixture.

…inlined modules

A common chunk without a facade module is keyed by its name the first time
the name is seen and by its smallest module id after that. With
`experimentalInlineCommonChunks`, an inlined record is copied into every
carrier chunk, so several same-named carriers can share their smallest module
id and get one key. `importmap.json` then holds a single entry for them, and
the other carriers' importers resolve to the wrong chunk.

When a key is already taken, derive it from the chunk name and all of its
module ids, then add a counter until it is unique. Keys that did not collide
before are unchanged.
@mattlewis92
mattlewis92 force-pushed the fix/chunk-import-map-unique-keys branch from 3bb24f8 to 1ee2567 Compare October 15, 2026 22:33
@codspeed

codspeed Bot commented Oct 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 4 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing mattlewis92:fix/chunk-import-map-unique-keys (1ee2567) with main (d226543)

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 commented Oct 17, 2026

Copy link
Copy Markdown
Member

Hi. Thanks for your efforts! This is a known issue and I've been looking into it for a week. I came up with #11230 which I think a more proper fix direction.

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