Skip to content

Commit

Permalink
fix(runtime): fix Deno.noColor when stdout is not tty (denoland#21208)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Nov 15, 2023
1 parent 4913274 commit c67de43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions cli/tests/unit/tty_color_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
);
6 changes: 3 additions & 3 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
});
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion runtime/ops/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deno_core::extension!(
op_bootstrap_language,
op_bootstrap_log_level,
op_bootstrap_no_color,
op_bootstrap_is_tty,
],
);

Expand Down Expand Up @@ -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::<BootstrapOptions>();
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::<BootstrapOptions>();
options.is_tty
}

0 comments on commit c67de43

Please sign in to comment.