fix(runtime): output to stderr with colors if a tty and stdout is piped (#23813)

This also fixes a bug where Deno would output to stderr with colours
when piped and stdout was not piped.
This commit is contained in:
David Sherret 2024-05-14 17:32:09 -04:00 committed by GitHub
parent 1e2b0a2219
commit e39b94f3aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 61 additions and 33 deletions

View file

@ -8,7 +8,8 @@ import { core, internals, primordials } from "ext:core/mod.js";
const ops = core.ops;
import {
op_bootstrap_args,
op_bootstrap_is_tty,
op_bootstrap_is_stderr_tty,
op_bootstrap_is_stdout_tty,
op_bootstrap_no_color,
op_bootstrap_pid,
op_main_module,
@ -62,10 +63,10 @@ import * as timers from "ext:deno_web/02_timers.js";
import {
customInspect,
getDefaultInspectOptions,
getNoColor,
getStderrNoColor,
inspectArgs,
quoteString,
setNoColorFn,
setNoColorFns,
} from "ext:deno_console/01_console.js";
import * as performance from "ext:deno_web/15_performance.js";
import * as url from "ext:deno_url/00_url.js";
@ -379,7 +380,10 @@ function importScripts(...urls) {
const opArgs = memoizeLazy(() => op_bootstrap_args());
const opPid = memoizeLazy(() => op_bootstrap_pid());
setNoColorFn(() => op_bootstrap_no_color() || !op_bootstrap_is_tty());
setNoColorFns(
() => op_bootstrap_no_color() || !op_bootstrap_is_stdout_tty(),
() => op_bootstrap_no_color() || !op_bootstrap_is_stderr_tty(),
);
function formatException(error) {
if (
@ -390,11 +394,11 @@ function formatException(error) {
} else if (typeof error == "string") {
return `Uncaught ${
inspectArgs([quoteString(error, getDefaultInspectOptions())], {
colors: !getNoColor(),
colors: !getStderrNoColor(),
})
}`;
} else {
return `Uncaught ${inspectArgs([error], { colors: !getNoColor() })}`;
return `Uncaught ${inspectArgs([error], { colors: !getStderrNoColor() })}`;
}
}