toolkit icon indicating copy to clipboard operation
toolkit copied to clipboard

Non-zero `exitCode` is not accessible from `exec`/`getExecOutput` with `ignoreReturnCode: false`

Open allanlei opened this issue 1 year ago • 1 comments

Describe the enhancement When ignoreReturnCode: false is set and a process exits with a non-zero exit code, an Error is thrown. This error contains the message The process '${this.toolPath}' failed with exit code ${this.processExitCode} but has no direct access to the exit code and would need to be parsed from the message as a workaround.

Compare this to ignoreReturnCode: true, which has the exitCode readily accessible.


let { exitCode, stdout, stderr } = await exec.getExecOutput(
  'ls', ['package.json'], 
  {ignoreReturnCode: true}
)

Code Snippet

try {
  // Non-zero exits will proceed to `catch`
  let { exitCode, stdout, stderr } = await exec.getExecOutput(
    'ls', ['package.json'], 
    {ignoreReturnCode: false}
  )
} catch (error) {
   // Exit code is not accessible in `error.exitCode`
}

Additional information Related code: https://github.com/actions/toolkit/blob/main/packages/exec/src/toolrunner.ts#L530-L534

allanlei avatar Dec 27 '23 08:12 allanlei

With exec it works for me.

async function ls(params) {
    const stdout = []
    const stderr = []
    const options = {
        listeners: {
            stdout: (data) => {
                stdout.push(data.toString())
            },
            stderr: (data) => {
                stderr.push(data.toString())
            }
        },
        ignoreReturnCode: true
    }
    const exitCode = await exec.exec('ls', params, options)
    let result = new CommandResult()
    result.exitCode = exitCode
    result.stdout = stdout.join('')
    result.stderr = stdout.join('')
}

class CommandResult {
    stdout = ''
    stderr = ''
    exitCode = 0
}

I could grab the exitCode when it's value is different from zero.

vitorhugods avatar Apr 26 '24 13:04 vitorhugods