Repository navigation
fix(commonjs): initialize replaced module.exports to {} for circular requires - #2045
Open
ItsRenjiii wants to merge 2 commits into
Open
ItsRenjiii wants to merge 2 commits into
ItsRenjiii wants to merge 2 commits into
Conversation
…requires
When a module's only export is a top-level `module.exports = ...` assignment
and it is wrapped in a require function, its exports variable was declared
without a value. A circular require (or a top-level `this`) that ran before the
assignment then saw `undefined` instead of the initial `{}` exports object that
Node provides.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[Medium impact] The PR appears safe to merge; both previous failures are addressed. Reviews (2) · Last reviewed commit: "fix(commonjs): set replaced exports on f..." · Reviewed by Greptile |
3 of 9 tasks
…evel this
Address review feedback:
- With a require wrapper, assign `{}` when the first require starts instead of
initializing the variable at module level. In a mixed ES/CommonJS cycle a
re-entrant require can complete the module before its body resumes, and the
module-level initializer then overwrote the completed exports.
- Without a require wrapper (`strictRequires: false`, or `'auto'`/`'debug'` for
modules outside a cycle), declare `var exports = {}` before the module body
when it uses top-level `this`, which is rewritten to that variable and can run
before the `module.exports =` assignment.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rollup Plugin Name:
commonjsThis PR contains:
Are tests included?
Breaking Changes?
If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.
List any relevant issue numbers:
Reported in discussion #2043 (no issue filed).
Description
A module whose only export is a top-level
module.exports = ...assignment usesexportMode === 'replace'. When it also gets a require wrapper, its exports variable is declared with no value:A circular
requirethat reaches the module before the assignment runs therefore getsundefined, where Node gives it the initial{}exports object. A top-levelthisused before the assignment is alsoundefined, sothis.x = 1throwsTypeError: Cannot set properties of undefined.This PR gives the variable Node's initial
{}in both places it can be read early:a = {};is set when the first require starts, right after thehasRequiredAguard. Setting it there rather than at module level keeps a value that a re-entrant require already completed: in a mixed ES/CommonJS cycle a module-levelvar a = {};can run afterrequireA()has finished and erase its exports.strictRequires: false, or'auto'/'debug'for a module outside a cycle), top-levelthisis rewritten toaand runs beforevar a = .... When the module uses top-levelthis, it now getsvar a = {};before the body, and the assignment becomes a plaina = .... Other unwrapped modules produce the same output as before.The
module.exports =assignment still replaces the object once it runs.Reproduction from the discussion (
rollup4.68.0,@rollup/plugin-commonjs29.0.3):node entry.jsstrictRequires: true/'auto'){ seenByB: 'object' }{ seenByB: 'undefined' }{ seenByB: 'object' }this.x = 1beforemodule.exports =(true/'auto'/false){ y: 2 }TypeError{ y: 2 }A cycle with
strictRequires: falsestill givesundefined, as onmaster, because that mode does not support cycles.Tests
strict-requires-circular-replace(cycle) andstrict-requires-this-before-replace,this-before-replace-autoandthis-before-replace-unwrapped(top-levelthisundertrue,'auto'andfalse). All four fail onmaster.strict-requires-circular-replace-mixed: the mixed ES/CommonJS cycle from review. It fails on this PR's first commit and passes now.var x;+x = {};inside the require function, plus one chunk hash that follows from its content.validate.ymlall pass locally:pnpm install --frozen-lockfile,prettier:check,pnpm --recursive build,lint:jsandci:coveragefor...[origin/master]. The commonjs suite (296 tests) passes on Node 20 and 24.🤖 Generated with Claude Code