Skip to content

fix: respect TERM=dumb and NO_COLOR for ANSI escape codes#412

Merged
yuxincs merged 3 commits into
uber-go:mainfrom
mvanhorn:fix-ansi-codes-term-dumb
May 15, 2026
Merged

fix: respect TERM=dumb and NO_COLOR for ANSI escape codes#412
yuxincs merged 3 commits into
uber-go:mainfrom
mvanhorn:fix-ansi-codes-term-dumb

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

Summary

Skips ANSI escape codes in PrettyPrintErrorMessage when TERM=dumb or NO_COLOR is set.

Why this matters

PrettyPrintErrorMessage in nilaway.go:61 unconditionally wraps error text with ANSI color codes (\x1b[31m for red, \x1b[95m for magenta, etc.). In terminals that don't support ANSI (like Emacs shell mode, some CI runners, or TERM=dumb), these appear as raw escape sequences in the output (#105).

Changes

  • nilaway.go: Added an early return in PrettyPrintErrorMessage that checks os.Getenv("TERM") == "dumb" and os.Getenv("NO_COLOR") != "" before applying ANSI codes. When either is set, returns plain text with "error: " prefix but no escape sequences. Also follows the NO_COLOR convention.

Testing

go build ./... passes. The change is a 4-line guard at the top of an existing function. The existing -pretty-print=false flag is unaffected - it controls whether PrettyPrintErrorMessage is called at all (nilaway.go:47-48).

Fixes #105

This contribution was developed with AI assistance (Claude Code).

PrettyPrintErrorMessage unconditionally emitted ANSI escape codes,
which broke output in terminals that don't support them (TERM=dumb)
and tools that pipe the output.

Skip ANSI formatting when TERM is set to "dumb" or when NO_COLOR is
set, following the NO_COLOR convention (https://no-color.org/).

Fixes uber-go#105
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 20, 2026

CLA assistant check
All committers have signed the CLA.

@vincent067
Copy link
Copy Markdown

This looks like a good issue for a first-time contributor. I'd be happy to work on respecting TERM=dumb and NO_COLOR environment variables. Could you assign this to me?

@mvanhorn
Copy link
Copy Markdown
Contributor Author

Hey @vincent067 — this is actually the PR that addresses #105 (TERM=dumb / NO_COLOR support). It's been sitting waiting for maintainer review since March. If you're looking for a first-time contribution on nilaway, the issue tracker has a few open ones you could pick up. Review/testing on this PR would also be welcome if you want to dig in here.

@yuxincs
Copy link
Copy Markdown
Contributor

yuxincs commented May 14, 2026

Hey, thanks for the contributions!

Perhaps a better place to do this is at https://github.com/uber-go/nilaway/blob/main/config/config.go#L167 where we calculate the default value (currently it's always true, but we can make that respect the terminal envs)? This way if user wants to override it by giving the -pretty-print flag it would still be effective

mvanhorn and others added 2 commits May 14, 2026 17:06
Move the env check from PrettyPrintErrorMessage into the PrettyPrint flag
default in config/config.go per @yuxincs review. The -pretty-print flag
continues to override the default via the existing override path in run.

Closes uber-go#105
@yuxincs yuxincs enabled auto-merge (squash) May 15, 2026 01:14
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.21%. Comparing base (c2da117) to head (561326c).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
config/config.go 50.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #412      +/-   ##
==========================================
- Coverage   87.25%   87.21%   -0.04%     
==========================================
  Files          72       72              
  Lines        8306     8312       +6     
==========================================
+ Hits         7247     7249       +2     
- Misses        867      869       +2     
- Partials      192      194       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown

Golden Test

Note

✅ NilAway errors reported on standard libraries are identical.

2570 errors on base branch (main, c2da117)
2570 errors on test branch (db2ed0b)

@yuxincs yuxincs merged commit 183b935 into uber-go:main May 15, 2026
9 checks passed
@mvanhorn
Copy link
Copy Markdown
Contributor Author

Thanks @yuxincs, that is a much better placement. Pushed 234b235:

  • Added defaultPrettyPrint() helper in config/config.go that returns false when NO_COLOR is set or TERM=dumb
  • Used it as the default for the -pretty-print flag (around line 165)
  • Reverted the env check from PrettyPrintErrorMessage in nilaway.go

The override path through pass.Analyzer.Flags.Lookup(PrettyPrintFlag) is untouched, so explicit -pretty-print=true still wins. Tests pass locally; lint fetch failed in my sandbox (network), so CI golangci-lint will be the source of truth there.

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.

Output always contains ANSI escape codes regardless of how the TERM variable is set

4 participants