Skip to content

Commit 2811e40

Browse files
committed
loader: enforce path normalization before lookup
Signed-off-by: Maël Nison <nison.mael@gmail.com>
1 parent 120c2a4 commit 2811e40

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/internal/modules/package_map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class PackageMap {
140140
* @returns {string|null}
141141
*/
142142
#getKeyForPath(filePath) {
143+
filePath = pathResolve(filePath);
144+
143145
const cached = this.#pathToKeyCache.get(filePath);
144146
if (cached !== undefined) { return cached; }
145147

test/parallel/test-require-package-map.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
7979
assert.match(stdout, /dep-b using dep-a-value/);
8080
assert.strictEqual(status, 0, stderr);
8181
});
82+
8283
});
8384

8485
describe('resolution boundaries', () => {
@@ -300,6 +301,22 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
300301
describe('longest path wins', () => {
301302
const longestPathMap = fixtures.path('package-map/package-map-longest-path.json');
302303

304+
it('resolves from createRequire() paths with mixed separators', () => {
305+
const { status, stdout, stderr } = spawnSync(process.execPath, [
306+
'--no-warnings',
307+
'--experimental-package-map', longestPathMap,
308+
'-e',
309+
`const { createRequire } = require('node:module'); const req = createRequire(process.cwd() + '/node_modules/inner/index.js'); const dep = req('dep-a'); console.log(dep.default);`,
310+
], {
311+
cwd: fixtures.path('package-map/root'),
312+
encoding: 'utf8',
313+
});
314+
315+
assert.strictEqual(stderr, '');
316+
assert.match(stdout, /dep-a-value/);
317+
assert.strictEqual(status, 0, stderr);
318+
});
319+
303320
it('resolves nested package using its own dependencies, not the parent', () => {
304321
// Inner lives at ./root/node_modules/inner which is inside root's
305322
// path (./root). The longest matching path should win, so code in

0 commit comments

Comments
 (0)