Skip to content

Enable terminal colors in CI #57998

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

Open
remcohaszing opened this issue Apr 24, 2025 · 13 comments
Open

Enable terminal colors in CI #57998

remcohaszing opened this issue Apr 24, 2025 · 13 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@remcohaszing
Copy link
Contributor

remcohaszing commented Apr 24, 2025

What is the problem this feature will solve?

I noticed util.styleText() supports colorizing text in GitLab CI and GitHub Actions in 22.14, but not in 22.15. And in 22.14, only when the color input is an array. The cause is #56722. That PR correctly solves the bug it references. But it does raise a question: Should util.styleText() enable colors if it runs in a CI provider that supports it? I think it would be nice to have.

What is the feature you are proposing to solve the problem?

Enable colorizing text if Node.js runs in a CI provider that is known to support colors.

What alternatives have you considered?

Keep the status quo. Don’t support colorizing text in CI by default.

@remcohaszing remcohaszing added the feature request Issues that request new features to be added to Node.js. label Apr 24, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Apr 24, 2025
@ChaseKnowlden
Copy link

Already implemented.

@AugustinMauroy
Copy link
Member

check you CI env var maybe no color is enabled

@remcohaszing
Copy link
Contributor Author

No. Colors are disabled by default on GitHub actions. I made a reproduction: https://github.com/remcohaszing/node-issue-styletext. You can see the CI output here: https://github.com/remcohaszing/node-issue-styletext/actions/runs/14704349430/job/41260837797

@remcohaszing remcohaszing changed the title Allow terminal colors in CI Enable terminal colors in CI Apr 28, 2025
@AugustinMauroy
Copy link
Member

AugustinMauroy commented Apr 28, 2025

It's strange because I can't reproduce the result on my Mac M1. Could it be GH?? no idea, I'm going to make myself a docker with ubuntu.

EDIT:
I can reproduce it with docker using ubuntu lasted. So it's mean it's an ubuntu issue or node ubuntu build.

@remcohaszing
Copy link
Contributor Author

The following yields the same result:

docker run --rm node:22.15 -e 'console.log(util.styleText(["red"], "array")); console.log(util.styleText("red", "string")); console.dir(process.env)'

Whereas the same command on my own Linux laptop does yield colors:

node -e 'console.log(util.styleText(["red"], "array")); console.log(util.styleText("red", "string")); console.dir(process.env)'

I don’t necessarily really Docker output to be colorized. This can run in many contexts. But I do think it would be nice to support colors in some common specific environments that are known to support colors, notably in CI.

@AugustinMauroy
Copy link
Member

I just used docker to have a local ubuntu environment.

@remcohaszing
Copy link
Contributor Author

You can start an interactive Node.js shell with:

docker run -ti --rm node:22.15

Or an interactive bash shell with:

docker run -ti --rm --entrypoint bash node:22.15

In both these cases util.styleText() yields colored content on my machine.

It just doesn’t add colors for non-interactive stdout. Another example to explicitly disallow colors piping the output through cat.

node -e 'console.log(util.styleText(["red"], "array")); console.log(util.styleText("red", "string"))' | cat

I think everything works as expected. There are just some situations where a non-interactive shell still supports colors. Mainly CI output comes to mind.

@targos
Copy link
Member

targos commented Apr 28, 2025

According to https://github.com/chalk/supports-color/blob/ae809ecabd5965d0685e7fc121efe98c47ad8724/index.js#L126-L129, the only way to enable colors support by default on CI hosts is to hardcode them.

@AugustinMauroy
Copy link
Member

AugustinMauroy commented Apr 28, 2025

Do we want that on node core ??? and do we have a fix for that using force_color ?

@remcohaszing
Copy link
Contributor Author

Ideological I’m in doubt. I believe CI systems that support colors, should signal that.

In practice however, I think this will never happen, but individual users of util.styleText() will receive requests to support this.

As a practical example, I use this in eslint-formatter-gitlab. If I want to colorize output in CI jobs, I need to specify a function like this:

function color(style, text) {
  return styleText(style, text, {
    validateStream: !process.env.GITLAB_CI
  })
}

For an individual case is fine of course, but I think every project should do this. And also every project should support other CI environments as well. So realistically one currently still needs a userland library to support terminal colors.

You could also assume that basic colors are supported when process.env.CI === 'true'. I’m familiar with GitLab and GitHub. I don’t know if it’s common for other CI environments to support colors.

@targos
Copy link
Member

targos commented May 2, 2025

Wait, we already have the hardcoded CI variables:

node/lib/internal/tty.js

Lines 161 to 174 in fca4107

if (env.CI) {
if ([
'APPVEYOR',
'BUILDKITE',
'CIRCLECI',
'DRONE',
'GITHUB_ACTIONS',
'GITLAB_CI',
'TRAVIS',
].some((sign) => sign in env) || env.CI_NAME === 'codeship') {
return COLORS_256;
}
return COLORS_2;
}

This might actually be a bug in util.styleText after all.

@AugustinMauroy
Copy link
Member

This might actually be a bug in util.styleText after all.

I think the function itself is good, it's the function itself

shouldColorize(stream) {

nodejs-github-bot pushed a commit that referenced this issue May 21, 2025
This adds a couple new entries or increases the support depending
on newer data.
I checked ncurses, tput, supports-color, and termstandard on
github. Most updates are from supports-color.

PR-URL: #58146
Refs: #57998
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this issue May 31, 2025
This adds a couple new entries or increases the support depending
on newer data.
I checked ncurses, tput, supports-color, and termstandard on
github. Most updates are from supports-color.

PR-URL: #58146
Refs: #57998
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this issue Jun 10, 2025
This adds a couple new entries or increases the support depending
on newer data.
I checked ncurses, tput, supports-color, and termstandard on
github. Most updates are from supports-color.

PR-URL: #58146
Refs: #57998
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

5 participants
@remcohaszing @targos @ChaseKnowlden @AugustinMauroy and others