Skip to content

Commit 657f1a5

Browse files
committed
module: allow require.cache edits before CJS import
Drop the assertion in loadCJSModuleWithModuleLoad() that the translator's module is still the require.cache entry for its filename. The exports returned by Module._load() are already mirrored into the translator's module when Module._load() does not load that module itself, so a deleted entry is now loaded afresh and a replaced entry's exports are used, as when the module is first imported after the same require.cache change. Signed-off-by: Daniel Roe <daniel@roe.dev>
1 parent 8aa19d2 commit 657f1a5

7 files changed

Lines changed: 52 additions & 4 deletions

File tree

β€Žlib/internal/modules/esm/translators.jsβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ translators.set('require-commonjs-typescript', (url, translateContext, parentURL
362362
* @param {import('./loader').TranslateContext} translateContext Context for the translator
363363
*/
364364
function loadCJSModuleWithModuleLoad(module, source, url, filename, isMain, translateContext) {
365-
assert(module === CJSModule._cache[filename]);
366365
debug(`loadCJSModuleWithModuleLoad ${url}`);
367366
let exports;
368367
if (translateContext.isResolvedBySyncHooks) {
@@ -383,9 +382,10 @@ function loadCJSModuleWithModuleLoad(module, source, url, filename, isMain, tran
383382
exports = wrapModuleLoad(filename, undefined, isMain, kShouldSkipModuleHooks);
384383
}
385384

386-
// Patched Module._load implementations may return exports without updating the
387-
// ESM-created cache entry. Mirror the returned value into the translator-owned
388-
// module so the synthetic module namespace observes the loaded exports.
385+
// Module._load may return exports without updating the ESM-created cache entry,
386+
// e.g. when it is patched or require.cache has been modified. Mirror the returned
387+
// value into the translator-owned module so the synthetic module namespace
388+
// observes the loaded exports.
389389
if (!module.loaded) {
390390
module.exports = exports;
391391
module.loaded = true;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Tests that a CommonJS module left over from an import() that failed to link
2+
// is loaded afresh when its require.cache entry is deleted before evaluation.
3+
4+
import '../common/index.mjs';
5+
import * as fixtures from '../common/fixtures.mjs';
6+
import assert from 'node:assert';
7+
import { createRequire } from 'node:module';
8+
9+
const require = createRequire(import.meta.url);
10+
11+
await assert.rejects(
12+
import(fixtures.fileURL('es-modules', 'cjs-after-failed-import', 'entry.mjs')),
13+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' },
14+
);
15+
16+
const depPath = fixtures.path('es-modules', 'cjs-after-failed-import', 'dep.cjs');
17+
delete require.cache[depPath];
18+
const { default: dep } = await import(fixtures.fileURL('es-modules', 'cjs-after-failed-import', 'importer.mjs'));
19+
assert.deepStrictEqual(dep, { name: 'dep' });
20+
assert.strictEqual(require(depPath), dep);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Tests that a CommonJS module left over from an import() that failed to link
2+
// uses the exports of its require.cache entry when replaced before evaluation.
3+
4+
import '../common/index.mjs';
5+
import * as fixtures from '../common/fixtures.mjs';
6+
import assert from 'node:assert';
7+
import { createRequire, Module } from 'node:module';
8+
9+
const require = createRequire(import.meta.url);
10+
11+
await assert.rejects(
12+
import(fixtures.fileURL('es-modules', 'cjs-after-failed-import', 'entry.mjs')),
13+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' },
14+
);
15+
16+
const depPath = fixtures.path('es-modules', 'cjs-after-failed-import', 'dep.cjs');
17+
const replacement = new Module(depPath);
18+
replacement.exports = { name: 'replacement' };
19+
replacement.loaded = true;
20+
require.cache[depPath] = replacement;
21+
const { default: dep } = await import(fixtures.fileURL('es-modules', 'cjs-after-failed-import', 'importer.mjs'));
22+
assert.strictEqual(dep, replacement.exports);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { name: 'dep' };
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './importer.mjs';
2+
import './data.json';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import dep from './dep.cjs';
2+
export default dep;

0 commit comments

Comments
Β (0)