-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix stderr output of console host to respect NO_COLOR #24391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix stderr output of console host to respect NO_COLOR #24391
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
This was the root cause of a bug I was investigating today. Is this fix expected to be merged in soon? Is there a current workaround? |
|
A current workaround is to set env:__SuppressAnsiEscapeSequences to 1 (or perhaps any value). |
|
Hey @SteveL-MSFT, any updates to share here? Seems like a pretty simple PR, but it's been open for 9 months. 😄 |
|
I would personally love to see this fixed before the next LTS release😺 |
| It 'No_COLOR should be respected for redirected output' { | ||
| $psi = [System.Diagnostics.ProcessStartInfo] @{ | ||
| FileName = 'pwsh' | ||
| # Pass a command that succeeds and normally produces colored output, and one that produces error output. | ||
| Arguments = '-NoProfile -Command Get-Item .; Get-Content \nosuch123' | ||
| # Redirect (capture) both stdout and stderr. | ||
| RedirectStandardOutput = $true | ||
| RedirectStandardError = $true | ||
| } | ||
| $psi.Environment.Add('NO_COLOR', 1) | ||
| ($ps = [System.Diagnostics.Process]::Start($psi)).WaitForExit() | ||
| $ps.StandardOutput.ReadToEnd() | Should -Not -Contain '\e' | ||
| $ps.StandardError.ReadToEnd() | Should -Not -Contain '\e' | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is passed for me in pwsh before the change... Nevertheless the change works for me in console if I manually set the env variable in cmd.exe and then run pwsh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I specifically noticed the original issue when running PowerShell inside a Docker container (for example, using mcr.microsoft.com/powershell:7.5-alpine-3.17). I'd imagine the env var injection into the pwsh process in that scenario is similar to running pwsh from cmd.exe, and why the issue is reproducible by setting the env var outside of pwsh first.
|
/azp run PowerShell-CI-linux-packaging, PowerShell-Windows-Packaging-CI |
|
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
📣 Hey @@SteveL-MSFT, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
|
@SeeminglyScience thank you SO much for getting this merged! Which release should we expect to see this PR's change reflected in? |
|
Thanks for prodding this one along! |
PR Summary
The
WriteErrorLine()path was missing a call toGetOutputString()which removed color when necessary so it always wrote the text in red. The fix is to add the same call as inWriteLine()PR Context
Fix #19961
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.- [ ] Issue filed:
(which runs in a different PS Host).