Skip to content

Implement a way to enable color in console even when the output is redirected #33980

Closed

Description

@ejsmith

Currently, console color is disabled by checking to see if the console output is redirected or not.

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.

There is a standard that a lot of other CLI systems use to disable these ANSI escape sequences using an environment variable called NO_COLOR here: https://no-color.org/

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

Activity

  1. added
    untriaged
    on Mar 23, 2020
  2. ejsmith commented on Mar 23, 2020

    @ejsmith
    Author

    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-color NPM package: https://github.com/chalk/supports-color/blob/master/index.js

  3. ejsmith commented on Mar 23, 2020

    @ejsmith
    Author

    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.

  4. NickCraver commented on Mar 24, 2020

    @NickCraver
    Member

    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 :)

  5. ethomson commented on Mar 24, 2020

    @ethomson

    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_COLOR for 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.

    Mentioned in microsoft/vstest#2370:

    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.

  6. ethomson commented on Mar 24, 2020

    @ethomson

    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.

  7. niemyjski commented on Mar 24, 2020

    @niemyjski

    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 💯

  8. am11 commented on Mar 24, 2020

    @am11
    Member

    For automatic CI detection, here is a list of environment variables from different services:

    Variable Check value Service(s)
    CI /TRUE/i or 1 TravisCI, AppVeyor, CirrusCI, CircleCI, GitLab CI
    TF_BUILD /TRUE/i Azure DevOPS
    DOCKERFILE_PATH non empty string Docker Hub CI
    GITHUB_SHA non empty string GitHub Actions
    JENKINS_URL non empty string Jenkins
    bamboo.buildKey non empty string Bamboo
    TEAMCITY_VERSION non empty string TeamCity
  9. ejsmith commented on Mar 24, 2020

    @ejsmith
    Author

    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.

  10. ejsmith commented on Mar 24, 2020

    @ejsmith
    Author

    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.

  11. 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
  12. 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
  13. ethomson commented on Mar 24, 2020

    @ethomson

    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.)

  14. 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
  15. 56 remaining items

  16. KalleOlaviNiemitalo commented on Jul 2, 2021

    @KalleOlaviNiemitalo
  17. jzabroski commented on Jul 2, 2021

    @jzabroski
    Contributor

    @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.

  18. jzabroski commented on Jul 2, 2021

    @jzabroski
    Contributor

    @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

  19. stephentoub commented on Jul 2, 2021

    @stephentoub
    Member

    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.

  20. jzabroski commented on Jul 2, 2021

    @jzabroski
    Contributor

    @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.

  21. ejsmith commented on Jul 3, 2021

    @ejsmith
    Author

    quite ridiculous

    it's been years and multiple major versions

    .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. 🙂

  22. 0xced commented on Jul 5, 2021

    @0xced
    Contributor

    Thanks Stephen for taking over this pull request, very much appreciated!

  23. added a commit that references this issue on Jul 9, 2021
    bb2b3ef
  24. added a commit that references this issue on Jul 9, 2021
    0d848f6
  25. ghost removed
    in-pr
    on Jul 9, 2021
  26. ejsmith commented on Jul 10, 2021

    @ejsmith
    Author

    Thank you @0xced and @stephentoub !!

  27. ghost locked as resolved and limited conversation to collaborators on Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions