Skip to content

Commit

Permalink
loader: fix esm resolve for symlink file
Browse files Browse the repository at this point in the history
Fix: #42195

PR-URL: #42197
Fixes: #42195
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
meixg authored and sxa committed Mar 7, 2022
1 parent 05c3ff5 commit 7f1c83e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
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;
}

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');
}
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);
}));

0 comments on commit 7f1c83e

Please sign in to comment.