-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
Steps to reproduce
1..5 | buggy_call
$?
The result is $False and this is expected
1..5 | $_
$?
The result is $True and this is not expected
Expected behavior
1..5 | $_
$?
The result of $? should be $False because previous statement results in an error.
Environment data
PS C:\temp> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.15063.296
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.296
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Notes
From Tom Nolan words:
The problem here is that $? Is set during the CheckActionPreference method when an error is encountered. https://github.com/PowerShell/PowerShell/blob/02b5f357a20e6dee9f8e60e3adb9025be3c94490/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs
However, CheckActionPreference is not executed until the AST has been parsed (https://github.com/PowerShell/PowerShell/blob/02b5f357a20e6dee9f8e60e3adb9025be3c94490/src/System.Management.Automation/engine/parser/Compiler.cs) and PowerShell actually attempts to execute the command, stepping through the pipeline.