Skip to content

Commit 2ccb1eb

Browse files
branchseerclaude
andcommitted
fix: prevent FORCE_COLOR auto-add when stdout is not a terminal
The auto-add FORCE_COLOR logic was causing issues on Windows CI where stdout might be detected as supporting colors even when it's not a TTY. This caused oxlint to output ANSI color codes in its output. Changes: - Add IsTerminal check before auto-adding FORCE_COLOR - Also check FORCE_COLOR=0 in reporter color detection Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 631f2e0 commit 2ccb1eb

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

crates/vite_task/src/session/reporter.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,29 @@ use super::{
1515
event::{CacheStatus, ExecutionEvent, ExecutionEventKind, ExecutionId, ExecutionItemDisplay},
1616
};
1717

18-
/// Wrap of `OwoColorize` that ignores style if `NO_COLOR` is set.
18+
/// Wrap of `OwoColorize` that ignores style if colors are disabled.
19+
///
20+
/// Colors are disabled if:
21+
/// - `NO_COLOR` environment variable is set and non-empty
22+
/// - `FORCE_COLOR` environment variable is set to "0"
1923
trait ColorizeExt {
2024
fn style(&self, style: Style) -> Styled<&Self>;
2125
}
2226

2327
impl<T: owo_colors::OwoColorize> ColorizeExt for T {
2428
fn style(&self, style: Style) -> Styled<&Self> {
25-
static NO_COLOR: LazyLock<bool> =
26-
LazyLock::new(|| std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()));
27-
owo_colors::OwoColorize::style(self, if *NO_COLOR { Style::new() } else { style })
29+
static DISABLE_COLOR: LazyLock<bool> = LazyLock::new(|| {
30+
// NO_COLOR takes precedence per https://no-color.org/
31+
if std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()) {
32+
return true;
33+
}
34+
// FORCE_COLOR=0 explicitly disables colors
35+
if std::env::var_os("FORCE_COLOR").is_some_and(|v| v == "0") {
36+
return true;
37+
}
38+
false
39+
});
40+
owo_colors::OwoColorize::style(self, if *DISABLE_COLOR { Style::new() } else { style })
2841
}
2942
}
3043

crates/vite_task_plan/src/envs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ impl EnvFingerprints {
6565
// Automatically add FORCE_COLOR environment variable if not already set
6666
// This enables color output in subprocesses when color is supported
6767
// TODO: will remove this temporarily until we have a better solution
68+
//
69+
// Only add FORCE_COLOR if:
70+
// 1. FORCE_COLOR is not already set (including FORCE_COLOR=0)
71+
// 2. NO_COLOR is not set
72+
// 3. stdout is a terminal (not just supports colors)
73+
// 4. Terminal supports colors
6874
if !all_envs.contains_key(OsStr::new("FORCE_COLOR"))
6975
&& !all_envs.contains_key(OsStr::new("NO_COLOR"))
76+
&& std::io::IsTerminal::is_terminal(&std::io::stdout())
7077
&& let Some(support) = on(Stream::Stdout)
7178
{
7279
let force_color_value = if support.has_16m {

0 commit comments

Comments
 (0)