Skip to content

Commit fbc0bd9

Browse files
BridgeARcodebytere
authored andcommitted
tty: do not end in an infinite warning recursion
It was possible that this warning ends up in an infinite recursion. The reason is that printing the warning triggered a color check and that triggered another warning. Limiting it to a single warning prevents this. PR-URL: #31429 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 61e534b commit fbc0bd9

6 files changed

+33
-11
lines changed

Diff for: lib/internal/tty.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,26 @@ const TERM_ENVS_REG_EXP = [
7373
/^vt100/
7474
];
7575

76+
let warned = false;
7677
function warnOnDeactivatedColors(env) {
77-
let name;
78+
if (warned)
79+
return;
80+
let name = '';
7881
if (env.NODE_DISABLE_COLORS !== undefined)
7982
name = 'NODE_DISABLE_COLORS';
80-
if (env.NO_COLOR !== undefined)
81-
name = 'NO_COLOR';
83+
if (env.NO_COLOR !== undefined) {
84+
if (name !== '') {
85+
name += "' and '";
86+
}
87+
name += 'NO_COLOR';
88+
}
8289

83-
if (name !== undefined) {
90+
if (name !== '') {
8491
process.emitWarning(
8592
`The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`,
8693
'Warning'
8794
);
95+
warned = true;
8896
}
8997
}
9098

Diff for: test/pseudo-tty/test-tty-color-support-warning-2.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
process.env.NODE_DISABLE_COLORS = '1';
6+
process.env.FORCE_COLOR = '3';
7+
8+
console.log();

Diff for: test/pseudo-tty/test-tty-color-support-warning-2.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.

Diff for: test/pseudo-tty/test-tty-color-support-warning.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
process.env.NO_COLOR = '1';
6+
process.env.NODE_DISABLE_COLORS = '1';
7+
process.env.FORCE_COLOR = '3';
8+
9+
console.log();

Diff for: test/pseudo-tty/test-tty-color-support-warning.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.

Diff for: test/pseudo-tty/test-tty-color-support.out

-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
11
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2-
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
3-
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
4-
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
5-
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
6-
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
7-
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
8-
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.

0 commit comments

Comments
 (0)