From 77d5b178d94ec385316ece88899509aa67262aa7 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 27 Aug 2025 02:07:27 +0200 Subject: [PATCH] fix(lockfile-to-pnp): ensure packageLocation ends in trailing slash (#9905) Discovered via https://github.com/pnpm/pnpm/issues/9904. PnP expects `packageLocation` to end in a trailing slash, this commit ensures that. --- .changeset/good-llamas-dream.md | 7 +++++++ lockfile/lockfile-to-pnp/src/index.ts | 3 +++ lockfile/lockfile-to-pnp/test/index.ts | 12 ++++++------ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .changeset/good-llamas-dream.md diff --git a/.changeset/good-llamas-dream.md b/.changeset/good-llamas-dream.md new file mode 100644 index 0000000000..0236f50f01 --- /dev/null +++ b/.changeset/good-llamas-dream.md @@ -0,0 +1,7 @@ +--- +"@pnpm/lockfile-to-pnp": patch +"pnpm": patch +--- + +Fix `.pnp.cjs` crash when importing subpath [#9904](https://github.com/pnpm/pnpm/issues/9904). + diff --git a/lockfile/lockfile-to-pnp/src/index.ts b/lockfile/lockfile-to-pnp/src/index.ts index bbac528eb7..5cdda38142 100644 --- a/lockfile/lockfile-to-pnp/src/index.ts +++ b/lockfile/lockfile-to-pnp/src/index.ts @@ -95,6 +95,9 @@ export function lockfileToPackageRegistry ( if (!packageLocation.startsWith('../')) { packageLocation = `./${packageLocation}` } + if (!packageLocation.endsWith('/')) { + packageLocation += '/' + } packageStore.set(pnpVersion, { packageDependencies: new Map([ [name, pnpVersion], diff --git a/lockfile/lockfile-to-pnp/test/index.ts b/lockfile/lockfile-to-pnp/test/index.ts index 904a354419..78f8946eb6 100644 --- a/lockfile/lockfile-to-pnp/test/index.ts +++ b/lockfile/lockfile-to-pnp/test/index.ts @@ -129,7 +129,7 @@ test('lockfileToPackageRegistry', () => { ['dep1', '1.0.0'], ['dep2', ['foo', '2.0.0']], ], - packageLocation: './node_modules/.pnpm/dep1@1.0.0/node_modules/dep1', + packageLocation: './node_modules/.pnpm/dep1@1.0.0/node_modules/dep1/', }, ], ], @@ -144,7 +144,7 @@ test('lockfileToPackageRegistry', () => { ['foo', '2.0.0'], ['qar', '3.0.0'], ], - packageLocation: './node_modules/.pnpm/foo@2.0.0/node_modules/foo', + packageLocation: './node_modules/.pnpm/foo@2.0.0/node_modules/foo/', }, ], ], @@ -158,7 +158,7 @@ test('lockfileToPackageRegistry', () => { packageDependencies: [ ['qar', '2.0.0'], ], - packageLocation: './node_modules/.pnpm/qar@2.0.0/node_modules/qar', + packageLocation: './node_modules/.pnpm/qar@2.0.0/node_modules/qar/', }, ], [ @@ -167,7 +167,7 @@ test('lockfileToPackageRegistry', () => { packageDependencies: [ ['qar', '3.0.0'], ], - packageLocation: './node_modules/.pnpm/qar@3.0.0/node_modules/qar', + packageLocation: './node_modules/.pnpm/qar@3.0.0/node_modules/qar/', }, ], ], @@ -265,7 +265,7 @@ test('lockfileToPackageRegistry packages that have peer deps', () => { ['haspeer', 'virtual:2.0.0(peer@1.0.0)#2.0.0'], ['peer', '1.0.0'], ], - packageLocation: './node_modules/.pnpm/haspeer@2.0.0_peer@1.0.0/node_modules/haspeer', + packageLocation: './node_modules/.pnpm/haspeer@2.0.0_peer@1.0.0/node_modules/haspeer/', }, ], ], @@ -279,7 +279,7 @@ test('lockfileToPackageRegistry packages that have peer deps', () => { packageDependencies: [ ['peer', '1.0.0'], ], - packageLocation: './node_modules/.pnpm/peer@1.0.0/node_modules/peer', + packageLocation: './node_modules/.pnpm/peer@1.0.0/node_modules/peer/', }, ], ],