Skip to content

Commit 86cb697

Browse files
aduh95marco-ippolito
authored andcommittedJan 24, 2025
esm: add a fallback when importer in not a file
PR-URL: #55471 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
1 parent 274d0b4 commit 86cb697

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
 

‎lib/internal/modules/esm/resolve.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,15 @@ const encodedSepRegEx = /%2F|%5C/i;
236236
*/
237237
function finalizeResolution(resolved, base, preserveSymlinks) {
238238
if (RegExpPrototypeExec(encodedSepRegEx, resolved.pathname) !== null) {
239+
let basePath;
240+
try {
241+
basePath = fileURLToPath(base);
242+
} catch {
243+
basePath = base;
244+
}
239245
throw new ERR_INVALID_MODULE_SPECIFIER(
240246
resolved.pathname, 'must not include encoded "/" or "\\" characters',
241-
fileURLToPath(base));
247+
basePath);
242248
}
243249

244250
let path;
@@ -256,14 +262,26 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
256262

257263
// Check for stats.isDirectory()
258264
if (stats === 1) {
259-
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
265+
let basePath;
266+
try {
267+
basePath = fileURLToPath(base);
268+
} catch {
269+
basePath = base;
270+
}
271+
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, basePath, String(resolved));
260272
} else if (stats !== 0) {
261273
// Check for !stats.isFile()
262274
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
263275
process.send({ 'watch:require': [path || resolved.pathname] });
264276
}
277+
let basePath;
278+
try {
279+
basePath = fileURLToPath(base);
280+
} catch {
281+
basePath = base;
282+
}
265283
throw new ERR_MODULE_NOT_FOUND(
266-
path || resolved.pathname, base && fileURLToPath(base), resolved);
284+
path || resolved.pathname, basePath, resolved);
267285
}
268286

269287
if (!preserveSymlinks) {

‎test/es-module/test-esm-main-lookup.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ await assert.rejects(import('../fixtures/es-modules/pjson-main'), {
1515
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
1616
url: fixtures.fileURL('es-modules/pjson-main').href,
1717
});
18+
await assert.rejects(import(`data:text/javascript,import${encodeURIComponent(JSON.stringify(fixtures.fileURL('es-modules/pjson-main')))}`), {
19+
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
20+
url: fixtures.fileURL('es-modules/pjson-main').href,
21+
});
22+
await assert.rejects(import(`data:text/javascript,import${encodeURIComponent(JSON.stringify(fixtures.fileURL('es-modules/does-not-exist')))}`), {
23+
code: 'ERR_MODULE_NOT_FOUND',
24+
url: fixtures.fileURL('es-modules/does-not-exist').href,
25+
});
1826

1927
assert.deepStrictEqual(
2028
{ ...await import('../fixtures/es-modules/pjson-main/main.mjs') },

0 commit comments

Comments
 (0)
Please sign in to comment.