Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Redirection of stderr output from external programs sets $? invariably to $false, no longer reflecting whether $LASTEXITCODE equals 0 #10512

Copy link
Copy link
@mklement0

Description

@mklement0
Issue body actions

Closely related: #4002

Normally, immediately after execution of an external program, $? and $LASTEXITCODE -eq 0 can be used interchangeably to test for success.

However, under the following conditions $? is invariably set to $false, even if $LASTEXITCODE contains 0 and therefore indicates success:

  • If a 2> or *> redirection is involved
  • and actual stderr output is produced by the external program.

Note that you generally cannot and shouldn't infer success vs. failure of an external program from the presence of stderr output - many CLIs use stderr to emit status information; only the process exit code should be consulted.

Steps to reproduce

Run the following on Windows, which produces both stderr and stdout output and reports 0 as the process exit code:

& { cmd /c 'nosuch & ver' *>$null; $?; $LASTEXITCODE } | Should -Be $true, 0

Expected behavior

The test should succeed.

Actual behavior

The test fails:

Expected @($true, 0), but got @($false, 0).

That is, $? was $false, even though $LASTEXITCODE was 0.

Environment data

PowerShell Core 7.0.0-preview.3
Windows PowerShell v5.1.18362.145
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.