Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo doesn't respect --color or CARGO_TERM_COLOR for help text and errors from clap #9012

Open
mchernyavsky opened this issue Dec 23, 2020 · 8 comments
Labels
A-console-output Area: Terminal output, colors, progress bar, etc. C-bug Category: bug S-needs-team-input Status: Needs input from team on whether/how to proceed.

Comments

@mchernyavsky
Copy link
Contributor

Problem

^title

I expect that with --color=always option (or CARGO_TERM_COLOR=always environment variable) the output will contain expected ANSI escape codes.

Steps

  1. Run cargo build --color=always --unknown &> out.txt
  2. Theout.txt will contain the following output (without ANSI escape codes):
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

USAGE:
    cargo build --color <WHEN>

For more information try --help

Notes

Output of cargo version:

cargo 1.50.0-nightly (a3c2627fb 2020-12-14)
@mchernyavsky mchernyavsky added the C-bug Category: bug label Dec 23, 2020
@ehuss ehuss added the A-console-output Area: Terminal output, colors, progress bar, etc. label Jan 7, 2021
@epage
Copy link
Contributor

epage commented Apr 20, 2022

The main problem here is the circular nature of this.

  • To read --color=always, we need to parse the CLI but the error is coming from our CLI parser
  • To read CARGO_TERM_COLOR, we need the Config configured which requires the CLI to be parsed.

We can't workaround it for --color=always but we can for CARGO_TERM_COLOR by directly looking up the value and passing that to clap. We'd probably want to extract the value parser out of set_color_choice into a std::str::FromStr impl on ColorChoice.

This would make it so fn cli() would have something like

    let color_choice = std::env::var_os("CARGO_TERM_COLOR");
    let color_choice = color_choice.and_then(|c| c.parse::<ColorChoice>().ok());
    let color_choice = match color_choice {
        Some(ColorChoice::CargoAuto) | None => clap::ColorChoice::Auto,
        Some(ColorChoice::Always) => clap::ColorChoice::Always,
        Some(ColorChoice::Never) => clap::ColorChoice::Never,
    };

This intentionally ignores the errors since it isn't worth reporting at this stage.

The main question is if this is worth it or not.

@weihanglo weihanglo added the S-needs-team-input Status: Needs input from team on whether/how to proceed. label Jun 2, 2023
@weihanglo weihanglo changed the title Cargo doesn't take into account --color=always if an unknown option is encountered Cargo doesn't respect --color or CARGO_TERM_COLOR for help text and errors from clap Dec 6, 2023
epage added a commit to epage/cargo that referenced this issue Feb 20, 2024
epage added a commit to epage/cargo that referenced this issue Feb 20, 2024
epage added a commit to epage/cargo that referenced this issue Feb 20, 2024
epage added a commit to epage/cargo that referenced this issue Feb 20, 2024
epage added a commit to epage/cargo that referenced this issue Feb 20, 2024
epage added a commit to epage/cargo that referenced this issue Feb 21, 2024
epage added a commit to epage/cargo that referenced this issue Feb 21, 2024
@epage
Copy link
Contributor

epage commented Feb 21, 2024

#13463 resolves the config side of this but leaves --color alone.

To handle --color, we'd need to be able to parse the command line to get those which requires knowing what level of styling to use. We could say "parse the command line, take the result, and strip the message based on our own decision". However, --help and errors short-circuits CLI parsing. We'd have to parse, see if an error gets back, and then re-parse using the "ignore errors" setting which is a very brittle feature in clap.

I'd propose that we reject --color and close this as "resolved" with the merge of #13463

bors added a commit that referenced this issue Feb 21, 2024
fix(cli): Control clap colors through config

### What does this PR try to resolve?

Part of #9012

### How should we test and review this PR?

