Skip to content

fix: sort ConcatenatedModule export keys for deterministic builds#13425

Open
mango766 wants to merge 1 commit intoweb-infra-dev:mainfrom
mango766:fix/deterministic-concatenated-module-exports
Open

fix: sort ConcatenatedModule export keys for deterministic builds#13425
mango766 wants to merge 1 commit intoweb-infra-dev:mainfrom
mango766:fix/deterministic-concatenated-module-exports

Conversation

@mango766
Copy link
Copy Markdown

Summary

Sort exports_map entries by key before rendering the __webpack_require__.d(exports, {...}) object in ConcatenatedModule.

exports_map is a HashMap<Atom, String>, so .iter() produces non-deterministic iteration order. This means the same source code can produce different export key ordering across builds, leading to different content hashes for the same chunk — even with no code changes.

The fix collects entries into a vec, sorts by key, then builds the definitions string. This matches the approach already used in ESMExportInitFragment (init_fragment.rs:356) where export_map is sorted before rendering.

Related links

Closes #13182

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Sort exports_map entries by key before rendering the
__webpack_require__.d(exports, {...}) object in ConcatenatedModule.
HashMap iteration order is non-deterministic, causing identical source
to produce different content hashes across builds.

This matches the approach already used in ESMExportInitFragment
(init_fragment.rs:356) where export_map is sorted before rendering.

Closes web-infra-dev#13182

Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
@github-actions github-actions bot added the release: bug fix release: bug related release(mr only) label Mar 20, 2026
Copy link
Copy Markdown

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

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: 6594e19f01

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

)
})
.collect();
definitions.sort_by(|a, b| a.0.cmp(&b.0));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Sort the remaining export-name comments for deterministic output

If the nondeterminism here comes from iterating FxHashMap<Atom, _>, this change only stabilizes the definitions object. The same code path still appends // UNUSED EXPORTS and // INLINED EXPORTS by joining FxHashSet<Atom> iterators directly in crates/rspack_core/src/concatenated_module.rs:1637-1648, so any concatenated module that tree-shakes or inlines an export will still produce run-to-run byte differences in non-minified/comment-preserving builds.

Useful? React with 👍 / 👎.

@chenjiahan chenjiahan requested a review from JSerFeng March 25, 2026 06:05
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 25, 2026

Merging this PR will not alter performance

✅ 23 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing mango766:fix/deterministic-concatenated-module-exports (6594e19) with main (a4e0229)

Open in CodSpeed

Footnotes

  1. 3 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.

@JSerFeng
Copy link
Copy Markdown
Contributor

Thanks

You can use IndexMap directly to avoid sorting, and you could do the same for unused and inlined exports as well if you don't mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: bug fix release: bug related release(mr only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Non-deterministic content hash: export key order in ConcatenatedModule

2 participants