From 63f10b9f0da0539b499a076f2951526ecae8260a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 20 Jan 2020 21:37:14 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/31429 Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Anna Henningsen --- lib/internal/tty.js | 16 ++++++++++++---- .../test-tty-color-support-warning-2.js | 8 ++++++++ .../test-tty-color-support-warning-2.out | 2 ++ .../pseudo-tty/test-tty-color-support-warning.js | 9 +++++++++ .../test-tty-color-support-warning.out | 2 ++ test/pseudo-tty/test-tty-color-support.out | 7 ------- 6 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 test/pseudo-tty/test-tty-color-support-warning-2.js create mode 100644 test/pseudo-tty/test-tty-color-support-warning-2.out create mode 100644 test/pseudo-tty/test-tty-color-support-warning.js create mode 100644 test/pseudo-tty/test-tty-color-support-warning.out diff --git a/lib/internal/tty.js b/lib/internal/tty.js index 98975fa68a436f..44d51737008eb0 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -73,18 +73,26 @@ const TERM_ENVS_REG_EXP = [ /^vt100/ ]; +let warned = false; function warnOnDeactivatedColors(env) { - let name; + if (warned) + return; + let name = ''; if (env.NODE_DISABLE_COLORS !== undefined) name = 'NODE_DISABLE_COLORS'; - if (env.NO_COLOR !== undefined) - name = 'NO_COLOR'; + if (env.NO_COLOR !== undefined) { + if (name !== '') { + name += "' and '"; + } + name += 'NO_COLOR'; + } - if (name !== undefined) { + if (name !== '') { process.emitWarning( `The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`, 'Warning' ); + warned = true; } } diff --git a/test/pseudo-tty/test-tty-color-support-warning-2.js b/test/pseudo-tty/test-tty-color-support-warning-2.js new file mode 100644 index 00000000000000..34ba49c0dfd1b5 --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning-2.js @@ -0,0 +1,8 @@ +'use strict'; + +require('../common'); + +process.env.NODE_DISABLE_COLORS = '1'; +process.env.FORCE_COLOR = '3'; + +console.log(); diff --git a/test/pseudo-tty/test-tty-color-support-warning-2.out b/test/pseudo-tty/test-tty-color-support-warning-2.out new file mode 100644 index 00000000000000..cd57b3f9f69672 --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning-2.out @@ -0,0 +1,2 @@ + +(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. diff --git a/test/pseudo-tty/test-tty-color-support-warning.js b/test/pseudo-tty/test-tty-color-support-warning.js new file mode 100644 index 00000000000000..ae164958d2d6b9 --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning.js @@ -0,0 +1,9 @@ +'use strict'; + +require('../common'); + +process.env.NO_COLOR = '1'; +process.env.NODE_DISABLE_COLORS = '1'; +process.env.FORCE_COLOR = '3'; + +console.log(); diff --git a/test/pseudo-tty/test-tty-color-support-warning.out b/test/pseudo-tty/test-tty-color-support-warning.out new file mode 100644 index 00000000000000..9360ae1328b2cd --- /dev/null +++ b/test/pseudo-tty/test-tty-color-support-warning.out @@ -0,0 +1,2 @@ + +(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. diff --git a/test/pseudo-tty/test-tty-color-support.out b/test/pseudo-tty/test-tty-color-support.out index f73fbdcd9651f6..e6904d10b8a7b8 100644 --- a/test/pseudo-tty/test-tty-color-support.out +++ b/test/pseudo-tty/test-tty-color-support.out @@ -1,8 +1 @@ (node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. -(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.