Skip to content

Streamed executable output prints Ansi Escaping in Write-* Commandlets #18771

Closed

Description

@Herrmel

Prerequisites

Steps to reproduce

When piping input of an application to an Commandlet like Write-Error the written content contains the ansi escaping signs.
For example the command:

dotnet --version | Write-Verbose -Verbose

Expected behavior

Printing the following output in yellow:

VERBOSE: 7.0.100

Actual behavior

The following Text is printed colorless:

�[33;1mVERBOSE: 7.0.100�[0m

Error details

If the output is intercepted with Out-String the output seems to work correctly unless the Parameter -Stream is used. Seems like the Encoding is only broken when the text is returned as a stream down the pipline instead of at the end of the command.

For some very odd reason after I type an incorrect parameter like this:

dotnet --version | Write-Verbose -u # -u does not exist

The error-message is again displayed with ansi escape signs, but afterwards everything works as expected when I use the previous command again.

Environment data

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

Visuals

image

Activity

  1. added
    Needs-Triage
    on Dec 12, 2022
  2. mklement0 commented on Dec 12, 2022

    @mklement0
    Contributor

    To summarize: the problem occurs when ALL of the following conditions are met:

    • Running on Windows in a regular (conshost.exe) console window (as opposed to Windows Terminal or Visual Studio Code's integrated terminal)

    • Piping from an external program to a Write-* cmdlet that normally produces colored output.

    # Prints the escape sequences verbatim instead of *rendering* them.
    cmd /c ver | Write-Error
    • It is only after such a pipeline has provoked an error in the Write-* cmdlet that subsequent calls then work as expected.
    # Provoke an error in Write-Error.
    # The initial symptom still manifests the *first* time you call this,
    # but goes away in all subsequent calls (whether they provoke an error in the Write-* cmdlet or not,
    # and whether a different Write-* cmdlet is used or not).
    cmd /c ver | Write-Error -NoSuchParam
  3. SteveL-MSFT commented on Dec 12, 2022

    @SteveL-MSFT
    Member

    This appears to be a problem with conhost specifically. I don't think there's anything we can do in this repo as the ESC char isn't being rendered correctly in this case in conhost. I would suggest opening an issue here: https://github.com/microsoft/terminal (it's also for conhost AFAIK)

  4. added
    Resolution-External
    and removed
    Needs-Triage
    on Dec 12, 2022
  5. mklement0 commented on Dec 12, 2022

    @mklement0
    Contributor

    Steve Lee (@SteveL-MSFT), given that 'foo' | Write-verbose -Verbose works just fine, don't you think that PowerShell's handling of external programs is involved here?

  6. mklement0 commented on Dec 12, 2022

    @mklement0
  7. Herrmel commented on Dec 13, 2022

    @Herrmel
    Author

    Steve Lee (@SteveL-MSFT), given that 'foo' | Write-verbose -Verbose works just fine, don't you think that PowerShell's handling of external programs is involved here?

    I thinnk so as well since it worked perfectly fine in my prevous version (7.2.4) I think. That version also used conhost, right?

  8. ghost closed this as completedon Dec 14, 2022
  9. zadjii-msft commented on Dec 14, 2022

    @zadjii-msft

    Wait, is this a Console issue? Looks to me like sometimes the text is getting formatted with escapes and other times, it isn't. When a raw ESC is getting written to the buffer like that, it's usually a good indication that some client was trying to write VT to the console without enabling ENABLE_VIRTUAL_TERMINAL_PROCESSING. By default, the console disables that flag (and IIRC, the Terminal has it turned on by default[1]). When that flag is disabled, then client apps are fully free to write a literal ESC into the buffer.

    Unfortunately, I don't really know enough about the machinations of powershell to determine what's going on in each of the above lines. I'd guess that sometimes pwsh is using the console API to format the output, and other times it's using VT.

    [1]: mild fact, close enough to the truth for this thread.

  10. mklement0 commented on Dec 14, 2022

    @mklement0
    Contributor

    Mike Griese (@zadjii-msft), PowerShell generally has VT processing enabled, but has special handling for native programs, including turning support back on after a command - but I'm hazy on the details. I assume https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs is a good place to start.

    A(nother) simple demonstration that it is PowerShell that is the problem here, not the Windows console host, is this:

    # OK, due to use of Out-String as an intermediary.
    cmd /c ver | Out-String | Write-Verbose -Verbose
  11. Herrmel commented on Dec 16, 2022

    @Herrmel
    Author

    Maybe this PR has something to do with it? #16612

  12. Herrmel commented on Dec 19, 2022

    @Herrmel
    Author

    Steve Lee (@SteveL-MSFT) How are we going to proceed? I would say at least reopen this until it is confirmed to be external. As of now everybody seems to think PowerShell does have problems with enabling VT-Output with ENABLE_VIRTUAL_TERMINAL_PROCESSING. #18294 seems like the same issue and also got closed without any further investigation.

  13. Herrmel commented on Feb 9, 2023

    @Herrmel
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-External

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions