Skip to content

Commit 705c100

Browse files
committed
fix(pnpm): preserve package names in lockfile scans
Assisted-by: Codex:GPT-6-Luna
1 parent ac4da89 commit 705c100

9 files changed

Lines changed: 107 additions & 16 deletions

‎.config/vitest.config.mts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { fileURLToPath } from 'node:url'
22

3-
import { defineConfig } from 'vitest/config'
3+
import type { UserConfig } from 'vitest/config'
44

55
import { resolveSnapshotPath } from './snapshot-path.mts'
66

7-
export default defineConfig({
7+
export default {
88
root: fileURLToPath(new URL('../', import.meta.url)),
99
resolve: {
1010
preserveSymlinks: false,
@@ -59,4 +59,4 @@ export default defineConfig({
5959
],
6060
},
6161
},
62-
})
62+
} satisfies UserConfig

‎.config/vitest.e2e.config.mts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { fileURLToPath } from 'node:url'
22

3-
import { defineConfig } from 'vitest/config'
3+
import type { UserConfig } from 'vitest/config'
44

55
import { resolveSnapshotPath } from './snapshot-path.mts'
66

7-
export default defineConfig({
7+
export default {
88
root: fileURLToPath(new URL('../', import.meta.url)),
99
resolve: {
1010
preserveSymlinks: false,
@@ -37,4 +37,4 @@ export default defineConfig({
3737
],
3838
},
3939
},
40-
})
40+
} satisfies UserConfig

‎.config/vitest.integration.config.mts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { fileURLToPath } from 'node:url'
22

3-
import { defineConfig } from 'vitest/config'
3+
import type { UserConfig } from 'vitest/config'
44

55
import { resolveSnapshotPath } from './snapshot-path.mts'
66

