diff --git a/.config/vitest.config.mts b/.config/vitest.config.mts index eefa13d63..106bcea95 100644 --- a/.config/vitest.config.mts +++ b/.config/vitest.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -59,4 +59,4 @@ export default defineConfig({ ], }, }, -}) +} satisfies UserConfig diff --git a/.config/vitest.e2e.config.mts b/.config/vitest.e2e.config.mts index 0ed28d4b0..668a97bdf 100644 --- a/.config/vitest.e2e.config.mts +++ b/.config/vitest.e2e.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -37,4 +37,4 @@ export default defineConfig({ ], }, }, -}) +} satisfies UserConfig diff --git a/.config/vitest.integration.config.mts b/.config/vitest.integration.config.mts index 5b37d0861..20c092a2b 100644 --- a/.config/vitest.integration.config.mts +++ b/.config/vitest.integration.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -29,4 +29,4 @@ export default defineConfig({ 'test/fixtures/**', ], }, -}) +} satisfies UserConfig diff --git a/.config/vitest.native.config.mts b/.config/vitest.native.config.mts index 994807a1a..bf7b0eede 100644 --- a/.config/vitest.native.config.mts +++ b/.config/vitest.native.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -32,4 +32,4 @@ export default defineConfig({ 'test/fixtures/**', ], }, -}) +} satisfies UserConfig diff --git a/.config/vitest.terminal.config.mts b/.config/vitest.terminal.config.mts index 063691615..3b40b7130 100644 --- a/.config/vitest.terminal.config.mts +++ b/.config/vitest.terminal.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false }, test: { @@ -23,4 +23,4 @@ export default defineConfig({ ], exclude: ['**/node_modules/**', '**/dist/**', '.claude/worktrees/**'], }, -}) +} satisfies UserConfig diff --git a/src/utils/pnpm-scanning.test.mts b/src/utils/pnpm-scanning.test.mts index c24d7ec29..89c7d340e 100644 --- a/src/utils/pnpm-scanning.test.mts +++ b/src/utils/pnpm-scanning.test.mts @@ -105,6 +105,31 @@ packages: expect(purls).toContain('pkg:npm/@types/node@20.11.19') }) + it('preserves underscores in package names while removing peer suffixes', async () => { + const lockfile = parsePnpmLockfile(`lockfileVersion: '9.0' +packages: + my_package@1.2.3_peer@5.0.0: {} + my_pkg@1.2.4(@scope/peer@2.0.0): {} + '@scope/my_package@2.3.4(peer@5.0.0)': {} + '@scope/my_pkg@2.3.5(@other/peer@2.0.0)': {} + my_package@1.2.6: {} + /legacy_package/3.4.5_peer@5.0.0: {} + /@legacy/scoped_pkg/4.5.6_@peer_scope/name@1.0.0: {} + '@versionless/name': {}`) + + expect(lockfile).toBeTruthy() + expect(await extractPurlsFromPnpmLockfile(lockfile!)).toEqual([ + 'pkg:npm/my_package@1.2.3', + 'pkg:npm/my_pkg@1.2.4', + 'pkg:npm/@scope/my_package@2.3.4', + 'pkg:npm/@scope/my_pkg@2.3.5', + 'pkg:npm/my_package@1.2.6', + 'pkg:npm/legacy_package@3.4.5', + 'pkg:npm/@legacy/scoped_pkg@4.5.6', + 'pkg:npm/@versionless/name', + ]) + }) + it('should scan the project graph from a split pnpm lockfile', async () => { const lockfileContent = `--- lockfileVersion: '9.0' diff --git a/src/utils/pnpm.mts b/src/utils/pnpm.mts index c3529a953..8803ad72e 100644 --- a/src/utils/pnpm.mts +++ b/src/utils/pnpm.mts @@ -49,10 +49,42 @@ export async function extractPurlsFromPnpmLockfile( visit(pkgPath) } return Array.from(seen).map(p => - idToNpmPurl(stripPnpmPeerSuffix(stripLeadingPnpmDepPathSlash(p))), + idToNpmPurl( + stripPnpmPeerSuffixFromDepPath(stripLeadingPnpmDepPathSlash(p)), + ), ) } +function stripPnpmPeerSuffixFromDepPath(depPath: string): string { + // Modern pnpm paths use name@version; older lockfiles used name/version. + // Peer suffix delimiters belong to the version component, since npm names + // themselves may contain underscores. + const scoped = depPath.startsWith('@') + const nameSlash = scoped ? depPath.indexOf('/') : -1 + const atIndex = scoped + ? depPath.indexOf('@', nameSlash + 1) + : depPath.indexOf('@') + const legacySlash = scoped + ? depPath.indexOf('/', nameSlash + 1) + : depPath.indexOf('/') + if (atIndex !== -1 && (legacySlash === -1 || atIndex < legacySlash)) { + return `${depPath.slice(0, atIndex + 1)}${stripPnpmPeerSuffix(depPath.slice(atIndex + 1))}` + } + + // Legacy pnpm v5 paths use name/version, including /@scope/name/version. + const versionSlash = legacySlash + if (versionSlash !== -1) { + const name = depPath.slice(0, versionSlash) + const version = stripPnpmPeerSuffix(depPath.slice(versionSlash + 1)) + if (semver.valid(version)) { + return `${name}@${version}` + } + } + + // Some lockfiles contain package IDs without versions; leave those intact. + return depPath +} + export function isPnpmDepPath(maybeDepPath: string): boolean { return maybeDepPath.length > 0 && maybeDepPath.charCodeAt(0) === 47 /*'/'*/ }