Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a00464e

Browse filesBrowse files
znewshamruyadorno
authored andcommitted
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
Copy full SHA for a00464e

File tree

Expand file treeCollapse file tree

2 files changed

+35
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-2
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+4-2Lines changed: 4 additions & 2 deletions
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, '/') ?
Collapse file

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

Copy file name to clipboardExpand all lines: test/es-module/test-esm-specifiers.mjs
+31Lines changed: 31 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.