Skip to content

Commit a20c918

Browse files
committed
Narrow npm fix
1 parent 1fe9d1b commit a20c918

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

src/commands/optimize/apply-optimization.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ export async function applyOptimization(
337337
logger?.log('Congratulations! Already Socket.dev optimized 🎉')
338338
}
339339

340-
if (pkgEnvDetails.agent === NPM || pkgJsonChanged) {
341-
// Always update package-lock.json until the npm overrides PR lands:
342-
// https://github.com/npm/cli/pull/8089
340+
if (pkgJsonChanged || pkgEnvDetails.features.npmBuggyOverrides) {
343341
await updateLockfile(pkgEnvDetails, { cmdName: CMD_NAME, logger, spinner })
344342
}
345343
}

src/commands/optimize/update-lockfile.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { cmdPrefixMessage } from '../../utils/cmd'
99
import type { EnvDetails } from '../../utils/package-environment'
1010
import type { Logger } from '@socketsecurity/registry/lib/logger'
1111

12-
const { NPM } = constants
12+
const { NPM, NPM_BUGGY_OVERRIDES_PATCHED_VERSION } = constants
1313

1414
export type UpdateLockfileOptions = {
1515
cmdName?: string | undefined
@@ -32,12 +32,9 @@ export async function updateLockfile(
3232
try {
3333
await runAgentInstall(pkgEnvDetails, { spinner })
3434
spinner?.stop()
35-
if (
36-
pkgEnvDetails.agent === NPM &&
37-
semver.lt(pkgEnvDetails.agentVersion, '11.2.0')
38-
) {
35+
if (pkgEnvDetails.features.npmBuggyOverrides) {
3936
logger?.log(
40-
`💡 Re-run ${cmdName ? `${cmdName} ` : ''}whenever ${pkgEnvDetails.lockName} changes.\n This can be skipped for npm >=11.2.0.`
37+
`💡 Re-run ${cmdName ? `${cmdName} ` : ''}whenever ${pkgEnvDetails.lockName} changes.\n This can be skipped for ${pkgEnvDetails.agent} >=${NPM_BUGGY_OVERRIDES_PATCHED_VERSION}.`
4138
)
4239
}
4340
} catch (e) {

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Constants = Remap<
8080
readonly IPC: IPC
8181
readonly LOCK_EXT: '.lock'
8282
readonly MODULE_SYNC: 'module-sync'
83+
readonly NPM_BUGGY_OVERRIDES_PATCHED_VERSION: '11.2.0'
8384
readonly NPM_REGISTRY_URL: 'https://registry.npmjs.org'
8485
readonly PNPM: 'pnpm'
8586
readonly REDACTED: '<redacted>'
@@ -151,6 +152,7 @@ const DRY_RUN_LABEL = '[DryRun]'
151152
const DRY_RUN_BAIL_TEXT = `${DRY_RUN_LABEL}: Bailing now`
152153
const LOCK_EXT = '.lock'
153154
const MODULE_SYNC = 'module-sync'
155+
const NPM_BUGGY_OVERRIDES_PATCHED_VERSION = '11.2.0'
154156
const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
155157
const PNPM = 'pnpm'
156158
const REDACTED = '<redacted>'
@@ -316,6 +318,7 @@ const constants = createConstantsObject(
316318
ENV: undefined,
317319
LOCK_EXT,
318320
MODULE_SYNC,
321+
NPM_BUGGY_OVERRIDES_PATCHED_VERSION,
319322
NPM_REGISTRY_URL,
320323
PNPM,
321324
REDACTED,

src/utils/package-environment.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { cmdPrefixMessage } from './cmd'
1818
import { findUp, readFileBinary, readFileUtf8 } from './fs'
1919
import constants from '../constants'
2020

21+
import type { Remap } from '@socketsecurity/registry/lib/objects'
2122
import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
2223
import type { SemVer } from 'semver'
2324

@@ -26,6 +27,7 @@ const {
2627
BUN,
2728
LOCK_EXT,
2829
NPM,
30+
NPM_BUGGY_OVERRIDES_PATCHED_VERSION,
2931
PNPM,
3032
VLT,
3133
YARN,
@@ -144,41 +146,48 @@ export type DetectOptions = {
144146
onUnknown?: (pkgManager: string | undefined) => void
145147
}
146148

147-
export type EnvDetails = Readonly<{
149+
type EnvBase = {
148150
agent: Agent
149151
agentExecPath: string
150-
agentVersion: SemVer
151-
lockName: string
152-
lockPath: string
153-
lockSrc: string
154-
minimumNodeVersion: string
155-
npmExecPath: string
156-
pkgJson: EditablePackageJson
157-
pkgPath: string
158-
supported: boolean
159-
targets: {
160-
browser: boolean
161-
node: boolean
152+
features: {
153+
// Fixed by https://github.com/npm/cli/pull/8089.
154+
// Landed in npm v11.2.0.
155+
npmBuggyOverrides: boolean
162156
}
163-
}>
164-
165-
export type PartialEnvDetails = Readonly<{
166-
agent: Agent
167-
agentExecPath: string
168-
agentVersion: SemVer | undefined
169-
lockName: string | undefined
170-
lockPath: string | undefined
171-
lockSrc: string | undefined
172157
minimumNodeVersion: string
173158
npmExecPath: string
174-
pkgJson: EditablePackageJson | undefined
175-
pkgPath: string | undefined
176-
supported: boolean
159+
pkgSupported: boolean
177160
targets: {
178161
browser: boolean
179162
node: boolean
180163
}
181-
}>
164+
}
165+
166+
export type EnvDetails = Readonly<
167+
Remap<
168+
EnvBase & {
169+
agentVersion: SemVer
170+
lockName: string
171+
lockPath: string
172+
lockSrc: string
173+
pkgJson: EditablePackageJson
174+
pkgPath: string
175+
}
176+
>
177+
>
178+
179+
export type PartialEnvDetails = Readonly<
180+
Remap<
181+
EnvBase & {
182+
agentVersion: SemVer | undefined
183+
lockName: string | undefined
184+
lockPath: string | undefined
185+
lockSrc: string | undefined
186+
pkgJson: EditablePackageJson | undefined
187+
pkgPath: string | undefined
188+
}
189+
>
190+
>
182191

183192
export async function detectPackageEnvironment({
184193
cwd = process.cwd(),
@@ -289,6 +298,11 @@ export async function detectPackageEnvironment({
289298
lockName = undefined
290299
lockPath = undefined
291300
}
301+
const pkgSupported = targets.browser || targets.node
302+
const npmBuggyOverrides =
303+
agent === NPM &&
304+
!!agentVersion &&
305+
semver.lt(agentVersion, NPM_BUGGY_OVERRIDES_PATCHED_VERSION)
292306
return {
293307
agent,
294308
agentExecPath,
@@ -300,7 +314,10 @@ export async function detectPackageEnvironment({
300314
npmExecPath,
301315
pkgJson: editablePkgJson,
302316
pkgPath,
303-
supported: targets.browser || targets.node,
317+
pkgSupported,
318+
features: {
319+
npmBuggyOverrides
320+
},
304321
targets
305322
}
306323
}
@@ -333,7 +350,7 @@ export async function detectAndValidatePackageEnvironment(
333350
)
334351
}
335352
})
336-
if (!details.supported) {
353+
if (!details.pkgSupported) {
337354
logger?.fail(
338355
cmdPrefixMessage(cmdName, 'No supported Node or browser range detected')
339356
)

0 commit comments

Comments
 (0)