Skip to content

Commit 2e210c3

Browse files
committed
Add more debug around resolving the actual arborist tree
1 parent 6b631b3 commit 2e210c3

File tree

4 files changed

+62
-66
lines changed

4 files changed

+62
-66
lines changed

src/commands/fix/agent-fix.mts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ export type InstallPhaseHandler = (
9494
fixConfig: FixConfig,
9595
) => Promise<void>
9696

97-
export type Installer = (
98-
pkgEnvDetails: EnvDetails,
99-
options: InstallOptions,
100-
) => Promise<InstallerResult>
101-
102-
export type InstallerResult = {
97+
export type ActualTreeResult = {
10398
actualTree?: NodeClass | undefined
10499
error?: unknown | undefined
105100
}
106101

102+
export type Installer = (
103+
pkgEnvDetails: EnvDetails,
104+
options: InstallOptions,
105+
) => Promise<ActualTreeResult>
106+
107107
const noopHandler = (() => {}) as unknown as InstallPhaseHandler
108108

109109
export async function agentFix(
@@ -282,7 +282,13 @@ export async function agentFix(
282282
}
283283
if (fixEnv.isCi && existsSync(path.join(rootPath, 'node_modules'))) {
284284
// eslint-disable-next-line no-await-in-loop
285-
actualTree = await getActualTree(cwd)
285+
const treeResult = await getActualTree(cwd)
286+
const maybeActualTree = treeResult.actualTree
287+
if (!maybeActualTree) {
288+
// Exit early if install fails.
289+
return handleInstallFail(treeResult.error)
290+
}
291+
actualTree = maybeActualTree
286292
} else {
287293
// eslint-disable-next-line no-await-in-loop
288294
const installResult = await installer(pkgEnvDetails, { cwd, spinner })

src/commands/fix/get-actual-tree.mts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { Arborist } from '../../shadow/npm/arborist/index.mts'
22
import { SAFE_NO_SAVE_ARBORIST_REIFY_OPTIONS_OVERRIDES } from '../../shadow/npm/arborist/lib/arborist/index.mts'
33

4-
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
4+
import type { ActualTreeResult } from './agent-fix.mts'
55

66
export async function getActualTree(
77
cwd: string = process.cwd(),
8-
): Promise<NodeClass> {
9-
// @npmcli/arborist DOES have partial support for pnpm structured node_modules
10-
// folders. However, support is iffy resulting in unhappy path errors and hangs.
11-
// So, to avoid the unhappy path, we restrict our usage to --dry-run loading
12-
// of the node_modules folder.
13-
const arb = new Arborist({
14-
path: cwd,
15-
...SAFE_NO_SAVE_ARBORIST_REIFY_OPTIONS_OVERRIDES,
16-
})
17-
return await arb.loadActual()
8+
): Promise<ActualTreeResult> {
9+
try {
10+
// @npmcli/arborist DOES have partial support for pnpm structured node_modules
11+
// folders. However, support is iffy resulting in unhappy paths of errors and hangs.
12+
// So, to avoid unhappy paths, we restrict our usage to --dry-run loading of the
13+
// node_modules folder.
14+
const arb = new Arborist({
15+
path: cwd,
16+
...SAFE_NO_SAVE_ARBORIST_REIFY_OPTIONS_OVERRIDES,
17+
})
18+
return { actualTree: await arb.loadActual() }
19+
} catch (e) {
20+
return { error: e }
21+
}
1822
}

src/commands/fix/npm-fix.mts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { getAlertsMapFromPurls } from '../../utils/alerts-map.mts'
1515
import { getNpmConfig } from '../../utils/npm-config.mts'
1616

1717
import type {
18+
ActualTreeResult,
1819
FixConfig,
1920
InstallOptions,
20-
InstallerResult,
2121
} from './agent-fix.mts'
2222
import type {
2323
ArboristInstance,
@@ -31,7 +31,7 @@ import type { PackageJson } from '@socketsecurity/registry/lib/packages'
3131
async function install(
3232
pkgEnvDetails: EnvDetails,
3333
options: InstallOptions,
34-
): Promise<InstallerResult> {
34+
): Promise<ActualTreeResult> {
3535
const {
3636
args: extraArgs,
3737
cwd,
@@ -70,45 +70,38 @@ async function install(
7070
...(useDebug ? [] : ['--silent']),
7171
...(extraArgs ?? []),
7272
]
73-
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
74-
debugFn('stdio', `spawn: ${quotedCmd}`)
7573

7674
const isSpinning = spinner?.isSpinning
7775
spinner?.stop()
7876

79-
let error
80-
let errored = false
77+
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
78+
debugFn('stdio', `spawn: ${quotedCmd}`)
8179
try {
8280
await runAgentInstall(pkgEnvDetails, {
8381
args,
8482
spinner,
8583
stdio: useDebug ? 'inherit' : 'ignore',
8684
})
87-
} catch (e) {
88-
errored = true
89-
error = e
85+
} catch (error) {
86+
const result = { error }
9087
debugFn('error', `caught: ${quotedCmd} failed`)
91-
debugDir('inspect', { error })
88+
debugDir('inspect', result)
89+
return result
9290
}
9391

94-
let actualTree: NodeClass | undefined = undefined
95-
if (!errored) {
96-
try {
97-
actualTree = await getActualTree(cwd)
98-
} catch (e) {
99-
errored = true
100-
error = e
101-
debugFn('error', 'caught: Arborist error')
102-
debugDir('inspect', { error })
92+
const treeResult = await getActualTree(cwd)
93+
if (treeResult.actualTree) {
94+
if (isSpinning) {
95+
spinner.start()
10396
}
97+
return treeResult
10498
}
99+
debugFn('error', 'caught: await arb.loadActual() error')
100+
debugDir('inspect', treeResult)
105101
if (isSpinning) {
106102
spinner.start()
107103
}
108-
return {
109-
...(actualTree ? { actualTree } : undefined),
110-
...(errored ? { error } : undefined),
111-
}
104+
return treeResult
112105
}
113106

114107
export async function npmFix(
@@ -145,8 +138,8 @@ export async function npmFix(
145138
debugDir('inspect', { error: e })
146139
return {
147140
ok: false,
148-
message: 'Arborist error',
149-
cause: (e as Error)?.message || 'Unknown Arborist error.',
141+
message: 'npm error',
142+
cause: (e as Error)?.message || 'Unknown npm error.',
150143
}
151144
}
152145
alertsMap = await getAlertsMapFromArborist(arb, getFixAlertsMapOptions())

src/commands/fix/pnpm-fix.mts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import { applyRange } from '../../utils/semver.mts'
2222
import { getOverridesDataPnpm } from '../optimize/get-overrides-by-agent.mts'
2323

2424
import type {
25+
ActualTreeResult,
2526
FixConfig,
2627
InstallOptions,
27-
InstallerResult,
2828
} from './agent-fix.mts'
2929
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
3030
import type { CResult, StringKeyValueObject } from '../../types.mts'
@@ -36,7 +36,7 @@ const { OVERRIDES, PNPM } = constants
3636
async function install(
3737
pkgEnvDetails: EnvDetails,
3838
options: InstallOptions,
39-
): Promise<InstallerResult> {
39+
): Promise<ActualTreeResult> {
4040
const {
4141
args: extraArgs,
4242
cwd,
@@ -57,45 +57,38 @@ async function install(
5757
'--config.confirmModulesPurge=false',
5858
...(extraArgs ?? []),
5959
]
60-
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
61-
debugFn('stdio', `spawn: ${quotedCmd}`)
6260

6361
const isSpinning = spinner?.isSpinning
6462
spinner?.stop()
6563

66-
let error
67-
let errored = false
64+
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
65+
debugFn('stdio', `spawn: ${quotedCmd}`)
6866
try {
6967
await runAgentInstall(pkgEnvDetails, {
7068
args,
7169
spinner,
7270
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
7371
})
74-
} catch (e) {
75-
errored = true
76-
error = e
72+
} catch (error) {
73+
const result = { error }
7774
debugFn('error', `caught: ${quotedCmd} failed`)
78-
debugDir('inspect', { error })
75+
debugDir('inspect', result)
76+
return result
7977
}
8078

81-
let actualTree: NodeClass | undefined = undefined
82-
if (!errored) {
83-
try {
84-
actualTree = await getActualTree(cwd)
85-
} catch (e) {
86-
errored = true
87-
error = e
88-
debugFn('error', 'caught: Arborist error')
89-
debugDir('inspect', { error })
79+
const treeResult = await getActualTree(cwd)
80+
if (treeResult.actualTree) {
81+
if (isSpinning) {
82+
spinner.start()
9083
}
84+
return treeResult
9185
}
86+
debugFn('error', 'caught: await arb.loadActual() error')
87+
debugDir('inspect', treeResult)
9288
if (isSpinning) {
9389
spinner.start()
9490
}
95-
return {
96-
...(actualTree ? { actualTree } : undefined),
97-
...(errored ? { error } : undefined),
98-
}
91+
return treeResult
9992
}
10093

10194
export async function pnpmFix(

0 commit comments

Comments
 (0)