Powershell error output not shown

I’m running a Powershell script in a GHA workflow (windows-latest runner) and the output when an error occurs doesn’t seem to show the reason for the failure. All it shows is “##[error]Process completed with exit code 1.”

This is problematic: the script appears to crash at this point and I can’t get output to show.  I’ve also tried to output the failure using try/catch: this stops the script failing, but output statements in the catch block aren’t shown.

I have direct EXE invocations at other points and they show errors; it’s just powershell script invocation that dies without a trace. This makes it very difficult to understand why a script is failing.

Adding step debugging produces more output about invocation, but doesn’t give any extra information about the issue.

For example, a script:

try {
    throw (new-object "Exception" @('foo'))
} catch {
    write-host "caught failure: $_"
}


throw "Failed"

with a YAML:

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: ['*']

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: windows-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2

    - name: Fail script
      run: .github/bin/fail-script.ps1

Results in the following output:

caught failure: foo
##[error]Process completed with exit code 1.

In this case, the first exception message is printed out BUT the second isn’t. In my case, where I’m using signtool, or Get-PfxCertificate, it just crashes entirely.

Losing this kind of context means that I’m stumped why my script isn’t working: I can make it work on a number of local machines and environments, but not the GHA runner.

Thank you for reporting this issue. I have asked help from engineering team , when they give me any response I will update here soon. 

Thanks for forwarding this on!

I’ve done some further investigation, and some further investigation.

As far as I can tell, if script processing results in an exception being thrown, then:

  1. script processing will stop (as expected),
  2. no error message shown, and
  3. the result “Process completed with exit code 1” will be shown.

This is understandable, but the lack of error output creates confusion and becomes difficult to debug.

It is possible to work around this limitation by specifying a custom shell:

- name: Do the thing
      run: |
          .github/bin/do-thing.ps1 -Path 'foo' -WhatThing '${{ env.THING }}'
      shell: |
        pwsh -noninteractive -command "try {{ $ErrorActionPreference='Stop'; . '{0}' }} catch {{ Write-Error ""FAILED: $_""; throw; }} if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) {{ exit $LASTEXITCODE }}"

The value for shell can be copied and pasted to all commands without alteration to improve the error output.

This custom shell (see doco) trys to follow the behaviour of the default pwsh shell as mentioned in the docs; that is, it fails fast and returns $LASTEXITCODE when that is non-zero. It also writes to the Error stream the description of any thrown error.

Therefore, if there is any script parsing error, or if there is a runtime error with the script, some details will be printed to the output to aid debugging. It would be great if the built in shell would pick up and improve this behaviour: it would be a great UX improvement when trying to get a pipeline working!

Note BTW that the doubled starting and closing braces are necessary as this string is passed to C#'s String.Format method, which requires the braces be doubled to escape them. The doubled quotes are for command line escaping.

Thank you for sharing your workaround here. I appreciate your contribution. I will report your feedback to product team.