Skip to content

Commit 1498f4b

Browse files
committed
Use a new Arborist instance and re-save package.json too
1 parent 1c7eaec commit 1498f4b

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/commands/fix/agent-fix.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ export async function agentFix(
406406
continue infosLoop
407407
}
408408

409+
// eslint-disable-next-line no-await-in-loop
410+
const pkgJsonSrc = await fs.readFile(
411+
editablePkgJson.filename!,
412+
'utf8',
413+
)
409414
// eslint-disable-next-line no-await-in-loop
410415
const lockSrc = await readLockfile(pkgEnvDetails.lockPath)
411416

@@ -459,7 +464,7 @@ export async function agentFix(
459464
if (!errored && fixEnv.isCi && fixEnv.repoInfo) {
460465
// Rewrite files in case the install reverted them.
461466
// eslint-disable-next-line no-await-in-loop
462-
await editablePkgJson.save({ ignoreWhitespace: true })
467+
await fs.writeFile(editablePkgJson.filename!, pkgJsonSrc, 'utf8')
463468
// eslint-disable-next-line no-await-in-loop
464469
await fs.writeFile(pkgEnvDetails.lockPath, lockSrc!, 'utf8')
465470
try {

src/commands/fix/npm-fix.mts

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

1616
import type { FixConfig, InstallOptions } from './agent-fix.mts'
17-
import type {
18-
ArboristInstance,
19-
NodeClass,
20-
} from '../../shadow/npm/arborist/types.mts'
17+
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
2118
import type { CResult } from '../../types.mts'
2219
import type { EnvDetails } from '../../utils/package-environment.mts'
2320
import type { PackageJson } from '@socketsecurity/registry/lib/packages'
@@ -49,17 +46,17 @@ export async function npmFix(
4946

5047
spinner?.start()
5148

52-
let arb: ArboristInstance
49+
const flatConfig = await getNpmConfig({
50+
npmVersion: pkgEnvDetails.agentVersion,
51+
})
52+
5353
let actualTree: NodeClass | undefined
5454
let alertsMap
5555
try {
5656
if (purls.length) {
5757
alertsMap = await getAlertsMapFromPurls(purls, getFixAlertsMapOptions())
5858
} else {
59-
const flatConfig = await getNpmConfig({
60-
npmVersion: pkgEnvDetails.agentVersion,
61-
})
62-
arb = new Arborist({
59+
const arb = new Arborist({
6360
path: pkgEnvDetails.pkgPath,
6461
...flatConfig,
6562
})
@@ -102,6 +99,10 @@ export async function npmFix(
10299
}),
103100
} as PackageJson
104101

102+
const arb = new Arborist({
103+
path: pkgEnvDetails.pkgPath,
104+
...flatConfig,
105+
})
105106
const idealTree = await arb.buildIdealTree()
106107
const node = findPackageNode(idealTree, packument.name, oldVersion)
107108
if (node) {

0 commit comments

Comments
 (0)