1- import { existsSync } from 'node:fs'
1+ import { existsSync , promises as fs } from 'node:fs'
22import path from 'node:path'
33
44import semver from 'semver'
@@ -253,11 +253,7 @@ export async function agentFix(
253253 await getActualTree ( cwd )
254254 : // eslint-disable-next-line no-await-in-loop
255255 await installer ( pkgEnvDetails , { cwd, spinner } )
256- const maybeLockSrc = maybeActualTree
257- ? // eslint-disable-next-line no-await-in-loop
258- await readLockfile ( pkgEnvDetails . lockPath )
259- : null
260- if ( maybeActualTree && maybeLockSrc ) {
256+ if ( maybeActualTree && existsSync ( pkgEnvDetails . lockPath ) ) {
261257 actualTree = maybeActualTree
262258 }
263259 }
@@ -385,19 +381,19 @@ export async function agentFix(
385381 )
386382
387383 // eslint-disable-next-line no-await-in-loop
388- const res = await editablePkgJson . save ( { ignoreWhitespace : true } )
384+ await editablePkgJson . save ( { ignoreWhitespace : true } )
389385
390386 // eslint-disable-next-line no-await-in-loop
391387 const unstagedCResult = await gitUnstagedModifiedFiles ( cwd )
392- const moddedFilepaths = res && unstagedCResult . ok
393- ? unstagedCResult . data . filter ( filepath => {
394- const basename = path . basename ( filepath )
395- return (
396- basename === 'package.json' ||
397- basename === pkgEnvDetails . lockName
398- )
399- } )
400- : [ ]
388+ const moddedFilepaths = unstagedCResult . ok
389+ ? unstagedCResult . data . filter ( filepath => {
390+ const basename = path . basename ( filepath )
391+ return (
392+ basename === 'package.json' ||
393+ basename === pkgEnvDetails . lockName
394+ )
395+ } )
396+ : [ ]
401397 if ( ! moddedFilepaths . length ) {
402398 logger . warn (
403399 'Unexpected condition: Nothing to commit, skipping PR creation.' ,
@@ -410,6 +406,9 @@ export async function agentFix(
410406 continue infosLoop
411407 }
412408
409+ // eslint-disable-next-line no-await-in-loop
410+ const lockSrc = await readLockfile ( pkgEnvDetails . lockPath )
411+
413412 if ( ! hasAnnouncedWorkspace ) {
414413 hasAnnouncedWorkspace = true
415414 workspaceLogCallCount = logger . logCallCount
@@ -428,14 +427,7 @@ export async function agentFix(
428427 cwd,
429428 spinner,
430429 } )
431- // eslint-disable-next-line no-await-in-loop
432- const unstagedCResult = await gitUnstagedModifiedFiles ( cwd )
433- console . log ( 'after installer' , unstagedCResult )
434- const maybeLockSrc = maybeActualTree
435- ? // eslint-disable-next-line no-await-in-loop
436- await readLockfile ( pkgEnvDetails . lockPath )
437- : null
438- if ( maybeActualTree && maybeLockSrc ) {
430+ if ( maybeActualTree && existsSync ( pkgEnvDetails . lockPath ) ) {
439431 actualTree = maybeActualTree
440432 // eslint-disable-next-line no-await-in-loop
441433 await afterInstall (
@@ -446,9 +438,6 @@ export async function agentFix(
446438 vulnerableVersionRange ,
447439 fixConfig ,
448440 )
449- // eslint-disable-next-line no-await-in-loop
450- const unstagedCResult = await gitUnstagedModifiedFiles ( cwd )
451- console . log ( 'after afterInstall' , unstagedCResult )
452441 if ( test ) {
453442 spinner ?. info ( `Testing ${ newId } in ${ workspace } .` )
454443 // eslint-disable-next-line no-await-in-loop
@@ -468,17 +457,11 @@ export async function agentFix(
468457
469458 // Check repoInfo to make TypeScript happy.
470459 if ( ! errored && fixEnv . isCi && fixEnv . repoInfo ) {
460+ // Rewrite files in case the install reverted them.
471461 // eslint-disable-next-line no-await-in-loop
472- const unstagedCResult = await gitUnstagedModifiedFiles ( cwd )
473- const moddedFilepaths = unstagedCResult . ok
474- ? unstagedCResult . data . filter ( filepath => {
475- const basename = path . basename ( filepath )
476- return (
477- basename === 'package.json' ||
478- basename === pkgEnvDetails . lockName
479- )
480- } )
481- : [ ]
462+ await editablePkgJson . save ( { ignoreWhitespace : true } )
463+ // eslint-disable-next-line no-await-in-loop
464+ await fs . writeFile ( pkgEnvDetails . lockPath , lockSrc ! , 'utf8' )
482465 try {
483466 if (
484467 // eslint-disable-next-line no-await-in-loop
@@ -503,11 +486,7 @@ export async function agentFix(
503486 cwd,
504487 spinner,
505488 } )
506- const maybeLockSrc = maybeActualTree
507- ? // eslint-disable-next-line no-await-in-loop
508- await readLockfile ( pkgEnvDetails . lockPath )
509- : null
510- if ( maybeActualTree && maybeLockSrc ) {
489+ if ( maybeActualTree && existsSync ( pkgEnvDetails . lockPath ) ) {
511490 actualTree = maybeActualTree
512491 continue infosLoop
513492 }
0 commit comments