Skip to content

Commit 645b788

Browse files
esm: identify parent importing a url with invalid host
PR-URL: #49736 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 0179989 commit 645b788

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/internal/modules/esm/resolve.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,15 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
231231
fileURLToPath(base));
232232
}
233233

234-
const path = fileURLToPath(resolved);
234+
let path;
235+
try {
236+
path = fileURLToPath(resolved);
237+
} catch (err) {
238+
const { setOwnProperty } = require('internal/util');
239+
setOwnProperty(err, 'input', `${resolved}`);
240+
setOwnProperty(err, 'module', `${base}`);
241+
throw err;
242+
}
235243

236244
const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
237245
StringPrototypeSlice(path, -1) : path));

test/es-module/test-esm-loader-default-resolver.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ describe('default resolver', () => {
4949
assert.strictEqual(stdout.trim(), 'index.byoe!');
5050
assert.strictEqual(stderr, '');
5151
});
52+
53+
it('should identify the parent module of an invalid URL host in import specifier', async () => {
54+
if (process.platform === 'win32') return;
55+
56+
const { code, stderr } = await spawnPromisified(execPath, [
57+
'--no-warnings',
58+
fixtures.path('es-modules', 'invalid-posix-host.mjs'),
59+
]);
60+
61+
assert.match(stderr, /ERR_INVALID_FILE_URL_HOST/);
62+
assert.match(stderr, /file:\/\/hmm\.js/);
63+
assert.match(stderr, /invalid-posix-host\.mjs/);
64+
assert.strictEqual(code, 1);
65+
});
5266
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "file://hmm.js";

0 commit comments

Comments
 (0)