Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x] esm: fix --specifier-resolution=node not preserving symlinks #47674

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,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);
Comment on lines +314 to +315
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you reassigning resolved? That variable is used again farther down in this function.

Suggested change
resolved = file;
path = fileURLToPath(resolved);
path = fileURLToPath(file);

I’m still wary of this change. As it modifies variables that are used outside of the if (getOptionValue('--experimental-specifier-resolution') === 'node') scope, it could have unintended consequences for anyone passing that flag. Those unintended consequences would be further bugs imposed upon the modules team to fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is that resolved is not reassigned when it should be. If preseveSymlinks is passed in the function just returns resolved and never (meaningfully) looks at path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please add some comments explaining this. The code by itself looks like a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added - but in fairness, the !preserveSymlinks block below also reassigns to resovled.

}

const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
Expand Down
31 changes: 31 additions & 0 deletions test/es-module/test-esm-specifiers.mjs
Original file line number Diff line number Diff line change
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