Skip to content

Redirection of external command output breaks ANSI sequences #16775

@PatTheMav

Description

@PatTheMav

Prerequisites

Steps to reproduce

  • Have Git for Windows installed
  • Run Powershell 7.21 (either in ConHost or the new Terminal app, bug is reproducible in both)
  • Create the following simple function:
function Test-Ansi {
    $VerbosePreference = 'Continue'
    Write-Verbose '------ Before ------'
    & "$Env:ProgramFiles\Git\usr\bin\patch.exe" --help > $null
    Write-Verbose '------ After ------'
}
  • Observe properly formatted coloring for the line before
  • Observe line after being uncolored and ANSI sequences instead printed verbatim

Additional Notes

This behaviour was only observed in PowerShell 7 - running the same function on PowerShell 5 yields the expected behaviour with no breaking changes to the ANSI output.

It also doesn't make a difference whether the redirection is done via | Out-Null or doing a $null assignment. Even just redirecting stderr via 2>$null triggers the issue. Not redirecting the command is the only way to have it run without breaking.

The issue also occurs independent of invocation, Invoke-Expression results in this bug as does using Start-Command.

Also running this slightly tweaked function:

function Test-Ansi-Async {
    $VerbosePreference = 'Continue'
    Write-Verbose '------ Before ------'
    Start-Process -FilePath "C:\Program Files\Git\usr\bin\patch.exe" -RedirectStandardOutput NUL -ArgumentList '--help' -NoNewWindow
    Write-Verbose '------ After ------'
}

will yield different bugs in ConHost or Windows Terminal:

  • In ConHost browsing through command history will have corrupted output, with characters remaining on screen between history items
  • In Windows Terminal the ANSI color codes of the next command will be corruped

(See screenshots for each case).

In both cases pressing "Enter" without any input restores proper output at the prompt.

Expected behavior

VERBOSE: ------ Before ------
VERBOSE: ------ After ------

Actual behavior

VERBOSE: ------ Before ------
←[33;1mVERBOSE: ------ After ------←[0m

Error details

PS C:\Windows\System32› Get-Error
PS C:\Windows\System32›

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.1
PSEdition                      Core
GitCommitId                    7.2.1
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

Visual Corruption in ConHost

2022-01-20_ConHost-ANSI-Bug

Broken ANSI Codes in Windows Terminal

2022-01-20_Terminal-ANSI-Bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions