fix: clean up ESM registry when require() of ESM module fails#27288
fix: clean up ESM registry when require() of ESM module fails#27288Jarred-Sumner merged 2 commits intomainfrom
Conversation
When `require()` loads an ESM module (.mjs) that throws during evaluation, the module was removed from `requireMap` but left in the ESM registry (`Loader.registry`) in a partially-initialized state. A subsequent `import()` of the same module would find this corrupt entry and throw `ReferenceError: Cannot access 'foo' before initialization` instead of re-throwing the original evaluation error. Fix by also deleting the module from `Loader.registry` in both `overridableRequire` and `requireESMFromHijackedExtension` when ESM evaluation fails, allowing `import()` to re-evaluate from scratch. Closes #27287 Co-Authored-By: Claude <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 WalkthroughThe changes fix a bug where requiring a failing ESM module via CommonJS would leave it in a partially-initialized state in the ESM registry. The fix ensures failed modules are fully cleared from both the require map and ESM registry on error, allowing subsequent imports to re-evaluate the module from scratch. Changes
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|
Newest first ✅ c17de — Looks good! Reviewed 2 files across Previous reviews✅ 68641 — Looks good! Reviewed 2 files across |
Summary
require()loads an ESM module (.mjs) that throws during evaluation, the module was removed fromrequireMapbut left in the ESM registry (Loader.registry) in a partially-initialized stateimport()of the same module would find this corrupt entry and throwReferenceError: Cannot access 'foo' before initializationinstead of re-throwing the original evaluation errorLoader.registryin bothoverridableRequireandrequireESMFromHijackedExtensionwhen ESM evaluation fails, allowingimport()to re-evaluate from scratchCloses #27287
Test plan
test/regression/issue/27287.test.tsUSE_SYSTEM_BUN=1)bun bd testrequire()correctly throws original error,import()now re-throws the same error instead ofReferenceError🤖 Generated with Claude Code