Skip to content

Diagnostic infra doesn't respect CLICOLOR_FORCE=1 #148864

@fmease

Description

@fmease

We've recently migrated to anstream/anstyle in #147207.

When lowering rustc's ColorConfig to anstream's/colorchoice's ColorChoice, we utilize a method called to_color_choice which interestingly isn't a straightforward 1:1 mapping as you might've thought:

pub fn to_color_choice(self) -> ColorChoice {
match self {
ColorConfig::Always => {
if io::stderr().is_terminal() {
ColorChoice::Always
} else {
ColorChoice::AlwaysAnsi
}
}
ColorConfig::Never => ColorChoice::Never,
ColorConfig::Auto if io::stderr().is_terminal() => ColorChoice::Auto,
ColorConfig::Auto => ColorChoice::Never,
}
}

Most importantly, instead of mapping Auto to Auto, it maps Auto to Never if STDERR isn't a terminal. I'm not sure why this choice was made. Regardless, it leads to us not fully utilizing anstream's "color capability detection".

For instance, it means that CLICOLOR_FORCE=1 (cc https://bixense.com/clicolors/) doesn't work: CLICOLOR_FORCE=1 rustc erroneous.rs 2>&1 >/dev/null | cat1 or CLICOLOR_FORCE=1 rustc erroneous.rs 2>/dev/null doesn't force color as cat / the file is a "non-terminal" and thus to_color_choice maps it to Never instead of asking anstyle-query.

Note that most if not all other detection mechanisms of anstyle-query do work for rustc/rustdoc. E.g., NO_COLOR=1 suppresses colors, TERM=dumb suppresses colors, in CI= TERM=dumb CI trumps TERM, in CLICOLOR= TERM=dumb CLICOLOR trumps TERM and so on.

Footnotes

  1. This bash (or zsh with option multios disabled) snippet redirects the stderr to a "non-terminal" (cat).

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.D-diagnostic-infraDiagnostics: Issues that affect all diagnostics, or relate to the diagnostic machinery itself.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions