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

loader: fix esm resolve for symlink file #42197

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,24 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
resolved.pathname, 'must not include encoded "/" or "\\" characters',
fileURLToPath(base));

const path = fileURLToPath(resolved);
let path = fileURLToPath(resolved);
if (getOptionValue('--experimental-specifier-resolution') === 'node') {
let file = resolveExtensionsWithTryExactName(resolved);
if (file !== undefined) return file;
if (!StringPrototypeEndsWith(path, '/')) {
file = resolveDirectoryEntry(new URL(`${resolved}/`));
if (file !== undefined) return file;
} else {
return resolveDirectoryEntry(resolved) || resolved;

// Directory
if (file === undefined) {
file = StringPrototypeEndsWith(path, '/') ?
(resolveDirectoryEntry(resolved) || resolved) : resolveDirectoryEntry(new URL(`${resolved}/`));

if (file === resolved) return file;

if (file === undefined) {
throw new ERR_MODULE_NOT_FOUND(
resolved.pathname, fileURLToPath(base), 'module');
}
}
throw new ERR_MODULE_NOT_FOUND(
resolved.pathname, fileURLToPath(base), 'module');

path = file;
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
}

const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
Expand Down
40 changes: 40 additions & 0 deletions test/es-module/test-esm-specifiers-symlink.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as common from '../common/index.mjs';
import path from 'path';
import fs from 'fs/promises';
import tmpdir from '../common/tmpdir.js';
import { spawn } from 'child_process';
import assert from 'assert';

tmpdir.refresh();
const tmpDir = tmpdir.path;

// Create the following file structure:
// ├── index.mjs
// ├── subfolder
// │ ├── index.mjs
// │ └── node_modules
// │ └── package-a
// │ └── index.mjs
// └── symlink.mjs -> ./subfolder/index.mjs
const entry = path.join(tmpDir, 'index.mjs');
const symlink = path.join(tmpDir, 'symlink.mjs');
const real = path.join(tmpDir, 'subfolder', 'index.mjs');
const packageDir = path.join(tmpDir, 'subfolder', 'node_modules', 'package-a');
const packageEntry = path.join(packageDir, 'index.mjs');
try {
await fs.symlink(real, symlink);
} catch (err) {
if (err.code !== 'EPERM') throw err;
common.skip('insufficient privileges for symlinks');
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
await fs.mkdir(packageDir, { recursive: true });
await Promise.all([
fs.writeFile(entry, 'import "./symlink.mjs";'),
fs.writeFile(real, 'export { a } from "package-a/index.mjs"'),
fs.writeFile(packageEntry, 'export const a = 1;'),
]);

spawn(process.execPath, ['--experimental-specifier-resolution=node', entry],
{ stdio: 'inherit' }).on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));