To accomplish this, I pivoted in how we handle `-C`.  In #11029, I made the config lazily loaded.  Instead, we are now reloading the config for `-C` like we do for "cargo script" and `cargo install`.  If there is any regression, it will be felt by those commands as well and we can fix all together.  As this is unstable, if there is a regression, the impact is less.  This allowed us access to the full config for getting the color setting, rather than taking more limited approaches like checking only `CARGO_TERM_CONFIG`.

### Additional information
@weihanglo
Copy link
Member

I'd propose that we reject --color and close this as "resolved" with the merge of #13463

That sounds fair. Should we document this unfortunate truth somewhere?

@bors bors closed this as completed in 6747425 Feb 21, 2024
@epage epage reopened this Feb 21, 2024
@epage
Copy link
Contributor

epage commented Feb 21, 2024

I untagged it; unsure why github closed it.

@epage
Copy link
Contributor

epage commented Feb 21, 2024

Should we document this unfortunate truth somewhere?

I'm for that but unsure where it would work that it both helps the user and doesn't overwhelm them with details when they don't care (CLI parsing is a bit niche).

@weihanglo
Copy link
Member

I untagged it; unsure why github closed it.

6747425

The close keyword also works in commit messages 😈.

bors added a commit that referenced this issue Feb 22, 2024
fix(cli): Respect CARGO_TERM_COLOR in '--list' and '-Zhelp'

### What does this PR try to resolve?

Similar to #9012, we aren't respecting `CARGO_TERM_COLOR` for `-Zhelp` and other places.  This corrects that.

### How should we test and review this PR?

#9012 was about initialization order to get the value.  Here, the problem is that we don't update `Shell` with `CARGO_TERM_COLOR`.

In doing this, I was concerned about keeping track of where it is safe to call `config_configure` without running it twice.  To this end, I refactored `main` to make it clear that each call to `config_configure` is in a mutually exclusive path that exists immediately.

### Additional information

Found this with the test for `-Zhelp` in #13461.
bors added a commit that referenced this issue Feb 23, 2024
test(cli): Verify terminal styling

### What does this PR try to resolve?

This uses a new feature from snapbox that let's us render terminal styling in SVG files.  This let's us see / visualize ANSI escape codes, including in github's UI (will render images, including side-by-side images for diffs).

### How should we test and review this PR?

Potential concerns
- assert-rs/snapbox#257: If the snapshot file doesn't exist, you need to `SNAPSHOTS=overwrite` twice
- #9012: because we narrow the enabling of colors to cargo via `CARGO_TERM_COLOR`, clap doesn't know about it and those are rendered without color
- File size: `du -ch tests` reported 13MB before and after
- Loss of information: this doesn't yet include links but we already weren't snapshotting these
- Unconditionally turning on styling and snapshotting for it for all "ui" tests
- Too many SVGs in one PR can negatively affect github's UI

### Additional information
stupendoussuperpowers pushed a commit to stupendoussuperpowers/cargo that referenced this issue Feb 28, 2024
@jtmoon79
Copy link

jtmoon79 commented Jun 16, 2024

FWIW cargo bench still prints with color with despite CARGO_TERM_COLOR=never and --color=never.

Steps to reproduce

For some project with benches

$ CARGO_TERM_COLOR=never cargo bench --benches --quiet --color=never

I used my project.
Using cargo 1.79.0 on Ubuntu 22, target x86_64-unknown-linux-gnu.

Screenshot 2024-06-15 165405

@weihanglo
Copy link
Member

@jtmoon79 thanks for the report. That is a different issue than this. Test harness (by default it's libtest from rustc) usually provide their own way to customize console output. Those arguments could be passed after double dashes --, e.g. cargo test -- --nocapture. Cargo makes no assumption to test/benchmark harness so it doesn't control the color of the output. See #1983 for the relevant issue.

charmitro pushed a commit to charmitro/cargo that referenced this issue Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-console-output Area: Terminal output, colors, progress bar, etc. C-bug Category: bug S-needs-team-input Status: Needs input from team on whether/how to proceed.
Projects
None yet
Development

No branches or pull requests

5 participants