Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d2921f

Browse filesBrowse files
JakobJingleheimerdanielleadams
authored andcommitted
esm: fix erroneous re-initialization of ESMLoader
PR-URL: #43763 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 42693aa commit 0d2921f
Copy full SHA for 0d2921f

File tree

Expand file treeCollapse file tree

3 files changed

+46
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/process/esm_loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/esm_loader.js
+9-1Lines 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) {
Collapse file
+30Lines changed: 30 additions & 0 deletions
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+
}
Collapse file
+7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.