Powershell does not return the correct non-zero exit code
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 1.4k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 24
Description
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:
- Create an action that outputs an exit code that is not 0 or 1.
run: php -r "exit(42);"
shell: pwsh
- See error, as in this example from https://github.com/johnstevenson/runner-actions-test/actions/runs/48177580 which shows the correct behaviour for
cmdandbashand the error frompwsh.

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:
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"] = "-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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.