import { quote } from 'shlex' ; import { TEMPORARY_ERROR } from '../../constants/error-messages' ; import { logger } from '../../logger' ; import { exec } from '../../util/exec' ; import type { ExecOptions } from '../../util/exec/types' ; import { findLocalSiblingOrParent , readLocalFile , writeLocalFile , } from '../../util/fs' ; import type { UpdateArtifact , UpdateArtifactsResult } from '../types' ; async function cargoUpdate ( manifestPath , isLockFileMaintenance ) { let cmd = `cargo update --manifest-path ${ quote ( manifestPath ) }` ; if ( ! isLockFileMaintenance ) { cmd += ` --workspace` ; } const execOptions = { docker : { image : 'rust' , } , } ; await exec ( cmd , execOptions ) ; } export async function updateArtifacts ( { packageFileName , updatedDeps , newPackageFileContent , config , } ) { logger . debug ( `cargo.updateArtifacts(${ packageFileName })` ) ; const isLockFileMaintenance = config . updateType === 'lockFileMaintenance' ; if ( ! isLockFileMaintenance && ( updatedDeps === undefined || updatedDeps . length < 1 ) ) { logger . debug ( 'No updated cargo deps - returning null' ) ; return null ; } const lockFileName = await findLocalSiblingOrParent ( packageFileName , 'Cargo.lock' ) ; const existingLockFileContent = lockFileName ? await readLocalFile ( lockFileName ) : null ; if ( ! existingLockFileContent ) { logger . debug ( 'No Cargo.lock found' ) ; return null ; } try { await writeLocalFile ( packageFileName , newPackageFileContent ) ; logger . debug ( 'Updating ' + lockFileName ) ; await cargoUpdate ( packageFileName , isLockFileMaintenance ) ; logger . debug ( 'Returning updated Cargo.lock' ) ; const newCargoLockContent = await readLocalFile ( lockFileName ) ; if ( existingLockFileContent === newCargoLockContent ) { logger . debug ( 'Cargo.lock is unchanged' ) ; return null ; } return [ { file : { name : lockFileName , contents : newCargoLockContent , } , } , ] ; } catch ( err ) { if ( err . message === TEMPORARY_ERROR ) { throw err ; } logger . warn ( { err } , 'Failed to update Cargo lock file' ) ; return [ { artifactError : { lockFile : lockFileName , stderr : err . message , } , } , ] ; } }