node icon indicating copy to clipboard operation
node copied to clipboard

Enable terminal colors in CI

Open remcohaszing opened this issue 8 months ago • 12 comments

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 avatar Apr 24 '25 08:04 remcohaszing

Already implemented.

ChaseKnowlden avatar Apr 24 '25 20:04 ChaseKnowlden

check you CI env var maybe no color is enabled

AugustinMauroy avatar Apr 26 '25 15:04 AugustinMauroy

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 avatar Apr 28 '25 09:04 remcohaszing

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.

AugustinMauroy avatar Apr 28 '25 09:04 AugustinMauroy

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.

remcohaszing avatar Apr 28 '25 11:04 remcohaszing

I just used docker to have a local ubuntu environment.

AugustinMauroy avatar Apr 28 '25 12:04 AugustinMauroy

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.

remcohaszing avatar Apr 28 '25 14:04 remcohaszing

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.

targos avatar Apr 28 '25 14:04 targos

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

AugustinMauroy avatar Apr 28 '25 16:04 AugustinMauroy

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.

remcohaszing avatar May 02 '25 09:05 remcohaszing

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.

targos avatar May 02 '25 09:05 targos

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

AugustinMauroy avatar May 02 '25 09:05 AugustinMauroy

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.

github-actions[bot] avatar Dec 01 '25 01:12 github-actions[bot]