Skip to content

Commit 8880a66

Browse files
committed
Fix check for .lock ext in optimize command
1 parent 40a40e0 commit 8880a66

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/commands/optimize.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type PackageJson = Awaited<ReturnType<typeof readPackageJson>>
4444

4545
const {
4646
BUN,
47+
LOCK_EXT,
4748
NPM,
4849
PNPM,
4950
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
@@ -136,10 +137,13 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
136137

137138
return {
138139
[BUN](lockSrc: string, name: string, lockBasename?: string) {
139-
return (lockBasename === '.lock' ? npmLockIncludes : yarnLockIncludes)(
140-
lockSrc,
141-
name
142-
)
140+
// This is a bit counterintuitive. When lockBasename ends with a .lockb
141+
// we treat it as a yarn.lock. When lockBasename ends with a .lock we
142+
// treat it as a package-lock.json. The bun.lock format is not identical
143+
// package-lock.json, however it close enough for npmLockIncludes to work.
144+
const lockScanner =
145+
lockBasename?.endsWith(LOCK_EXT) ? npmLockIncludes : yarnLockIncludes
146+
return lockScanner(lockSrc, name)
143147
},
144148
[NPM]: npmLockIncludes,
145149
[PNPM](lockSrc: string, name: string) {
@@ -659,6 +663,10 @@ async function addOverrides(
659663
const thingToScan = isLockScanned
660664
? lockSrc
661665
: await lsByAgent[agent](agentExecPath, pkgPath, { npmExecPath })
666+
// The AgentDepsIncludesFn and AgentLockIncludesFn types overlap in their
667+
// first two parameters. AgentLockIncludesFn accepts an optional third
668+
// parameter which AgentDepsIncludesFn will ignore so we cast thingScanner
669+
// as an AgentLockIncludesFn type.
662670
const thingScanner = <AgentLockIncludesFn>(
663671
(isLockScanned ? lockIncludesByAgent[agent] : depsIncludesByAgent[agent])
664672
)

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ type RegistryEnv = typeof registryConstants.ENV
99
type Constants = {
1010
readonly API_V0_URL: 'https://api.socket.dev/v0'
1111
readonly BABEL_RUNTIME: '@babel/runtime'
12+
readonly BINARY_LOCK_EXT: '.lockb'
1213
readonly BUN: 'bun'
1314
readonly ENV: RegistryEnv & {
1415
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: boolean
1516
}
1617
readonly DIST_TYPE: 'module-sync' | 'require'
18+
readonly LOCK_EXT: '.lock'
1719
readonly NPM_REGISTRY_URL: 'https://registry.npmjs.org'
1820
readonly NPX: 'npx'
1921
readonly PNPM: 'pnpm'
@@ -43,7 +45,9 @@ const {
4345

4446
const API_V0_URL = 'https://api.socket.dev/v0'
4547
const BABEL_RUNTIME = '@babel/runtime'
48+
const BINARY_LOCK_EXT = '.lockb'
4649
const BUN = 'bun'
50+
const LOCK_EXT = '.lock'
4751
const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
4852
const NPX = 'npx'
4953
const PNPM = 'pnpm'
@@ -105,10 +109,12 @@ const constants = <Constants>createConstantsObject(
105109
{
106110
API_V0_URL,
107111
BABEL_RUNTIME,
112+
BINARY_LOCK_EXT,
108113
BUN,
109114
ENV: undefined,
110115
// Lazily defined values are initialized as `undefined` to keep their key order.
111116
DIST_TYPE: undefined,
117+
LOCK_EXT,
112118
NPM_REGISTRY_URL,
113119
NPX,
114120
PNPM,

src/utils/package-manager-detector.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ import { existsSync, findUp, readFileBinary, readFileUtf8 } from './fs'
1616
import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
1717
import type { SemVer } from 'semver'
1818

19-
const { BUN, NPM, PNPM, VLT, YARN_BERRY, YARN_CLASSIC } = constants
19+
const {
20+
BINARY_LOCK_EXT,
21+
BUN,
22+
LOCK_EXT,
23+
NPM,
24+
PNPM,
25+
VLT,
26+
YARN_BERRY,
27+
YARN_CLASSIC
28+
} = constants
2029

2130
export const AGENTS = [BUN, NPM, PNPM, YARN_BERRY, YARN_CLASSIC, VLT] as const
2231
export type Agent = (typeof AGENTS)[number]
@@ -46,9 +55,10 @@ async function getAgentVersion(
4655
return result
4756
}
4857

58+
// The order of LOCKS properties IS significant as it affects iteration order.
4959
const LOCKS: Record<string, Agent> = {
50-
'bun.lock': BUN,
51-
'bun.lockb': BUN,
60+
[`bun${LOCK_EXT}`]: BUN,
61+
[`bun${BINARY_LOCK_EXT}`]: BUN,
5262
// If both package-lock.json and npm-shrinkwrap.json are present in the root
5363
// of a project, npm-shrinkwrap.json will take precedence and package-lock.json
5464
// will be ignored.
@@ -57,9 +67,9 @@ const LOCKS: Record<string, Agent> = {
5767
'package-lock.json': NPM,
5868
'pnpm-lock.yaml': PNPM,
5969
'pnpm-lock.yml': PNPM,
60-
'yarn.lock': YARN_CLASSIC,
70+
[`yarn${LOCK_EXT}`]: YARN_CLASSIC,
6171
'vlt-lock.json': VLT,
62-
// Look for a hidden lock file if .npmrc has package-lock=false:
72+
// Lastly, look for a hidden lock file which is present if .npmrc has package-lock=false:
6373
// https://docs.npmjs.com/cli/v10/configuring-npm/package-lock-json#hidden-lockfiles
6474
//
6575
// Unlike the other LOCKS keys this key contains a directory AND filename so
@@ -92,10 +102,10 @@ const readLockFileByAgent: Record<Agent, ReadLockFile> = (() => {
92102
return {
93103
[BUN]: wrapReader(async (lockPath: string, agentExecPath: string) => {
94104
const ext = path.extname(lockPath)
95-
if (ext === '.lock') {
105+
if (ext === LOCK_EXT) {
96106
return await defaultReader(lockPath)
97107
}
98-
if (ext === '.lockb') {
108+
if (ext === BINARY_LOCK_EXT) {
99109
const lockBuffer = await binaryReader(lockPath)
100110
if (lockBuffer) {
101111
try {
@@ -107,6 +117,7 @@ const readLockFileByAgent: Record<Agent, ReadLockFile> = (() => {
107117
// https://bun.sh/guides/install/yarnlock
108118
return (await spawn(agentExecPath, [lockPath])).stdout.trim()
109119
}
120+
return undefined
110121
}),
111122
[NPM]: defaultReader,
112123
[PNPM]: defaultReader,

0 commit comments

Comments
 (0)