Skip to content

fix(esm-library): deconflict module external helper names#13695

Merged
JSerFeng merged 5 commits intoweb-infra-dev:mainfrom
JSerFeng:fy/fix-esm-library-module-external-name-collision
Apr 16, 2026
Merged

fix(esm-library): deconflict module external helper names#13695
JSerFeng merged 5 commits intoweb-infra-dev:mainfrom
JSerFeng:fy/fix-esm-library-module-external-name-collision

Conversation

@JSerFeng
Copy link
Copy Markdown
Contributor

Summary

This PR fixes modern-module ESM rendering when a ModuleExternal init fragment introduces top-level bindings that collide with source bindings during deconfliction.

It also adds an externals case that covers helper-shaped source code directly in index.js, including a literal require("fs"), while the project external for fs is also configured as node-commonjs.

Root Cause

rspack_plugin_esm_library previously reserved namespace-import locals from ModuleExternal init fragments, but it did not reserve other top-level declarations introduced by those fragments. That allowed generated helpers like __rspack_createRequire and __rspack_createRequire_require to collide with user/source bindings in ESM library rendering.

What Changed

  • reserve top-level declarations from ModuleExternal init fragments before top-level symbol deconfliction
  • keep the reservation order aligned with init-fragment render order and key deduplication semantics
  • add unit coverage for helper/provided-dependency declarations in link.rs
  • add an ESM output case where the source already contains helper-shaped bindings and require("fs"), while fs is also a node-commonjs external

Validation

  • cargo test -p rspack_plugin_esm_library link --lib
  • cargo fmt --all --check
  • cd tests/rspack-test && PATH="/tmp/codex-bin:$HOME/.nvm/versions/node/v20.19.0/bin:$PATH" pnpm testu EsmOutput.test.js -t "externals/node-commonjs-helper-name-collision"
  • cd tests/rspack-test && PATH="/tmp/codex-bin:$HOME/.nvm/versions/node/v20.19.0/bin:$PATH" pnpm test EsmOutput.test.js -t "externals/node-commonjs-helper-name-collision"

@github-actions github-actions Bot added the release: bug fix release: bug related release(mr only) label Apr 14, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 14, 2026

Merging this PR will degrade performance by 4.12%

⚡ 2 improved benchmarks
❌ 2 regressed benchmarks
✅ 24 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation rust@create_module_hashes 21.7 ms 21.4 ms +1.42%
Simulation rust@persistent_cache_restore_after_single_file_change@basic-react-development 27.8 ms 29 ms -4.12%
Simulation rust@persistent_cache_restore@basic-react-development 26.8 ms 26.1 ms +2.74%
Simulation rust@create_chunk_ids 10.2 ms 10.6 ms -3.39%

Comparing JSerFeng:fy/fix-esm-library-module-external-name-collision (2394a16) with main (e26bca5)

Open in CodSpeed

@JSerFeng
Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

ℹ️ 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".

@JSerFeng JSerFeng marked this pull request as ready for review April 14, 2026 07:01
Copilot AI review requested due to automatic review settings April 14, 2026 07:01
@JSerFeng
Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

ℹ️ 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".

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes ESM-library rendering deconfliction when ModuleExternal init fragments introduce helper bindings that can collide with source/module top-level bindings (notably the node-commonjs createRequire helper).

Changes:

  • Collect and reserve top-level declarations introduced by ModuleExternal init fragments prior to top-level symbol deconfliction.
  • Refactor module-external init-fragment collection to keep reservation behavior aligned with init-fragment render ordering and key de-duplication.
  • Add unit tests for the new reservation behavior and add an ESM output test case covering helper-shaped source bindings plus node-commonjs externals.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/rspack_plugin_esm_library/src/link.rs Reserves additional top-level decls from ModuleExternal init fragments and adds unit coverage for the new parsing/reservation logic.
tests/rspack-test/esmOutputCases/externals/node-commonjs-helper-name-collision/rspack.config.js Adds a repro config that externalizes fs as node-commonjs.
tests/rspack-test/esmOutputCases/externals/node-commonjs-helper-name-collision/index.js Adds a repro entry asserting helper-name deconfliction against source bindings and require("fs").
tests/rspack-test/esmOutputCases/externals/node-commonjs-helper-name-collision/__snapshots__/esm.snap.txt Snapshot verifying generated ESM output uses deconflicted helper names.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rspack_plugin_esm_library/src/link.rs
@JSerFeng
Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

ℹ️ 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".

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 15, 2026

📝 Rspack Ecosystem CI: Open

suite result
rstest ❌ failure
modernjs ✅ success
lynx-stack ❌ failure
rsbuild ✅ success
rspress ✅ success
rsbuild-rsc-plugin ✅ success
rsdoctor ✅ success
rslib ❌ failure
examples ❌ failure
devserver ✅ success
plugin ❌ failure

@JSerFeng JSerFeng merged commit 3d76b2b into web-infra-dev:main Apr 16, 2026
36 checks passed
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.

3 participants