Skip to content

$? doesn't change in case of parsing error #3798

@dronkoff

Description

@dronkoff

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions