Skip to content

fix(cli): handle terminals with limited color support#4458

Merged
sasa-tomic merged 2 commits intomasterfrom
fix/terminal-color-panic
Jan 14, 2026
Merged

fix(cli): handle terminals with limited color support#4458
sasa-tomic merged 2 commits intomasterfrom
fix/terminal-color-panic

Conversation

@sasa-tomic
Copy link
Contributor

@sasa-tomic sasa-tomic commented Jan 12, 2026

Motivation

  • Solve a long-standing issue we have observed many times in non-interactive environments:
⠁ Reinstalling code for canister 72ch2-fiaaa-aaaar-qbsvq-cai                    
⠁ Reinstalling code for canister 72ch2-fiaaa-aaaar-qbsvq-cai                    

thread 'main' panicked at src/dfx/src/main.rs:94:18:
Failed to set stderr output color.: ColorOutOfRange
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  • Color setting was non-optional. term crate panics in CI environments lacking full terminal capabilities.

Solution

  • Drop term dependency; use std::io::IsTerminal for detection. The term crate's "robustness" caused this bug - it queries terminfo and refuses to set colors if the terminal reports limited capabilities. This is "correct" but brittle - users hit ColorOutOfRange in some environments.
  • Respect NO_COLOR env var: https://no-color.org/
  • Inject ANSI codes directly, removing custom wrapper module. This follows the industry standard: Modern CLI tools (cargo, rustc, git, npm, ripgrep) all use ANSI escape codes directly. They check is_terminal() + NO_COLOR and just emit ANSI codes. This is the accepted pattern.

@sasa-tomic sasa-tomic self-assigned this Jan 12, 2026
@sasa-tomic sasa-tomic marked this pull request as ready for review January 12, 2026 16:10
@sasa-tomic sasa-tomic requested a review from a team as a code owner January 12, 2026 16:10
@tojot-dfinity
Copy link

Thank you!

I remember @viviveevee and someone else commenting on my question in the dfiniy slack, but I no longer have access to check who.

Copy link
Contributor

@viviveevee viviveevee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a changelog entry, otherwise good to merge. Thanks!

### Motivation
- `term` crate panicked on terminals lacking color support.

### Solution
- Swap `term` crate for raw ANSI escape codes.
- Emit color only if TTY and `NO_COLOR` is unset.
- Delete obsolete `stderr_wrapper` module.
@sasa-tomic sasa-tomic force-pushed the fix/terminal-color-panic branch from 6ebc298 to 7ad656d Compare January 14, 2026 12:12
@sasa-tomic sasa-tomic enabled auto-merge (squash) January 14, 2026 12:12
@sasa-tomic sasa-tomic merged commit 9ffbb0f into master Jan 14, 2026
133 checks passed
@sasa-tomic sasa-tomic deleted the fix/terminal-color-panic branch January 14, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants