Skip to content

Commit 0d2921f

Browse files
JakobJingleheimerdanielleadams
authored andcommitted
esm: fix erroneous re-initialization of ESMLoader
PR-URL: #43763 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
1 parent 42693aa commit 0d2921f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

lib/internal/process/esm_loader.js

+9-1
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) {
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import { spawnSync } from 'node:child_process';
5+
6+
7+
{ // Verify unadulterated source is loaded when there are no loaders
8+
const { status, stderr, stdout } = spawnSync(
9+
process.execPath,
10+
[
11+
'--loader',
12+
fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'),
13+
'--no-warnings',
14+
fixtures.path('es-modules', 'runmain.mjs'),
15+
],
16+
{ encoding: 'utf8' },
17+
);
18+
19+
// Length minus 1 because the first match is the needle.
20+
const resolveHookRunCount = (stdout.match(/resolve passthru/g)?.length ?? 0) - 1;
21+
22+
assert.strictEqual(stderr, '');
23+
/**
24+
* resolveHookRunCount = 2:
25+
* 1. fixtures/…/runmain.mjs
26+
* 2. node:module (imported by fixtures/…/runmain.mjs)
27+
*/
28+
assert.strictEqual(resolveHookRunCount, 2);
29+
assert.strictEqual(status, 0);
30+
}

test/fixtures/es-modules/runmain.mjs

+7
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)