Skip to content

Commit a00464e

Browse files
znewshamruyadorno
authored andcommittedAug 17, 2023
esm: fix specifier resolution and symlinks
Ensure `--experimental-specifier-resolution=node` works when combined with `--preserve-symlinks`. PR-URL: #47674 Refs: #47649 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 3eeca52 commit a00464e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
 

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,10 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
307307
resolved.pathname, fileURLToPath(base), 'module');
308308
}
309309
}
310-
311-
path = file;
310+
// If `preserveSymlinks` is false, `resolved` is returned and `path`
311+
// is used only to check that the resolved path exists.
312+
resolved = file;
313+
path = fileURLToPath(resolved);
312314
}
313315

314316
const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?

‎test/es-module/test-esm-specifiers.mjs

+31
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,37 @@ describe('ESM: specifier-resolution=node', { concurrency: true }, () => {
3636
strictEqual(stdout, '');
3737
strictEqual(code, 0);
3838
});
39+
it('should work with --preserve-symlinks', async () => {
40+
const { code, stderr, stdout } = await spawnPromisified(execPath, [
41+
'--no-warnings',
42+
'--experimental-specifier-resolution=node',
43+
'--preserve-symlinks',
44+
'--input-type=module',
45+
'--eval',
46+
[
47+
'import { strictEqual } from "node:assert";',
48+
// CommonJS index.js
49+
`import commonjs from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-commonjs'))};`,
50+
// ESM index.js
51+
`import module from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-module'))};`,
52+
// Directory entry with main.js
53+
`import main from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/dir-with-main'))};`,
54+
// Notice the trailing slash
55+
`import success, { explicit, implicit, implicitModule } from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/'))};`,
56+
'strictEqual(commonjs, "commonjs");',
57+
'strictEqual(module, "module");',
58+
'strictEqual(main, "main");',
59+
'strictEqual(success, "success");',
60+
'strictEqual(explicit, "esm");',
61+
'strictEqual(implicit, "cjs");',
62+
'strictEqual(implicitModule, "cjs");',
63+
].join('\n'),
64+
]);
65+
66+
strictEqual(stderr, '');
67+
strictEqual(stdout, '');
68+
strictEqual(code, 0);
69+
});
3970

4071
it('should throw when the file doesn\'t exist', async () => {
4172
const { code, stderr, stdout } = await spawnPromisified(execPath, [

0 commit comments

Comments
 (0)
Please sign in to comment.