Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .config/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -59,4 +59,4 @@ export default defineConfig({
],
},
},
})
} satisfies UserConfig
6 changes: 3 additions & 3 deletions .config/vitest.e2e.config.mts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -37,4 +37,4 @@ export default defineConfig({
],
},
},
})
} satisfies UserConfig
6 changes: 3 additions & 3 deletions .config/vitest.integration.config.mts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -29,4 +29,4 @@ export default defineConfig({
'test/fixtures/**',
],
},
})
} satisfies UserConfig
6 changes: 3 additions & 3 deletions .config/vitest.native.config.mts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -32,4 +32,4 @@ export default defineConfig({
'test/fixtures/**',
],
},
})
} satisfies UserConfig
6 changes: 3 additions & 3 deletions .config/vitest.terminal.config.mts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -23,4 +23,4 @@ export default defineConfig({
],
exclude: ['**/node_modules/**', '**/dist/**', '.claude/worktrees/**'],
},
})
} satisfies UserConfig
25 changes: 25 additions & 0 deletions src/utils/pnpm-scanning.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
34 changes: 33 additions & 1 deletion src/utils/pnpm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 /*'/'*/
}
Expand Down
Loading