Skip to content

Powershell does not return the correct non-zero exit code #351

Open

Description

@johnstevenson

Describe the bug
Powershell does not return the correct exit code if it is non-zero and not 1.

To Reproduce
Steps to reproduce the behavior:

  1. Create an action that outputs an exit code that is not 0 or 1.
run: php -r "exit(42);"
shell: pwsh
  1. See error, as in this example from https://github.com/johnstevenson/runner-actions-test/actions/runs/48177580 which shows the correct behaviour for cmd and bash and the error from pwsh.

action-error-exit

Expected behavior
For powershell to return the exit code returned by the script.

Runner Version and Platform

Current runner version: 2.165.2
Tested on ubuntu-latest, macos-latest and windows-latest

See https://github.com/johnstevenson/runner-actions-test/actions/runs/48202446 for tests that use .bat, .sh and .ps1 scripts rather than using the php code.

What's not working?

The code that is appended to the powershell script in FixUpScriptContents:

var append = @"if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE }";

This can be fixed by either changing the line to:

var append = @"if (Test-Path -LiteralPath variable:\LASTEXITCODE) { $Host.SetShouldExit($LASTEXITCODE) }"; 

or by executing the script as a file, rather than a command which always returns the correct exit code:

["pwsh"] = "-command \". '{0}'\"",
["powershell"] = "-command \". '{0}'\"",

        ["pwsh"] = "-file \"'{0}'\""
        ["powershell"] = "-file \"'{0}'\"",

Incidentally, the documentation at https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#using-a-specific-shell shows the powershell internal commands as pwsh -command "& '{0}'" rather than pwsh -command ". '{0}'", but I'm not sure where to report this.

Activity

  1. added
    bug
    on Mar 2, 2020
  2. bryanmacfarlane commented on Mar 2, 2020

    @bryanmacfarlane
    Contributor

    @thboop - this is a dupe right?

  3. thboop commented on Mar 3, 2020

    @thboop
    Collaborator

    @thboop - this is a dupe right?

    I do not believe so.

  4. self-assigned this
    on Mar 3, 2020
  5. removed their assignment
    on Apr 14, 2020
  6. added
    Runner Bug
    and removed
    bug
    on Jun 6, 2020
  7. cprosser-clear commented on Oct 28, 2020

    @cprosser-clear

    Is this being worked on? This is causing a lot of problems for us.

  8. tomer-ds commented on Nov 9, 2022

    @tomer-ds

    Hi all... any updates on this?
    Tried the same test for LASTEXITCODE and same result of course

  9. bartekpacia commented on May 9, 2023

    @bartekpacia

    This problem makes it easy to miss test failures on Windows! IMHO it should be prioritized.

  10. balupton commented on Nov 15, 2025

    @balupton

    This is reproducible simply with:

          - name: "PowerShell Exit Status"
            shell: powershell
            run: Exit(5)
    

    The exit status will still be 1, despite locally such results in the correct exit status.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Runner Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions