Skip to content

Commit b4a7a3f

Browse files
esm: fix erroneous re-initialization of ESMLoader
1 parent a933a75 commit b4a7a3f

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

lib/internal/process/esm_loader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
4040
};
4141

4242
const esmLoader = new ESMLoader();
43-
4443
exports.esmLoader = esmLoader;
4544

45+
// Module.runMain() causes loadESM() to re-run (which it should do); however, this should NOT cause
46+
// ESM to be re-initialised; doing so causes duplicate custom loaders to be added to the public
47+
// esmLoader.
48+
let isESMInitialized = false;
49+
4650
/**
4751
* Causes side-effects: user-defined loader hooks are added to esmLoader.
4852
* @returns {void}
4953
*/
5054
async function initializeLoader() {
55+
if (isESMInitialized) { return; }
56+
5157
const { getOptionValue } = require('internal/options');
5258
const customLoaders = getOptionValue('--experimental-loader');
5359

@@ -75,6 +81,8 @@ async function initializeLoader() {
7581
// Hooks must then be added to external/public loader
7682
// (so they're triggered in userland)
7783
await esmLoader.addCustomLoaders(keyedExportsList);
84+
85+
isESMInitialized = true;
7886
}
7987

8088
exports.loadESM = async function loadESM(callback) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import '../common/index.mjs';
2+
import fixtures from '../common/fixtures.js';
3+
import assert from 'node:assert';
4+
import { spawnSync } from 'node:child_process';
5+
import { fileURLToPath } from 'node:url';
6+
7+
8+
{ // Verify unadulterated source is loaded when there are no loaders
9+
const { status, stderr, stdout } = spawnSync(
10+
process.execPath,
11+
[
12+
'--loader',
13+
fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'),
14+
'--no-warnings',
15+
fileURLToPath(fixtures.fileURL('es-modules', 'runmain.mjs')),
16+
],
17+
{ encoding: 'utf8' },
18+
);
19+
20+
const resolveHookRunCount = [...(stdout.matchAll(/resolve passthru/g) ?? new Array() )]
21+
.length - 1; // less 1 because the first is the needle
22+
23+
assert.strictEqual(stderr, '');
24+
/**
25+
* resolveHookRunCount = 2:
26+
* 1. fixtures/…/runmain.mjs
27+
* 2. node:module (imported by fixtures/…/runmain.mjs)
28+
*/
29+
assert.strictEqual(resolveHookRunCount, 2);
30+
assert.strictEqual(status, 0);
31+
}

test/fixtures/es-modules/runmain.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { runMain } from 'node:module';
2+
3+
try { await import.meta.resolve('doesnt-matter.mjs') } catch {}
4+
5+
runMain();
6+
7+
try { await import.meta.resolve('doesnt-matter.mjs') } catch {}

0 commit comments

Comments
 (0)