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: fix imports from non-file module #42881

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 3 additions & 5 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,6 @@ function resolveAsCommonJS(specifier, parentURL) {
// TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed`
function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
if (parsedParentURL) {
const parentURL = fileURLToPath(parsedParentURL?.href);

Comment on lines -1018 to -1019
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a reason for this (I'm pretty sure not doing this will break something), but I can't remember what it was. @bmeck do you remember?

Copy link
Member

@JakobJingleheimer JakobJingleheimer Apr 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH! I partially remember! It's when something is wrong with parsedParentURL—that's why it uses ?.. Without this, the error message was too confusing, like " is not allowed" (where there should be something at the front). Adding it provided an important clue to what is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're checking that parsedParentURL is truthy the line just before that, I don't think the ?. could ever take effect. Arguably it's fixing a bigger issue than a confusing error message, I'd be inclined to land this anyway. We can always improve things later if the original problem comes back. wdyt?

if (
parsedParentURL.protocol === 'http:' ||
parsedParentURL.protocol === 'https:'
Expand All @@ -1030,7 +1028,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
) {
throw new ERR_NETWORK_IMPORT_DISALLOWED(
specifier,
parentURL,
parsedParentURL,
'remote imports cannot import from a local location.'
);
}
Expand All @@ -1041,14 +1039,14 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
NativeModule.canBeRequiredWithoutScheme(specifier)) {
throw new ERR_NETWORK_IMPORT_DISALLOWED(
specifier,
parentURL,
parsedParentURL,
'remote imports cannot import from a local location.'
);
}

throw new ERR_NETWORK_IMPORT_DISALLOWED(
specifier,
parentURL,
parsedParentURL,
'only relative and absolute specifiers are supported.'
);
}
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-data-urls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
function createURL(mime, body) {
return `data:${mime},${body}`;
Expand Down Expand Up @@ -107,4 +108,8 @@ function createBase64URL(mime, body) {
const module = await import(plainESMURL);
assert.strictEqual(module.default, 2);
}
{
const plainESMURL = `data:text/javascript,${encodeURIComponent(`import ${JSON.stringify(fixtures.fileURL('es-module-url', 'empty.js'))}`)}`;
await import(plainESMURL);
}
})().then(common.mustCall());