Enable terminal colors in CI
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.
Already implemented.
check you CI env var maybe no color is enabled
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
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.
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.
I just used docker to have a local ubuntu environment.
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.
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.
Do we want that on node core ??? and do we have a fix for that using force_color ?
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.
Wait, we already have the hardcoded CI variables: https://github.com/nodejs/node/blob/fca4107e76f0931ecfb6f90e7ad41bfd298e1e71/lib/internal/tty.js#L161-L174
This might actually be a bug in util.styleText after all.
This might actually be a bug in util.styleText after all.
I think the function itself is good, it's the function itself https://github.com/nodejs/node/blob/fca4107e76f0931ecfb6f90e7ad41bfd298e1e71/lib/internal/util/colors.js#L18
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the https://github.com/nodejs/node/labels/never-stale label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document.