Skip to content

Commit

Permalink
esm: fix specifier resolution and symlinks
Browse files Browse the repository at this point in the history
Ensure `--experimental-specifier-resolution=node` works when combined
with `--preserve-symlinks`.

PR-URL: #47674
Refs: #47649
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
znewsham authored and ruyadorno committed Aug 17, 2023
1 parent 3eeca52 commit a00464e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/modules/esm/resolve.js
Expand Up @@ -307,8 +307,10 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
resolved.pathname, fileURLToPath(base), 'module');
}
}

path = file;
// If `preserveSymlinks` is false, `resolved` is returned and `path`
// is used only to check that the resolved path exists.
resolved = file;
path = fileURLToPath(resolved);
}

const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
Expand Down
31 changes: 31 additions & 0 deletions test/es-module/test-esm-specifiers.mjs
Expand Up @@ -36,6 +36,37 @@ describe('ESM: specifier-resolution=node', { concurrency: true }, () => {
strictEqual(stdout, '');
strictEqual(code, 0);
});
it('should work with --preserve-symlinks', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-specifier-resolution=node',
'--preserve-symlinks',
'--input-type=module',
'--eval',
[
'import { strictEqual } from "node:assert";',
// CommonJS index.js
`import commonjs from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-commonjs'))};`,
// ESM index.js
`import module from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/package-type-module'))};`,
// Directory entry with main.js
`import main from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/dir-with-main'))};`,
// Notice the trailing slash
`import success, { explicit, implicit, implicitModule } from ${JSON.stringify(fixtures.fileURL('es-module-specifiers/'))};`,
'strictEqual(commonjs, "commonjs");',
'strictEqual(module, "module");',
'strictEqual(main, "main");',
'strictEqual(success, "success");',
'strictEqual(explicit, "esm");',
'strictEqual(implicit, "cjs");',
'strictEqual(implicitModule, "cjs");',
].join('\n'),
]);

strictEqual(stderr, '');
strictEqual(stdout, '');
strictEqual(code, 0);
});

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

0 comments on commit a00464e

Please sign in to comment.