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 d7972b7

Browse filesBrowse files
coreyfarrellMylesBorins
authored andcommitted
repl: fix referrer for dynamic import
The ESM loader does not accept a directory as the referrer, it requires a path within the directory. Add `/repl` to ensure relative dynamic imports can succeed. Fixes: #19570 PR-URL: #30609 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Coe <bencoe@gmail.com>
1 parent 036a982 commit d7972b7
Copy full SHA for d7972b7

File tree

Expand file treeCollapse file tree

2 files changed

+34
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-3
lines changed
Open diff view settings
Collapse file

‎lib/repl.js‎

Copy file name to clipboardExpand all lines: lib/repl.js
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,12 @@ function REPLServer(prompt,
324324
if (code === '\n')
325325
return cb(null);
326326

327-
let pwd;
327+
let parentURL;
328328
try {
329329
const { pathToFileURL } = require('url');
330-
pwd = pathToFileURL(process.cwd()).href;
330+
// Adding `/repl` prevents dynamic imports from loading relative
331+
// to the parent of `process.cwd()`.
332+
parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href;
331333
} catch {
332334
}
333335
while (true) {
@@ -342,7 +344,7 @@ function REPLServer(prompt,
342344
filename: file,
343345
displayErrors: true,
344346
importModuleDynamically: experimentalModules ? async (specifier) => {
345-
return asyncESM.ESMLoader.import(specifier, pwd);
347+
return asyncESM.ESMLoader.import(specifier, parentURL);
346348
} : undefined
347349
});
348350
} catch (e) {
Collapse file
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cp = require('child_process');
5+
const fixtures = require('../common/fixtures');
6+
7+
const args = [
8+
'--interactive',
9+
'--experimental-repl-await',
10+
'--experimental-modules'
11+
];
12+
13+
const opts = { cwd: fixtures.path('es-modules') };
14+
const child = cp.spawn(process.execPath, args, opts);
15+
16+
let output = '';
17+
child.stdout.setEncoding('utf8');
18+
child.stdout.on('data', (data) => {
19+
output += data;
20+
});
21+
22+
child.on('exit', common.mustCall(() => {
23+
const results = output.replace(/^> /mg, '').split('\n').slice(2);
24+
assert.deepStrictEqual(results, ['[Module] { message: \'A message\' }', '']);
25+
}));
26+
27+
child.stdin.write('await import(\'./message.mjs\');\n');
28+
child.stdin.write('.exit');
29+
child.stdin.end();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.