Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: ensure cwd-relative imports for module --eval #28389

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const {
ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK,
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
const { URL } = require('url');
const {
URL,
pathToFileURL
} = require('url');
const { validateString } = require('internal/validators');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');
Expand Down Expand Up @@ -107,7 +110,10 @@ class Loader {
return { url, format };
}

async eval(source, url = `eval:${++this.evalIndex}`) {
async eval(
source,
url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href
) {
const evalInstance = async (url) => {
return {
module: new ModuleWrap(source, url),
Expand Down
10 changes: 5 additions & 5 deletions test/message/async_error_eval_esm.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Error: test
at one (eval:1:2:9)
at two (eval:1:15:9)
at one (file:*/[eval1]:2:9)
at two (file:*/[eval1]:15:9)
at processTicksAndRejections (internal/process/task_queues.js:*:*)
at async three (eval:1:18:3)
at async four (eval:1:22:3)
at async main (eval:1:28:5)
at async three (file:*/[eval1]:18:3)
at async four (file:*/[eval1]:22:3)
at async main (file:*/[eval1]:28:5)
9 changes: 9 additions & 0 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,12 @@ child.exec(
assert.ifError(err);
assert.strictEqual(stdout, 'object\n');
}));

// Assert that packages can be imported cwd-relative with --eval
child.exec(
`${nodejs} ${execOptions} ` +
'--eval "import \'./test/fixtures/es-modules/mjs-file.mjs\'"',
common.mustCall((err, stdout) => {
assert.ifError(err);
assert.strictEqual(stdout, '.mjs file\n');
}));