You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Streamed executable output prints Ansi Escaping in Write-* Commandlets #18771
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
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
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)
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?
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?
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.
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.
Activity
mklement0 commentedon Dec 12, 2022
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.Write-*cmdlet that subsequent calls then work as expected.SteveL-MSFT commentedon Dec 12, 2022
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)
mklement0 commentedon Dec 12, 2022
Steve Lee (@SteveL-MSFT), given that
'foo' | Write-verbose -Verboseworks just fine, don't you think that PowerShell's handling of external programs is involved here?mklement0 commentedon Dec 12, 2022
Herrmel commentedon Dec 13, 2022
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?
zadjii-msft commentedon Dec 14, 2022
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.
mklement0 commentedon Dec 14, 2022
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:
Herrmel commentedon Dec 16, 2022
Maybe this PR has something to do with it? #16612
Herrmel commentedon Dec 19, 2022
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.Herrmel commentedon Feb 9, 2023
Steve Lee (@SteveL-MSFT) ???