7-
export default defineConfig({
7+
export default {
88
root: fileURLToPath(new URL('../', import.meta.url)),
99
resolve: {
1010
preserveSymlinks: false,
@@ -29,4 +29,4 @@ export default defineConfig({
2929
'test/fixtures/**',
3030
],
3131
},
32-
})
32+
} satisfies UserConfig

‎.config/vitest.native.config.mts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { fileURLToPath } from 'node:url'
22

3-
import { defineConfig } from 'vitest/config'
3+
import type { UserConfig } from 'vitest/config'
44

55
import { resolveSnapshotPath } from './snapshot-path.mts'
66

7-
export default defineConfig({
7+
export default {
88
root: fileURLToPath(new URL('../', import.meta.url)),
99
resolve: {
1010
preserveSymlinks: false,
@@ -32,4 +32,4 @@ export default defineConfig({
3232
'test/fixtures/**',
3333
],
3434
},
35-
})
35+
} satisfies UserConfig

‎.config/vitest.terminal.config.mts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { fileURLToPath } from 'node:url'
22

3-
import { defineConfig } from 'vitest/config'
3+
import type { UserConfig } from 'vitest/config'
44

55
import { resolveSnapshotPath } from './snapshot-path.mts'
66

7-
export default defineConfig({
7+
export default {
88
root: fileURLToPath(new URL('../', import.meta.url)),
99
resolve: { preserveSymlinks: false },
1010
test: {
@@ -23,4 +23,4 @@ export default defineConfig({
2323
],
2424
exclude: ['**/node_modules/**', '**/dist/**', '.claude/worktrees/**'],
2525
},
26-
})
26+
} satisfies UserConfig

‎.github/workflows/ci.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ jobs:
233233
Expand-Archive $archivePath $tools
234234
(Join-Path $tools "node-v$nodeVersion-win-x64") | Add-Content $env:GITHUB_PATH
235235
236+
- name: Disable pnpm global virtual store • Windows
237+
if: runner.os == 'Windows'
238+
shell: pwsh
239+
run: |
240+
'pnpm_config_enable_global_virtual_store=false' | Add-Content $env:GITHUB_ENV
241+
'npm_config_enable_global_virtual_store=false' | Add-Content $env:GITHUB_ENV
242+
236243
- &test-package-managers
237244
name: Install pinned package managers
238245
shell: bash

‎src/utils/pnpm-scanning.test.mts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ packages:
105105
expect(purls).toContain('pkg:npm/@types/node@20.11.19')
106106
})
107107

108+
it('preserves underscores in package names while removing peer suffixes', async () => {
109+
const lockfile = parsePnpmLockfile(`lockfileVersion: '9.0'
110+
packages:
111+
my_package@1.2.3_peer@5.0.0: {}
112+
my_pkg@1.2.4(@scope/peer@2.0.0): {}
113+
'@scope/my_package@2.3.4(peer@5.0.0)': {}
114+
'@scope/my_pkg@2.3.5(@other/peer@2.0.0)': {}
115+
my_package@1.2.6: {}
116+
/legacy_package/3.4.5_peer@5.0.0: {}
117+
/@legacy/scoped_pkg/4.5.6_@peer_scope/name@1.0.0: {}
118+
'@versionless/name': {}`)
119+
120+
expect(lockfile).toBeTruthy()
121+
expect(await extractPurlsFromPnpmLockfile(lockfile!)).toEqual([
122+
'pkg:npm/my_package@1.2.3',
123+
'pkg:npm/my_pkg@1.2.4',
124+
'pkg:npm/@scope/my_package@2.3.4',
125+
'pkg:npm/@scope/my_pkg@2.3.5',
126+
'pkg:npm/my_package@1.2.6',
127+
'pkg:npm/legacy_package@3.4.5',
128+
'pkg:npm/@legacy/scoped_pkg@4.5.6',
129+
'pkg:npm/@versionless/name',
130+
])
131+
})
132+
108133
it('should scan the project graph from a split pnpm lockfile', async () => {
109134
const lockfileContent = `---
110135
lockfileVersion: '9.0'

‎src/utils/pnpm.mts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,42 @@ export async function extractPurlsFromPnpmLockfile(
4949
visit(pkgPath)
5050
}
5151
return Array.from(seen).map(p =>
52-
idToNpmPurl(stripPnpmPeerSuffix(stripLeadingPnpmDepPathSlash(p))),
52+
idToNpmPurl(
53+
stripPnpmPeerSuffixFromDepPath(stripLeadingPnpmDepPathSlash(p)),
54+
),
5355
)
5456
}
5557

58+
function stripPnpmPeerSuffixFromDepPath(depPath: string): string {
59+
// Modern pnpm paths use name@version; older lockfiles used name/version.
60+
// Peer suffix delimiters belong to the version component, since npm names
61+
// themselves may contain underscores.
62+
const scoped = depPath.startsWith('@')
63+
const nameSlash = scoped ? depPath.indexOf('/') : -1
64+
const atIndex = scoped
65+
? depPath.indexOf('@', nameSlash + 1)
66+
: depPath.indexOf('@')
67+
const legacySlash = scoped
68+
? depPath.indexOf('/', nameSlash + 1)
69+
: depPath.indexOf('/')
70+
if (atIndex !== -1 && (legacySlash === -1 || atIndex < legacySlash)) {
71+
return `${depPath.slice(0, atIndex + 1)}${stripPnpmPeerSuffix(depPath.slice(atIndex + 1))}`
72+
}
73+
74+
// Legacy pnpm v5 paths use name/version, including /@scope/name/version.
75+
const versionSlash = legacySlash
76+
if (versionSlash !== -1) {
77+
const name = depPath.slice(0, versionSlash)
78+
const version = stripPnpmPeerSuffix(depPath.slice(versionSlash + 1))
79+
if (semver.valid(version)) {
80+
return `${name}@${version}`
81+
}
82+
}
83+
84+
// Some lockfiles contain package IDs without versions; leave those intact.
85+
return depPath
86+
}
87+
5688
export function isPnpmDepPath(maybeDepPath: string): boolean {
5789
return maybeDepPath.length > 0 && maybeDepPath.charCodeAt(0) === 47 /*'/'*/
5890
}

‎test/workflow-actions.test.mts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,33 @@ describe('CI scheduling', () => {
211211
expect(e2e.on).toEqual({ workflow_dispatch: null })
212212
})
213213

214+
it('keeps pnpm global virtual store disabled for the full Windows test job', () => {
215+
const steps = ci.jobs.test.steps as Array<{
216+
if?: string
217+
name?: string
218+
run?: string
219+
}>
220+
const storeConfig = steps.find(
221+
step => step.name === 'Disable pnpm global virtual store • Windows',
222+
)
223+
expect(storeConfig).toMatchObject({
224+
if: "runner.os == 'Windows'",
225+
run: expect.stringContaining(
226+
'pnpm_config_enable_global_virtual_store=false',
227+
),
228+
})
229+
expect(storeConfig?.run).toContain(
230+
'npm_config_enable_global_virtual_store=false',
231+
)
232+
expect(
233+
steps.find(step => step.name === 'Install dependencies • Bash')?.run,
234+
).toContain('--config.enable-global-virtual-store=false')
235+
expect(
236+
steps.find(step => step.name === 'Install dependencies • PowerShell')
237+
?.run,
238+
).toContain('--config.enable-global-virtual-store=false')
239+
})
240+
214241
it('enables fail-fast on every CI and E2E matrix', () => {
215242
for (const workflow of [ci, e2e]) {
216243
const jobs = Object.values<{

0 commit comments

Comments
 (0)