Skip to content

Commit c9a96ae

Browse files
guybedfordtargos
authored andcommitted
esm: ensure cwd-relative imports for module --eval
PR-URL: #28389 Reviewed-By: Colin Ihrig <[email protected]>
1 parent b808484 commit c9a96ae

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

lib/internal/modules/esm/loader.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const {
99
ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK,
1010
ERR_UNKNOWN_MODULE_FORMAT
1111
} = require('internal/errors').codes;
12-
const { URL } = require('url');
12+
const {
13+
URL,
14+
pathToFileURL
15+
} = require('url');
1316
const { validateString } = require('internal/validators');
1417
const ModuleMap = require('internal/modules/esm/module_map');
1518
const ModuleJob = require('internal/modules/esm/module_job');
@@ -107,7 +110,10 @@ class Loader {
107110
return { url, format };
108111
}
109112

110-
async eval(source, url = `eval:${++this.evalIndex}`) {
113+
async eval(
114+
source,
115+
url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href
116+
) {
111117
const evalInstance = async (url) => {
112118
return {
113119
module: new ModuleWrap(source, url),

test/message/async_error_eval_esm.out

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Error: test
2-
at one (eval:1:2:9)
3-
at two (eval:1:15:9)
2+
at one (file:*/[eval1]:2:9)
3+
at two (file:*/[eval1]:15:9)
44
at processTicksAndRejections (internal/process/task_queues.js:*:*)
5-
at async three (eval:1:18:3)
6-
at async four (eval:1:22:3)
7-
at async main (eval:1:28:5)
5+
at async three (file:*/[eval1]:18:3)
6+
at async four (file:*/[eval1]:22:3)
7+
at async main (file:*/[eval1]:28:5)

test/parallel/test-cli-eval.js

+9
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,12 @@ child.exec(
274274
assert.ifError(err);
275275
assert.strictEqual(stdout, 'object\n');
276276
}));
277+
278+
// Assert that packages can be imported cwd-relative with --eval
279+
child.exec(
280+
`${nodejs} ${execOptions} ` +
281+
'--eval "import \'./test/fixtures/es-modules/mjs-file.mjs\'"',
282+
common.mustCall((err, stdout) => {
283+
assert.ifError(err);
284+
assert.strictEqual(stdout, '.mjs file\n');
285+
}));

0 commit comments

Comments
 (0)