From 847932edd2d3382d189debdbc8212bf8e042274b Mon Sep 17 00:00:00 2001 From: Zack Newsham Date: Sat, 22 Apr 2023 14:03:13 -0400 Subject: [PATCH 1/4] ensure --experimental-specifier-resolution=node works with --preserve-symlinks --- lib/internal/modules/esm/resolve.js | 3 ++- test/es-module/test-esm-specifiers.mjs | 31 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index fd7d2feada9bbb..fb730c3689ff64 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -310,7 +310,8 @@ function finalizeResolution(resolved, base, preserveSymlinks) { } } - path = file; + resolved = file; + path = fileURLToPath(resolved); } const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ? diff --git a/test/es-module/test-esm-specifiers.mjs b/test/es-module/test-esm-specifiers.mjs index 692db968141997..a9419e90ced097 100644 --- a/test/es-module/test-esm-specifiers.mjs +++ b/test/es-module/test-esm-specifiers.mjs @@ -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, [ From eb8bb8d8957ed30749bbb06656886b0d65673f7a Mon Sep 17 00:00:00 2001 From: Zack Newsham Date: Sat, 22 Apr 2023 17:35:21 -0400 Subject: [PATCH 2/4] add comment explaining reassignment of resolve --- lib/internal/modules/esm/resolve.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index fb730c3689ff64..4632eee6bb82e6 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -309,7 +309,8 @@ function finalizeResolution(resolved, base, preserveSymlinks) { resolved.pathname, fileURLToPath(base), 'module'); } } - + // if preserveSymlinks is false resolved is returned and path + // is used only to check that the resolved path exists resolved = file; path = fileURLToPath(resolved); } From e7479af7fbe728d804fa6f47c474e890848016af Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 23 Apr 2023 09:48:01 -0400 Subject: [PATCH 3/4] Update lib/internal/modules/esm/resolve.js Co-authored-by: Antoine du Hamel --- lib/internal/modules/esm/resolve.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 4632eee6bb82e6..a0c48d4ba17335 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -310,7 +310,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) { } } // if preserveSymlinks is false resolved is returned and path - // is used only to check that the resolved path exists + // is used only to check that the resolved path exists. resolved = file; path = fileURLToPath(resolved); } From 46f027e9858f40e85f0c2a036218ad9b8edd25ea Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 23 Apr 2023 09:48:10 -0400 Subject: [PATCH 4/4] Update lib/internal/modules/esm/resolve.js Co-authored-by: Antoine du Hamel --- lib/internal/modules/esm/resolve.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index a0c48d4ba17335..3b3688df30d091 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -309,7 +309,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) { resolved.pathname, fileURLToPath(base), 'module'); } } - // if preserveSymlinks is false resolved is returned and path + // If `preserveSymlinks` is false, `resolved` is returned and `path` // is used only to check that the resolved path exists. resolved = file; path = fileURLToPath(resolved);