Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow || to override $PSNativeCommandUseErrorActionPreference = $true, for ad-hoc error handling / ignoring nonzero exit codes #20067

Open
mklement0 opened this issue Aug 2, 2023 · 0 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@mklement0
Copy link
Contributor

mklement0 commented Aug 2, 2023

Summary of the new feature / enhancement

$PSNativeCommandUseErrorActionPreference = $true currently cannot be overridden for individual calls, even though that would be handy to deal with (a) anticipated failures and (b) non-conventional CLIs such as robocopy.exe and choice.exe that use nonzero exit codes to (also) signal success conditions.

Analogous to how you can use || in Bash to override its roughly equivalent setting (set -e; e.g, bash -c 'set -e ; ls nosuch || : ; echo still here'), PowerShell would benefit from this ability too.

That is:

  • If a native-program call is the LHS of the || operator, do not emit the System.Management.Automation.NativeCommandExitException-based error when $PSNativeCommandUseErrorActionPreference = $true is in effect.

  • This allows either custom error handling via the RHS operand, or ignoring the nonzero exit code altogether via @() (which is in effect a no-op, similar to Bash's : builtin, though, unlike the latter, @() does not reset $LASTEXITCODE).

Example:

$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'  # This will currently cause all NativeCommandExitExceptions to abort the script.

# WISHFUL THINKING:
# Effectively ignore the nonzero exit code, without aborting the script.
choice.exe /m Continue? /c ync || @()
# ... examine $LASTEXITCODE.

# WISHFUL THINKING:
# Custom handling of a command that may fail, without aborting the script.
'foo' | findstr bar 2>$null || Write-Warning 'string not found'

# If support for the || override were implemented, execution would continue,
# and 'still here' would print.
'still here'

Conceptually related proposal (whose scope is larger) that would solve the problem differently (Invoke-NativeCommand { ls nosuchfile } -IgnoreExitCode):

Proposed technical implementation details (optional)

No response

@mklement0 mklement0 added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
Development

No branches or pull requests

1 participant