KEMBAR78
Fix stderr output of console host to respect NO_COLOR by SteveL-MSFT · Pull Request #24391 · PowerShell/PowerShell · GitHub
Skip to content

Conversation

@SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented Oct 7, 2024

PR Summary

The WriteErrorLine() path was missing a call to GetOutputString() which removed color when necessary so it always wrote the text in red. The fix is to add the same call as in WriteLine()

PR Context

Fix #19961

PR Checklist

Copy link
Collaborator

@kilasuit kilasuit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Oct 18, 2024
@davidmatson
Copy link

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?

@davidmatson
Copy link

A current workaround is to set env:__SuppressAnsiEscapeSequences to 1 (or perhaps any value).

@cf-rdegregory
Copy link

Hey @SteveL-MSFT, any updates to share here? Seems like a pretty simple PR, but it's been open for 9 months. 😄

@bmkaiser
Copy link

I would personally love to see this fixed before the next LTS release😺

Comment on lines +1186 to +1199
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'
}
Copy link
Collaborator

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.

Copy link

@cf-rdegregory cf-rdegregory Aug 6, 2025

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.

@SeeminglyScience SeeminglyScience added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Aug 26, 2025
@SeeminglyScience
Copy link
Collaborator

/azp run PowerShell-CI-linux-packaging, PowerShell-Windows-Packaging-CI

@azure-pipelines
Copy link

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@SeeminglyScience SeeminglyScience merged commit 16aeaba into PowerShell:master Aug 26, 2025
36 of 37 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Aug 26, 2025

📣 Hey @@SteveL-MSFT, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@cf-rdegregory
Copy link

@SeeminglyScience thank you SO much for getting this merged! Which release should we expect to see this PR's change reflected in?

@jborean93
Copy link
Collaborator

Thanks for prodding this one along!

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

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ansi colors not working in devops agent, does not always respect "NO_COLOR", only works with "__SuppressAnsiEscapeSequences"

8 participants