PowerShell-RFC icon indicating copy to clipboard operation
PowerShell-RFC copied to clipboard

Native Command Error Handling

Open theJasonHelmick opened this issue 4 years ago • 12 comments

Link to original: #261

theJasonHelmick avatar Feb 09 '21 16:02 theJasonHelmick

In Bash, it is possible to temporarily disable set -e by using command || true, e.g.

set -e
cat ./nonexistent || true
echo "Test" # will be written

From the specification, it is unclear whether a similar trick will work with $PSNativeCommandErrorAction. I believe it should be written explicitly.

ForNeVeR avatar Mar 28 '21 06:03 ForNeVeR

@ForNeVeR you would do this:

$PSNativeCommandErrorAction = $true
& {
    $PSNativeCommandErrorAction = $false
    cat ./nonexistent
    "Test" # will be written
}

cat ./nonexistent
"Will not get output"

The disabling of $PSNativeCommandErrorAction will only be valid for that scriptblock.

@theJasonHelmick When can we get this? Please let this ship in 7.2 - at least as an experimental feature!!! Maybe I should submit a PR to remove Start-NativeExecution from the PowerShell build scripts. I think the team isn't feeling the pain as much as the rest of us in the community. :-)

rkeithhill avatar Mar 29 '21 05:03 rkeithhill

@PowerShell/powershell-committee reviewed this and are okay to move forward with an experimental implementation based on this RFC.

However, it's not currently committed for 7.2. As such, we're adding the up-for-grabs label.

joeyaiello avatar Mar 31 '21 22:03 joeyaiello

@theJasonHelmick The RFC does not mention anything about this being an experimental feature. I don't think that it needs to be experimental since it is "opt-in" via setting $PSNativeCommandUseErrorActionPreference to $true. Just wanted to get clarification on this. Do you agree?

rkeithhill avatar Jul 23 '21 06:07 rkeithhill

@theJasonHelmick Have you seen this ApplicationFailedException? This covers the case where a system exception is thrown when PS tries to run the application. My concern is that someone looking at the code base may be confused by seeing ApplicationFailedException and NativeCommandException.

rkeithhill avatar Jul 24 '21 06:07 rkeithhill

@rkeithhill even as an opt-in feature, it still needs to be marked as an experimental feature to denote to end-users that the design is not final and may change in the future.

Ideally, it should be code-fenced such that the opt-in isn't possible without the experimental feature turned on, but if the Maintainers agree with you that it's prohibitively difficult to do that, it should still have a placeholder experimental feature (where turning it off would do nothing) to denote the potential instability of the interface.

joeyaiello avatar Jul 27 '21 19:07 joeyaiello

@rkeithhill agreement that, especially given what's in the docs today for ApplicationFailedException ("Defines the exception that is thrown if a native command fails", it might make sense to rename NativeCommandException to something else.

@SteveL-MSFT is suggesting NativeCommandExitException, but we're open to other possibilities.

We're also referring this RFC to the Engine WG for review.

joeyaiello avatar Jul 27 '21 19:07 joeyaiello

~~Why not re-use ApplicationFailedException if it is about exit?~~

iSazonov avatar Jul 28 '21 04:07 iSazonov

@theJasonHelmick - would you please confirm that the implemented experimental feature matches what is written here?

JamesWTruher avatar Mar 09 '22 23:03 JamesWTruher

Is this now part of PowerShell as an experimental features (cannot find it in 7.2.2) or when will it come?

per-oestergaard avatar Mar 23 '22 13:03 per-oestergaard

It's in the current 7.3.0 preview - PSNativeCommandErrorActionPreference. After enabling the feature and restarting pwsh, try this:

whoami -xyzzy
get-date
& {
    $ErrorActionPreference = 'Stop'
    $PSNativeCommandUseErrorActionPreference = $true
    whoami -xyzzy
    get-date
}

rkeithhill avatar Mar 23 '22 15:03 rkeithhill