Skip to content

fix(deconflict): rename CJS locals shadowing wrapped-ESM namespace objects#9970

Draft
IWANABETHATGUY wants to merge 1 commit into
mainfrom
fix/9882-wrapped-esm-namespace-object
Draft

fix(deconflict): rename CJS locals shadowing wrapped-ESM namespace objects#9970
IWANABETHATGUY wants to merge 1 commit into
mainfrom
fix/9882-wrapped-esm-namespace-object

Conversation

@IWANABETHATGUY

@IWANABETHATGUY IWANABETHATGUY commented Jun 25, 2026

Copy link
Copy Markdown
Member

Standalone PR on main (the #9921 / #10006 stack it followed is now merged).

Extends the #9882 chunk-root shadowing fix to one more channel: a CJS closure local that shadows a wrapped-ESM namespace object read via require().

Problem

When a CJS-wrapped module require()s a wrapped-ESM module, the finalizer rewrites the require to (init_x(), __toCommonJS(xxx_exports)) — reading the importee's chunk-root namespace object. An author-local sharing that namespace object's final name shadows the read, producing a self-referential declaration:

var xxx_exports = (init_x(), __toCommonJS(xxx_exports)); // reads itself → undefined

TypeError: Cannot convert undefined or null to object at module-eval time.

Fix

Handle this in the precise rename_cjs_locals_shadowing_referenced_chunk_bindings post-pass — the same mechanism #9921 already uses for named-import and star-member shadowing. For each require() of a non-CommonJS importee (mirroring the finalizer's gate), rename a root-scope local that shares the importee namespace object's final name.

Why the post-pass rather than the chunk-root captured-name set: it runs after root-scope names are assigned, so it sees the namespace object's real (possibly renamed) name, and it only touches modules that actually reference the namespace — renaming the shadowing local, never the namespace.

Test

cjs_shadowing_require_namespace_object — throws __toCommonJS(undefined) without the fix, passes with it (the namespace keeps its bare name, the colliding local is suffixed). The shared sibling-suffix-skipping path of the renamer is already covered by cjs_shadowing_renamed_chunk_binding.

Full integration suite: 1778 passed, 0 failed.

@IWANABETHATGUY IWANABETHATGUY marked this pull request as draft June 25, 2026 11:12
@codspeed-hq

codspeed-hq Bot commented Jun 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing fix/9882-wrapped-esm-namespace-object (22a0b55) with main (9570c95)2

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.

  2. No successful run was found on refactor/9882-extract-captured-names (a5f5037) during the generation of this report, so main (9570c95) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch from 0acfd6b to b07d5db Compare June 25, 2026 11:31
@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-cjs-wrapped-esm-shadowing branch from 89d1a08 to c3968fa Compare June 29, 2026 03:41
@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch from b07d5db to 7da5f84 Compare June 29, 2026 03:55

IWANABETHATGUY commented Jun 29, 2026

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add the label graphite: merge-when-ready to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@IWANABETHATGUY IWANABETHATGUY changed the base branch from fix/9882-cjs-wrapped-esm-shadowing to graphite-base/9970 June 29, 2026 04:09
@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch from 7da5f84 to 22a0b55 Compare June 29, 2026 04:09
@IWANABETHATGUY IWANABETHATGUY changed the base branch from graphite-base/9970 to refactor/9882-extract-captured-names June 29, 2026 04:10
@IWANABETHATGUY IWANABETHATGUY marked this pull request as ready for review June 29, 2026 04:10
@IWANABETHATGUY IWANABETHATGUY marked this pull request as draft June 29, 2026 04:11
@IWANABETHATGUY IWANABETHATGUY force-pushed the refactor/9882-extract-captured-names branch from a5f5037 to cfad266 Compare June 29, 2026 05:24
@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch 2 times, most recently from 058482c to c35d12a Compare June 29, 2026 05:33
@IWANABETHATGUY IWANABETHATGUY force-pushed the refactor/9882-extract-captured-names branch from cfad266 to a5b2c80 Compare June 29, 2026 05:33
@graphite-app graphite-app Bot changed the base branch from refactor/9882-extract-captured-names to graphite-base/9970 June 29, 2026 06:08
@graphite-app graphite-app Bot closed this in 520d81e Jun 29, 2026
@IWANABETHATGUY IWANABETHATGUY changed the base branch from graphite-base/9970 to main June 29, 2026 06:36
@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch from c35d12a to 2e3d1cb Compare June 29, 2026 06:36
@netlify

netlify Bot commented Jun 29, 2026

Copy link
Copy Markdown

Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit f2d1006
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a424e18eb0dd900099f4719

@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch from 2e3d1cb to 5ff4397 Compare June 29, 2026 10:37
…jects

A CJS-wrapped module that `require()`s a wrapped-ESM module reads the importee's
chunk-root namespace object via `(init_x(), __toCommonJS(xxx_exports))`. An
author-local sharing that namespace object's final name shadows the read,
producing the self-referential `var xxx_exports = (init_x(), __toCommonJS(xxx_exports))`
-> `__toCommonJS(undefined)` -> TypeError at module-eval (issue #9882,
require()/namespace channel).

Handle this in the precise `rename_cjs_locals_shadowing_referenced_chunk_bindings`
post-pass (the same mechanism #9921 uses for named-import and star-member
shadowing): for each `require()` of a non-CommonJS importee, rename a root-scope
local sharing the importee namespace object''s *final* name. The post-pass runs
after root-scope names are assigned, so it sees the namespace''s real name and
only touches modules that actually reference it -- renaming the shadowing local,
never the namespace.

Adds the regression fixture cjs_shadowing_require_namespace_object: it throws
`__toCommonJS(undefined)` on the base and passes with the fix.
@IWANABETHATGUY IWANABETHATGUY force-pushed the fix/9882-wrapped-esm-namespace-object branch from 67e3b00 to f2d1006 Compare June 29, 2026 10:51
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