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
Implement a way to enable color in console even when the output is redirected #33980
I understand why this was done because there are scenarios that don't support ANSI escape sequences and the output looks really ugly in those scenarios when a bunch of non-rendered ANSI escape sequences are mixed into the output of the CLI.
This may seem trivial, but when all CI systems like Jenkins, AppVeyor, GitHub Actions and pretty much every single other CI system simply record the redirected console output of the build resulting in a big blob of monotone text and a human is expected to look at that output and quickly identify the issues then it becomes a big problem for usability with .NET Core as a whole.
I am proposing that .NET Core is changed to enable ANSI color sequences by default and that it checks for the NO_COLOR environment variable to disable them.
This issue is being created after a discussion about this topic here: microsoft/vstest#2370
Reacted by Blake Niemyjski, Ganbarukamo41, Sander Rijken, Claire Novotny, Hadrian Tang, Martijn Laarman, Will Rogers, Dennis Roche, Kieran Benton, Nirmal Guru and 16 more
Was thinking that the most likely reason why this was introduced in the first place is that, until recently, Windows didn't even support ANSI escape sequences. So maybe this could be implemented as a smart default where it's automatically turned it off on Windows versions less than 10.10586 which is the first build of Windows that started supporting ANSI.
I think there are a lot of problems flipped from "default off" to "default on" that would break a lot of systems. I suggest re-titling the issue to be a bit broader to not narrow into a specific fix, e.g. "Allow a way to enable dotnet colorized output, even if console output is redirected". That's really what we're trying to solve - and for .NET that generally needs to be in a non-breaking way.
This could be an environmental variable, or just like on the NO_COLOR site it could be dotnet * --color=true (or whatever is semantically "familiar") as a CLI option...or something else :)
I think there are a lot of problems flipped from "default off" to "default on" that would break a lot of systems. I suggest re-titling the issue to be a bit broader to not narrow into a specific fix, e.g. "Allow a way to enable dotnet colorized output, even if console output is redirected". That's really what we're trying to solve - and for .NET that generally needs to be in a non-breaking way.
I agree. When the output is not a tty (or "when the console output is redirected") is often because you're writing to a file. You would almost never want inband control characters / ANSI color codes in that output. Running in a CI system is the exception here, not the rule.
You may want to honor NO_COLORfor interactive use - that would be a welcome improvement for people (like me) who started using text consoles before we had these high-falutin' colors and likes it that way. (Also, please get off my lawn.) But NO_COLOR is - as best I can understand - about interactive use, not redirected use / when isatty is false.
For non-interactive use, you really must default to no colors and then opt-in the CI systems explicitly. As a CI vendor, this is what I want and expect you to do for our customers in common.
I think introducing heuristics similar to Chalk's is quite dangerous as they will become maintenance hell and will effectively turn dotnet into a bottleneck when new CIs are introduced/need to be added.
Yes, this is true. (Though I'd suggest abstracting it out into a separate package.) As a CI vendor, we want to make sure that our shared users have colors. We will take on this burden of contributing support.
Speaking of a separate package - I'm like 99% sure that cake already has this logic to detect when it's running in CI. Whether that's already abstracted out into a separate package or could be, I don't know, but it may be worth looking there first.
I'd think in 2020, we'd be smart enough to detect our environments and show color automatically for a really nice experience instead of opting into it. I don't opt into it in other programming languages, it just works.
Also, the cool kids have color and we need it too 💯
I think there are a lot of problems flipped from "default off" to "default on" that would break a lot of systems. I suggest re-titling the issue to be a bit broader to not narrow into a specific fix, e.g. "Allow a way to enable dotnet colorized output, even if console output is redirected". That's really what we're trying to solve - and for .NET that generally needs to be in a non-breaking way.
I understand that the bar is high for breaking changes in .NET, but not sure that this is actually a breaking change. It would end up being ugly output in the few (IMO) cases where ANSI is not supported. I think it's REALLY important for .NET to have a great experience out of the box for people. The CLI is for developers to use and for developers to look at the output and be able to easily interpret it. I would think that the cases where you wouldn't want ANSI colors are far far less than the cases you do want it. A normal developer is not going to know they need to opt into having color. So we would either need all CI services to agree on some environment variable or we would need the code to check for all of the known CI services which as someone pointed out in the other thread seems pretty hacky and not something that I think the .NET team is going to want in their code.
Yes, this is true. (Though I'd suggest abstracting it out into a separate package.) As a CI vendor, we want to make sure that our shared users have colors. We will take on this burden of contributing support.
I am pretty sure that the .NET team isn't going to add a nuget package for something in the core runtime code. So that leaves us with adding code that needs to be maintained to detect all of the known CI services which seems also unlikely that the .NET team would be OK with in the core runtime code.
That is why I think whatever we do it's going to have to be kept simple. Either an opt-in or an opt-out command line flag and an environment variable. I personally feel like it should be opt-out or some sort of smart default.
changed the title [-]Use NO_COLOR environment variable for disabling console color[/-][+]Implement a way to enable color in console even when the console output is not redirected[/+]on Mar 24, 2020
changed the title [-]Implement a way to enable color in console even when the console output is not redirected[/-][+]Implement a way to enable color in console even when the output is not redirected[/+]on Mar 24, 2020
I personally feel like it should be opt-out or some sort of smart default.
Smart defaults are the status quo: color on the console, no color when going to log files. This means that interactive users get delightful colors (yay!) and nobody gets ANSI color codes spammed all over their log files (boo).
What you want is an option to turn off colors on the console (the NO_COLOR environment variable approach). And an option to enable colors when the output is redirected. Or more accurately, something that isn't a tty, like a CI system.
(The title of the issue is misleading - at the moment, color is always enabled in the console when the output is not redirected. microsoft/vstest#2370 is asking to enable color when the console is redirected.)
changed the title [-]Implement a way to enable color in console even when the output is not redirected[/-][+]Implement a way to enable color in console even when the output is redirected[/+]on Mar 24, 2020
@0xced I understand what it's like to have a PR open with Microsoft. I have had some that were open for almost 3 years before they finally got merged (and I thought the project was left for dead, only to discover that PR's acceptance ignited more development from the open source community).
It's difficult to know how people will build upon your work, but I for one am thrilled with your efforts. If you're ever in Boston, I will buy you a beer if you can find me.
@KathleenDollard, I take it there's no interest in an end-to-end coordinated story here around color, driven by the CLI, for consistency across the ecosystem. Given that, we'll just add the limited one-off, Unix-specific env var to Console and call it day. Thanks.
I had actually discussed this with Rich Turner, and he gave me a thoughtful reply from the console team's perspective. See: dotnet/msbuild#4299 (comment)
Short summary of Rich's advice:
you should start planning to adopt VT, and avoid Console APIs
-- @bitcrazed
end-to-end coordinated story here around color, driven by the CLI, for consistency across the ecosystem
I had actually discussed this with Rich Turner, and he gave me a thoughtful reply from the console team's perspective
Thanks, but my end-to-end comments aren't specific to Console. They're about ensuring that everyone, regardless of how they're outputting color and whether they're using System.Console or not, plays by the same set of rules, e.g. if an app does manually emit escape codes via text as in that example, what should that app do / respect to enable or disable doing so.
@stephentoub I agree with you about a set of rules. In the thread I linked, I proposed "request-based mediated execution" a'la REST as an improvement to linux-style troff sub-system which couples the formatting to the left-pipe and right-pipe and requires duplicating knowledge about that media metadata. I have pushed for this design for several years. It would be awesome.
Using an env variable like NO_COLOR is a proxy for having a true browser-as-shell experience where the mime type plaintext is respected. But it's out-of-channel rather than in-channel. You just need to squint to see the future 20-30 years from now.
.NET 5 shipped in November of 2020. What other major version was released since this issue (which is in the 6.0 milestone) was opened in March 2020?
I get that you're frustrated, and I get that the bot auto-closing the PR only added to that. But calling this "ridiculous" and employing hyperbole won't win any points.
My $.02.
I get it. I overreacted a bit. I apologize. The reason I said multiple major versions is because it missed .NET 5 and it appeared that it wasn't going to happen for .NET 6 since the PR was closed and it seems like we've got to be getting close to lockdown mode for .NET 6. Again, I apologize. Thank you very much for looking into this and for getting that PR updated and merged in time for .NET 6. 🙂
Activity
ejsmith commentedon Mar 23, 2020
Also, I think it is relevant to show what other CLI tools are doing to detect color support in the NodeJS world. Here is the
supports-colorNPM package: https://github.com/chalk/supports-color/blob/master/index.jsejsmith commentedon Mar 23, 2020
Was thinking that the most likely reason why this was introduced in the first place is that, until recently, Windows didn't even support ANSI escape sequences. So maybe this could be implemented as a smart default where it's automatically turned it off on Windows versions less than 10.10586 which is the first build of Windows that started supporting ANSI.
NickCraver commentedon Mar 24, 2020
I think there are a lot of problems flipped from "default off" to "default on" that would break a lot of systems. I suggest re-titling the issue to be a bit broader to not narrow into a specific fix, e.g. "Allow a way to enable
dotnetcolorized output, even if console output is redirected". That's really what we're trying to solve - and for .NET that generally needs to be in a non-breaking way.This could be an environmental variable, or just like on the
NO_COLORsite it could bedotnet * --color=true(or whatever is semantically "familiar") as a CLI option...or something else :)ethomson commentedon Mar 24, 2020
I agree. When the output is not a tty (or "when the console output is redirected") is often because you're writing to a file. You would almost never want inband control characters / ANSI color codes in that output. Running in a CI system is the exception here, not the rule.
You may want to honor
NO_COLORfor interactive use - that would be a welcome improvement for people (like me) who started using text consoles before we had these high-falutin' colors and likes it that way. (Also, please get off my lawn.) ButNO_COLORis - as best I can understand - about interactive use, not redirected use / whenisattyis false.For non-interactive use, you really must default to no colors and then opt-in the CI systems explicitly. As a CI vendor, this is what I want and expect you to do for our customers in common.
Mentioned in microsoft/vstest#2370:
Yes, this is true. (Though I'd suggest abstracting it out into a separate package.) As a CI vendor, we want to make sure that our shared users have colors. We will take on this burden of contributing support.
ethomson commentedon Mar 24, 2020
Speaking of a separate package - I'm like 99% sure that cake already has this logic to detect when it's running in CI. Whether that's already abstracted out into a separate package or could be, I don't know, but it may be worth looking there first.
niemyjski commentedon Mar 24, 2020
I'd think in 2020, we'd be smart enough to detect our environments and show color automatically for a really nice experience instead of opting into it. I don't opt into it in other programming languages, it just works.
Also, the cool kids have color and we need it too 💯
am11 commentedon Mar 24, 2020
For automatic CI detection, here is a list of environment variables from different services:
CITF_BUILDDOCKERFILE_PATHGITHUB_SHAJENKINS_URLbamboo.buildKeyTEAMCITY_VERSIONejsmith commentedon Mar 24, 2020
I understand that the bar is high for breaking changes in .NET, but not sure that this is actually a breaking change. It would end up being ugly output in the few (IMO) cases where ANSI is not supported. I think it's REALLY important for .NET to have a great experience out of the box for people. The CLI is for developers to use and for developers to look at the output and be able to easily interpret it. I would think that the cases where you wouldn't want ANSI colors are far far less than the cases you do want it. A normal developer is not going to know they need to opt into having color. So we would either need all CI services to agree on some environment variable or we would need the code to check for all of the known CI services which as someone pointed out in the other thread seems pretty hacky and not something that I think the .NET team is going to want in their code.
ejsmith commentedon Mar 24, 2020
I am pretty sure that the .NET team isn't going to add a nuget package for something in the core runtime code. So that leaves us with adding code that needs to be maintained to detect all of the known CI services which seems also unlikely that the .NET team would be OK with in the core runtime code.
That is why I think whatever we do it's going to have to be kept simple. Either an opt-in or an opt-out command line flag and an environment variable. I personally feel like it should be opt-out or some sort of smart default.
[-]Use NO_COLOR environment variable for disabling console color[/-][+]Implement a way to enable color in console even when the console output is not redirected[/+][-]Implement a way to enable color in console even when the console output is not redirected[/-][+]Implement a way to enable color in console even when the output is not redirected[/+]ethomson commentedon Mar 24, 2020
Smart defaults are the status quo: color on the console, no color when going to log files. This means that interactive users get delightful colors (yay!) and nobody gets ANSI color codes spammed all over their log files (boo).
What you want is an option to turn off colors on the console (the
NO_COLORenvironment variable approach). And an option to enable colors when the output is redirected. Or more accurately, something that isn't a tty, like a CI system.(The title of the issue is misleading - at the moment, color is always enabled in the console when the output is not redirected. microsoft/vstest#2370 is asking to enable color when the console is redirected.)
[-]Implement a way to enable color in console even when the output is not redirected[/-][+]Implement a way to enable color in console even when the output is redirected[/+]56 remaining items
KalleOlaviNiemitalo commentedon Jul 2, 2021
jzabroski commentedon Jul 2, 2021
@0xced I understand what it's like to have a PR open with Microsoft. I have had some that were open for almost 3 years before they finally got merged (and I thought the project was left for dead, only to discover that PR's acceptance ignited more development from the open source community).
It's difficult to know how people will build upon your work, but I for one am thrilled with your efforts. If you're ever in Boston, I will buy you a beer if you can find me.
jzabroski commentedon Jul 2, 2021
I had actually discussed this with Rich Turner, and he gave me a thoughtful reply from the console team's perspective. See: dotnet/msbuild#4299 (comment)
Short summary of Rich's advice:
stephentoub commentedon Jul 2, 2021
Thanks, but my end-to-end comments aren't specific to Console. They're about ensuring that everyone, regardless of how they're outputting color and whether they're using System.Console or not, plays by the same set of rules, e.g. if an app does manually emit escape codes via text as in that example, what should that app do / respect to enable or disable doing so.
jzabroski commentedon Jul 2, 2021
@stephentoub I agree with you about a set of rules. In the thread I linked, I proposed "request-based mediated execution" a'la REST as an improvement to linux-style troff sub-system which couples the formatting to the left-pipe and right-pipe and requires duplicating knowledge about that media metadata. I have pushed for this design for several years. It would be awesome.
Using an env variable like
NO_COLORis a proxy for having a true browser-as-shell experience where the mime type plaintext is respected. But it's out-of-channel rather than in-channel. You just need to squint to see the future 20-30 years from now.ejsmith commentedon Jul 3, 2021
I get it. I overreacted a bit. I apologize. The reason I said multiple major versions is because it missed .NET 5 and it appeared that it wasn't going to happen for .NET 6 since the PR was closed and it seems like we've got to be getting close to lockdown mode for .NET 6. Again, I apologize. Thank you very much for looking into this and for getting that PR updated and merged in time for .NET 6. 🙂
0xced commentedon Jul 5, 2021
Thanks Stephen for taking over this pull request, very much appreciated!
Add possibility to write ANSI color escape codes when the console out…
Add possibility to write ANSI color escape codes when the console out…
ejsmith commentedon Jul 10, 2021
Thank you @0xced and @stephentoub !!