From c67de43ff3221ae5554398095261d684b6d41dda Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 15 Nov 2023 14:10:12 +0900 Subject: [PATCH] fix(runtime): fix Deno.noColor when stdout is not tty (#21208) --- cli/tests/unit/tty_color_test.ts | 11 +++++++++++ runtime/js/99_main.js | 6 +++--- runtime/ops/bootstrap.rs | 9 ++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cli/tests/unit/tty_color_test.ts b/cli/tests/unit/tty_color_test.ts index 747d92dcc1c29e..3adc16b23945c9 100644 --- a/cli/tests/unit/tty_color_test.ts +++ b/cli/tests/unit/tty_color_test.ts @@ -13,3 +13,14 @@ Deno.test( assertEquals(output, "1\n"); }, ); + +Deno.test( + { permissions: { run: true, read: true } }, + async function denoNoColorIsNotAffectedByNonTty() { + const { stdout } = await new Deno.Command(Deno.execPath(), { + args: ["eval", "console.log(Deno.noColor)"], + }).output(); + const output = new TextDecoder().decode(stdout); + assertEquals(output, "false\n"); + }, +); diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 06a7d605d61e26..76a279bbddf378 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -241,7 +241,7 @@ function opMainModule() { const opArgs = memoizeLazy(() => ops.op_bootstrap_args()); const opPid = memoizeLazy(() => ops.op_bootstrap_pid()); const opPpid = memoizeLazy(() => ops.op_ppid()); -setNoColorFn(() => ops.op_bootstrap_no_color()); +setNoColorFn(() => ops.op_bootstrap_no_color() || !ops.op_bootstrap_is_tty()); function formatException(error) { if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, error)) { @@ -530,7 +530,7 @@ function bootstrapMainRuntime(runtimeOptions) { ObjectDefineProperties(finalDenoNs, { pid: util.getterOnly(opPid), ppid: util.getterOnly(opPpid), - noColor: util.getterOnly(getNoColor), + noColor: util.getterOnly(() => ops.op_bootstrap_no_color()), args: util.getterOnly(opArgs), mainModule: util.getterOnly(opMainModule), }); @@ -666,7 +666,7 @@ function bootstrapWorkerRuntime( } ObjectDefineProperties(finalDenoNs, { pid: util.getterOnly(opPid), - noColor: util.getterOnly(getNoColor), + noColor: util.getterOnly(() => ops.op_bootstrap_no_color()), args: util.getterOnly(opArgs), }); // Setup `Deno` global - we're actually overriding already diff --git a/runtime/ops/bootstrap.rs b/runtime/ops/bootstrap.rs index 1b86a7509a369a..72e31a1d616189 100644 --- a/runtime/ops/bootstrap.rs +++ b/runtime/ops/bootstrap.rs @@ -15,6 +15,7 @@ deno_core::extension!( op_bootstrap_language, op_bootstrap_log_level, op_bootstrap_no_color, + op_bootstrap_is_tty, ], ); @@ -57,5 +58,11 @@ pub fn op_bootstrap_log_level(state: &mut OpState) -> i32 { #[op2(fast)] pub fn op_bootstrap_no_color(state: &mut OpState) -> bool { let options = state.borrow::(); - options.no_color || !options.is_tty + options.no_color +} + +#[op2(fast)] +pub fn op_bootstrap_is_tty(state: &mut OpState) -> bool { + let options = state.borrow::(); + options.is_tty }