Skip to content

Commit c8da4ec

Browse files
authored
loader: enforce path normalization before lookup
Signed-off-by: Maël Nison <nison.mael@gmail.com> PR-URL: #63917 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent eb7ee32 commit c8da4ec

2 files changed

Lines changed: 18 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
300300
describe('longest path wins', () => {
301301
const longestPathMap = fixtures.path('package-map/package-map-longest-path.json');
302302

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

0 commit comments

Comments
 (0)