module: normalize package map paths

Signed-off-by: dansatch <dansatch98@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66054
Fixes: https://github.com/nodejs/node/issues/66043
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Hilary Asogwa authored and Antoine du Hamel committed 2026-09-28 23:54:48 +02:00
1 parent d84b4550e5
commit 9baea33509
6 files changed
+104 -1

No files matched your search

+1 -1
View File
@@ -105,7 +105,7 @@ class PackageMap {
let absolutePath;
try {
absolutePath = fileURLToPath(packageURL);
absolutePath = pathResolve(fileURLToPath(packageURL));
} catch (err) {
const error = new ERR_PACKAGE_MAP_INVALID(
this.#configPath,
@@ -27,6 +27,18 @@ spawnSyncAndAssert(process.execPath, [
trim: true,
});
// Directory-form URLs match package files even when the map is nested below them.
spawnSyncAndAssert(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/nested-project/map-dir/package-map-parent-url.json'),
'--input-type=module',
'--eval', `import dep from 'dep-a'; console.log(dep);`,
], { cwd: fixtures.path('package-map/nested-project/src') }, {
stdout: /dep-a-value/,
trim: true,
});
// URL fields are decoded as URLs before being converted to paths.
spawnSyncAndAssert(process.execPath, [
'--no-warnings',
@@ -0,0 +1,12 @@
{
"packages": {
"app": {
"url": "..",
"dependencies": {"dep-a": "dep-a"}
},
"dep-a": {
"url": "../../dep-a/",
"dependencies": {}
}
}
}
@@ -0,0 +1,12 @@
{
"packages": {
"root": {
"url": "./root/",
"dependencies": {"dep-a": "dep-a"}
},
"dep-a": {
"url": "./dep-a/",
"dependencies": {}
}
}
}
@@ -0,0 +1,16 @@
{
"packages": {
"root": {
"url": "./root",
"dependencies": {"dep-a": "dep-a"}
},
"dep-a": {
"url": "./dep-a",
"dependencies": {}
},
"dep-a-directory": {
"url": "./dep-a/",
"dependencies": {}
}
}
}
+51
View File
@@ -32,6 +32,40 @@ writeFileSync(fileUrlFixturePath, JSON.stringify({
describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PARALLEL }, () => {
describe('basic resolution', () => {
it('resolves dependencies from a parent directory URL', () => {
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/nested-project/map-dir/package-map-parent-url.json'),
'-e',
`const dep = require('dep-a'); console.log(dep.default);`,
], {
cwd: fixtures.path('package-map/nested-project/src'),
encoding: 'utf8',
});
assert.strictEqual(stderr, '');
assert.match(stdout, /dep-a-value/);
assert.strictEqual(status, 0, stderr);
});
it('resolves packages with directory-form URLs', () => {
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/package-map-directory-urls.json'),
'-e',
`const dep = require('dep-a'); console.log(dep.default);`,
], {
cwd: fixtures.path('package-map/root'),
encoding: 'utf8',
});
assert.strictEqual(stderr, '');
assert.match(stdout, /dep-a-value/);
assert.strictEqual(status, 0, stderr);
});
it('resolves require() through package map', () => {
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--no-warnings',
@@ -197,6 +231,23 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
assert.match(stderr, /pkg-b/);
assert.notStrictEqual(status, 0, stderr);
});
it('throws for package URLs differing only by a trailing separator', () => {
const { status, stderr } = spawnSync(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/package-map-duplicate-directory-path.json'),
'-e',
`require('dep-a');`,
], {
cwd: fixtures.path('package-map/root'),
encoding: 'utf8',
});
assert.match(stderr, /ERR_PACKAGE_MAP_INVALID/);
assert.match(stderr, /dep-a-directory/);
assert.notStrictEqual(status, 0, stderr);
});
});
describe('conditional exports', () => {