Skip to content
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

console: adhere to environment variables enforcing colors #32308

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ changes:
- version: v11.7.0
pr-url: https://github.com/nodejs/node/pull/24978
description: The `inspectOptions` option is introduced.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/32308
description: The `colorMode` default adheres to the environment variables
enforcing or disabling colors.
-->

* `options` {Object}
Expand All @@ -99,12 +103,12 @@ changes:
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying
streams. **Default:** `true`.
* `colorMode` {boolean|string} Set color support for this `Console` instance.
Setting to `true` enables coloring while inspecting values. Setting to
`false` disables coloring while inspecting values. Setting to
`'auto'` makes color support depend on the value of the `isTTY` property
and the value returned by `getColorDepth()` on the respective stream. This
option can not be used, if `inspectOptions.colors` is set as well.
**Default:** `'auto'`.
Set to `true` to enable coloring while inspecting values and set to `false`
to disable coloring. Set to `'auto'` checks the `FORCE_COLOR`, `NO_COLOR`
and `NODE_DISABLE_COLORS` environment variables. If none is set, makes color
support depend on the value of the `isTTY` property and the value returned
by `hasColors()` on the respective stream. This option can not be used, if
`inspectOptions.colors` is set as well. **Default:** `'auto'`.
* `inspectOptions` {Object} Specifies options that are passed along to
[`util.inspect()`][].

Expand Down
14 changes: 11 additions & 3 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const {
inspect,
formatWithOptions
} = require('internal/util/inspect');
const {
getEnforcedColorBits
} = require('internal/tty');
const {
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
} = require('internal/util/types');
Expand Down Expand Up @@ -266,9 +269,14 @@ const kNoColorInspectOptions = {};
Console.prototype[kGetInspectOptions] = function(stream) {
let color = this[kColorMode];
if (color === 'auto') {
color = stream.isTTY && (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should not be dropped.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention of this PR is to circumvent this check. This is a semver-major PR to adhere to the environment variables. Those should always come first, no matter what kind of stream is used. Otherwise it's not possible to fully (de)activate colors using environment variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those should always come first, no matter what kind of stream is used.

I’m -1 on this, sorry. Console instances should be as self-contained as possible, relying on external state like environment variables is counterintuitive imo and breaks orthogonality.

Otherwise it's not possible to fully (de)activate colors using environment variables.

Right, but I don’t think that should be the case. Custom Console instances should adhere to their options and nothing else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only change the behavior under the following circumstances:

Colors will not be printed anymore when deactivated with environment variables (FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR) for TTY streams that do not have a getColorDepth() function.

Colors will be printed when activated with the FORCE_COLOR environment variable for all streams.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colors will be printed when activated with the FORCE_COLOR environment variable for all streams.

Right – what I’m saying is that I’m -1 on this specifically (because, again, that breaks orthogonality).

Copy link
Member Author

@BridgeAR BridgeAR Mar 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but I don’t think that should be the case. Custom Console instances should adhere to their options and nothing else.

Using a boolean for the color option will do exactly that. It's only about the auto mode which ideally checks for the users intent. Using an environment variable is a clear indicator for the user to request a specific mode.

I’m -1 on this, sorry. Console instances should be as self-contained as possible, relying on external state like environment variables is counterintuitive imo and breaks orthogonality.

But it's still going to be self-contained? The current behavior seems counter-intuitive to me. I would expect environment variables to clearly have an impact on default values. It is one of the strongest possible ways a user is able to request a general behavior.


I'll open a twitter survey to get a bigger opinion pool.

We often use environment variables to override the default value. This is what is also suggested here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Anna's point is that there's no inherent connection between the stream a Console is using and the process's env (they are orthogonal), so even if the mode is auto, using data from the env is nonsensical.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry to necrobump, but I really don't follow the reasoning behind console checking whether a stream is a TTY before calling the getColorDepth function if it exists and is a function. Why does it matter that the environment is orthogonal to the console when the responsibility to determine if colors should be used are handed off to the getColorDepth function? In my own testing I noticed that if isTTY is dropped from the condition I get colored output when piping to a non TTY despite not setting FORCE_COLOR so there is more to it than just dropping that part of the expression to get a behavior that corresponds to my interpretation of the manual.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just сame across the discussion regarding the proposed change to the console color auto mode, and I'd like to express my support for the modification.

The goal of ensuring that the console color auto mode considers environment variables for (de)activating colors is a valuable one.

Here's why I believe this change is beneficial:

Clarity and Predictability: When users set environment variables like FORCE_COLOR, NODE_DISABLE_COLORS, or NO_COLOR, they have a clear intention regarding color output. By checking these variables, the console can align itself with the user's preferences.

User Empowerment: Allowing users to control color output through environment variables empowers them to customize their development environment to suit their needs. It's in line with the philosophy of "configuration over convention."

Consistency: This change promotes consistency by ensuring that environment variables take precedence. Regardless of the stream type (TTY or non-TTY), the user's color preferences are respected.

I believe that by implementing this change, the console becomes more versatile and user-friendly. While it's essential to maintain self-contained behavior in most cases, it's equally important to provide flexibility to accommodate diverse development environments and individual preferences.

Ultimately, using environment variables to influence default behaviors is a common practice in many software projects. It's a powerful way for users to communicate their intent and align tools with their expectations.

typeof stream.getColorDepth === 'function' ?
stream.getColorDepth() > 2 : true);
const colorBits = getEnforcedColorBits(process.env);
if (colorBits !== undefined) {
color = colorBits !== 1;
} else {
color = stream.isTTY && (
typeof stream.hasColors === 'function' ?
stream.hasColors() : true);
}
}

const options = optionsMap.get(this);
Expand Down
15 changes: 12 additions & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,18 @@ const fatalExceptionStackEnhancers = {
colors: defaultColors
}
} = lazyInternalUtilInspect();
const colors = (internalBinding('util').guessHandleType(2) === 'TTY' &&
require('internal/tty').hasColors()) ||
defaultColors;
const {
getEnforcedColorBits,
hasColors
} = require('internal/tty');
let colors = false;
const colorBits = getEnforcedColorBits(process.env);
if (colorBits !== undefined) {
colors = colorBits !== 1;
} else {
const handleType = internalBinding('util').guessHandleType(2);
colors = (handleType === 'TTY' && hasColors()) || defaultColors;
}
try {
return inspect(error, { colors });
} catch {
Expand Down
15 changes: 11 additions & 4 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ function warnOnDeactivatedColors(env) {
}
}

// The `getColorDepth` API got inspired by multiple sources such as
// https://github.com/chalk/supports-color,
// https://github.com/isaacs/color-support.
function getColorDepth(env = process.env) {
function getEnforcedColorBits(env) {
// Use level 0-3 to support the same levels as `chalk` does. This is done for
// consistency throughout the ecosystem.
if (env.FORCE_COLOR !== undefined) {
Expand Down Expand Up @@ -129,6 +126,15 @@ function getColorDepth(env = process.env) {
env.TERM === 'dumb') {
return COLORS_2;
}
}

// The `getColorDepth` API got inspired by multiple sources such as
// https://github.com/chalk/supports-color,
// https://github.com/isaacs/color-support.
function getColorDepth(env = process.env) {
const colorBits = getEnforcedColorBits(env);
if (colorBits !== undefined)
return colorBits;

if (process.platform === 'win32') {
// Lazy load for startup performance.
Expand Down Expand Up @@ -225,5 +231,6 @@ function hasColors(count, env) {

module.exports = {
getColorDepth,
getEnforcedColorBits,
hasColors
};