Skip to content

module: fix require() of re-exported CJS dep after cache deletion - #67761

Open
Pranshu12354 wants to merge 3 commits into
nodejs:mainfrom
Pranshu12354:fix-67611-cjs-reexport-fast-path
Open

Pranshu12354 wants to merge 3 commits into
nodejs:mainfrom
Pranshu12354:fix-67611-cjs-reexport-fast-path

Conversation

@Pranshu12354

Copy link
Copy Markdown

Fixes: #67611

The fast path in Module._load() (relativeResolveCache hit) returned getExportsForCircularRequire() for any unloaded cache entry, without the kIsCachedByESMLoader/kModuleCircularVisited checks the regular cache path performs a few lines below. Cache entries pre-created by the ESM loader for re-exported dependencies (cjsPreparseModuleExports()) were therefore mistaken for circular requires on re-import after require.cache deletion, returning empty exports (and swallowing syntax errors in the dependency).

This mirrors the regular-path checks in the fast path and falls through to actually loading an unvisited ESM-loader cache entry. The new regression test covers re-import with values 1–3 and the syntax-error case; it fails on an unpatched build with actual {} vs expected { value: 2 }.

Disclosure: this change was prepared with AI assistance (Muse); I reviewed the diff and the test logic before submitting.

The fast path in Module._load() returned getExportsForCircularRequire()
for any unloaded cache entry found via relativeResolveCache, without
the kIsCachedByESMLoader/kModuleCircularVisited checks the regular
cache path performs. Cache entries pre-created by the ESM loader for
re-exported dependencies (cjsPreparseModuleExports()) were therefore
mistaken for circular requires on re-import, returning empty exports.

Mirror the regular-path checks in the fast path and fall through to
loading for an unvisited ESM-loader cache entry.

Fixes: nodejs#67611
Signed-off-by: Prashant Singh Sengar <prashantsengar2003@gmail.com>
Assisted-by: Muse
The fast path in Module._load() returned getExportsForCircularRequire()
for any unloaded cache entry found via relativeResolveCache, without
the kIsCachedByESMLoader/kModuleCircularVisited checks the regular
cache path performs. Cache entries pre-created by the ESM loader for
re-exported dependencies (cjsPreparseModuleExports()) were therefore
mistaken for circular requires on re-import, returning empty exports.

Mirror the regular-path checks in the fast path and fall through to
loading for an unvisited ESM-loader cache entry.

Fixes: nodejs#67611
Signed-off-by: Prashant Singh Sengar <prashantsengar2003@gmail.com>
Assisted-by: Muse
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

require() in an imported CommonJS module returns empty exports for a re-exported dependency after require.cache deletion

2 participants