Skip to content

[Bug]: Imported binding is shadowed inside CommonJS-wrapped ESM entry, causing runtime TypeError #9882

Description

@jimsimon

Reproduction link or steps

https://github.com/jimsimon/rolldown-cjs-import-name-collision-repro

npm install
npm run verify

What is expected?

The imported dependency binding and the local variable inside the generated wrapper should be deconflicted. For example, the dependency binding could be renamed:

var sharedValue$1;

var init_dependency = __esmMin(() => {
  sharedValue$1 = /* dependency value */;
});

var require_entry = __commonJSMin((exports, module) => {
  init_dependency();

  var firstCondition = { kind: sharedValue$1.EventMatch };

  var sharedValue = class extends BaseAlias {};
});

The exact generated names do not matter. The important part is that the reference to the imported binding must not resolve to the local var declared inside the wrapper callback.

What is actually happening?

Rolldown emits output where an imported binding from one module is shadowed by a local var inside a generated CommonJS wrapper.

The source entry imports SharedEnum from src/dependency.js and later declares a local var sharedValue. The entry also contains a CommonJS environment probe, which causes Rolldown to wrap the entry with its CommonJS runtime helper.

Rolldown then emits code shaped like this:

var sharedValue;

var init_dependency = __esmMin(() => {
  sharedValue = /* dependency value */;
});

var require_entry = __commonJSMin((exports, module) => {
  init_dependency();

  var firstCondition = { kind: sharedValue.EventMatch };

  var sharedValue = class extends BaseAlias {};
});

The inner var sharedValue is hoisted inside the wrapper callback, so sharedValue.EventMatch reads the inner local variable before it is initialized. At runtime this throws:

TypeError: Cannot read properties of undefined (reading 'EventMatch')

System Info

Rolldown: 1.1.1
Chrome: 148.0.7778.215
Node: 22.20.0
Yarn: 4.9.4

Any additional comments?

No response

Metadata

Metadata

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions