-
Notifications
You must be signed in to change notification settings - Fork 7.7k
PSNativeCommandErrorActionPreference non-zero 0 exit code exception false positive #20034
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
As a (largely inconsequential) aside: An alternative per-call solution - not mutually exclusive with your proposal - is part of the larger My personal concerns with respect to a potential
|
I agree that the example provided was not the best and robocopy would have been a better one. Lets first level set on some common points/facts.
Now for my views on this feature:
|
Fortunately that won't be the case: As @SteveL-MSFT said here:
In other words: the feature will be off by default in the release version - sensibly so. |
Glad to hear it; that addresses my second concern.
I think proper framing is important here:
So it should be noted that straying from this convention is ill-advised, because it runs afoul of core shell features, especially in the Unix world. |
P.S.: To be clear:
|
Although |
The @PowerShell/wg-powershell-engine discussed this and concluded that it's not worth adding a mechanism to register successful non-zero exit codes. As @SteveL-MSFT points out, this feature is not enabled by default. It is "opt-in". And there is a documented mechanism to handle these exception cases in scripts where you have enabled # Some native commands, like robocopy use non-zero exit codes to represent
# information other than errors. In these cases, you can temporarily disable
# the behavior and prevent non-zero exit codes from issuing errors.
$definedPreference = $PSNativeCommandUseErrorActionPreference
$PSNativeCommandUseErrorActionPreference = $false
robocopy.exe D:\reports\operational "\\reporting\ops" CY2022Q4.md
$robocopyExitCode = $LASTEXITCODE
if ($robocopyExitCode -gt 8) {
throw "robocopy failed with exit code $robocopyExitCode"
}
$PSNativeCommandUseErrorActionPreference = $definedPreference Although I suggest we update the docs to take advantage of scriptblock scoping & cleanup e.g.: # Enable non-zero exit codes to stop script
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true
# This should stop the script if a non-zero exit code is returned
dotnet build
# Handle app that returns non-zero "successful" exit code
& {
$PSNativeCommandUseErrorActionPreference = $false
robocopy.exe D:\reports\operational "\\reporting\ops" CY2022Q4.md
if ($LASTEXITCODE -gt 8) {
throw "robocopy failed with exit code $robocopyExitCode"
}
}
# This should stop the script if a non-zero exit code is returned
dotnet test |
This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes. |
Uh oh!
There was an error while loading. Please reload this page.
Prerequisites
Steps to reproduce
There are quite a few programs that return non-zero success exit codes. With the
PSNativeCommandErrorActionPreference
experimental feature turned on this causes errors to be emitted to the console.I propose that a variable and/or cmdlet maintain a dictionary of program successful exit codes to be checked at runtime when a program runs a non-zero exit code. The dictionary could also be expanded to include error codes. Imagine a community module(s) with a set of programs and success/error exit codes with descriptions that PowerShell could leverage to return to the user.
A current example with brew:
Expected behavior
Programs that return successful non-zero exit codes; PowerShell does not write errors.
Actual behavior
Errors are written on all non-zero exit codes.
Error details
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: