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: identify parent importing a url with invalid host #49736

Merged
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: 9 additions & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,15 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
fileURLToPath(base));
}

const path = fileURLToPath(resolved);
let path;
try {
path = fileURLToPath(resolved);
} catch (err) {
const { setOwnProperty } = require('internal/util');
setOwnProperty(err, 'input', `${resolved}`);
setOwnProperty(err, 'module', `${base}`);
throw err;
}

const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
StringPrototypeSlice(path, -1) : path));
Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-loader-default-resolver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ describe('default resolver', () => {
assert.strictEqual(stdout.trim(), 'index.byoe!');
assert.strictEqual(stderr, '');
});

it('should identify the parent module of an invalid URL host in import specifier', async () => {
if (process.platform === 'win32') return;

const { code, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules', 'invalid-posix-host.mjs'),
]);

assert.match(stderr, /ERR_INVALID_FILE_URL_HOST/);
assert.match(stderr, /file:\/\/hmm\.js/);
assert.match(stderr, /invalid-posix-host\.mjs/);
assert.strictEqual(code, 1);
});
});
1 change: 1 addition & 0 deletions test/fixtures/es-modules/invalid-posix-host.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "file://hmm.js";