Skip to content

cmdio: strip styles from LogString output on a color-less stderr - #7738

Draft
pietern wants to merge 2 commits into
pietern/charm-v2/02a-rendererfrom
pietern/charm-v2/02b-logstring-strip
Draft

pietern wants to merge 2 commits into
pietern/charm-v2/02a-rendererfrom
pietern/charm-v2/02b-logstring-strip

Conversation

@pietern

@pietern pietern commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Changes

cmdio.LogString writes through the stderr renderer's Writer, which strips escape sequences when stderr does not support color. The stderr writer is built once per cmdIO (stderrWriter), so concurrent first calls agree on it. Changelog fragment: .nextchanges/cli/stderr-strip-color.md.

What is stripped: messages written with LogString (warnings, hints and similar), when stderr is a file or pipe and stdout is a terminal. Callers style those messages with cmdio.Yellow and similar helpers, which check stdout's color support, so the styles used to reach a redirected stderr.

What is not stripped: a stderr that is a terminal keeps its color, including with CI or CLICOLOR=0 set. cmdio checks only NO_COLOR and TERM=dumb for color, and stderr's renderer keeps the profile termenv reports for a terminal.

Why

Redirected stderr should not carry escape codes meant for the terminal. The strip is in its own PR so that the output change is reviewable on its own; it changes two pin rows.

Tests

  • go build ./..., golangci-lint (0 issues), gofmt clean.
  • TERM=xterm-256color go test -race -count=3 ./libs/cmdio/... passes, including TestLogStringConcurrent, TestLogStringStripsStylesOnColorlessStderr.
  • Golden changes (nine files):
    • TestStderrPin_LogStringStyles/stdout-tty-stderr-redirected: the helper row loses its SGR (helper: \x1b[31mred\x1b[0m becomes helper: red).
    • TestColorPin_Swatch, the eight color environments (ci, clicolor-0, colorterm-yes, term-screen, term-unset, term-vt100, term-xterm-256color, tmux-truecolor): the helpers row loses its SGR. The pin's stderr is a pipe, so the strip applies. no-color and term-dumb were already plain.
  • PTY check, selftest tui colors (default stream, which goes through LogString), stdout a terminal, stderr redirected to a file: before this PR the file had SGR, after it had none, with the default environment, CI=1 and CLICOLOR=0. With stderr on the terminal, both builds keep the SGR under all three.

Stack: based on pietern/charm-v2/02a-renderer.

Manual checks for real terminals:

  • databricks selftest tui colors 2>/tmp/err.txt; cat -v /tmp/err.txt: no escape codes in the file.
  • databricks selftest tui colors: colors on the terminal, unchanged.
  • CI=1 databricks selftest tui colors: stderr on the terminal keeps its colors; the same with 2>/tmp/err.txt strips.

This pull request and its description were written by Isaac.

@eng-dev-ecosystem-bot

eng-dev-ecosystem-bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Integration test report

Commit: d83f279

Run: 44744303829

Env 💚​RECOVERED ✅​pass 🙈​skip Time
💚​ aws linux-2core-8gb 1 272 19 6:51
💚​ aws-windows-latest-4core-16gb 1 274 17 4:08
💚​ azure linux-2core-8gb 1 271 19 5:03
💚​ azure-windows-latest-4core-16gb 1 273 17 4:20
💚​ gcp linux-2core-8gb 1 272 19 5:00
💚​ gcp-windows-latest-4core-16gb 1 274 17 4:46
Test Name aws linux-2core-8gb aws-windows-latest-4core-16gb azure linux-2core-8gb azure-windows-latest-4core-16gb gcp linux-2core-8gb gcp-windows-latest-4core-16gb
💚​ TestAccept 💚​R 💚​R 💚​R 💚​R 💚​R 💚​R
Top 5 slowest tests (at least 2 minutes):
duration env testname
4:44 gcp-windows-latest-4core-16gb TestAccept
4:18 azure-windows-latest-4core-16gb TestAccept
4:07 aws-windows-latest-4core-16gb TestAccept
2:04 gcp linux-2core-8gb TestAccept
2:01 azure linux-2core-8gb TestAccept

@pietern
pietern added this pull request to stack #7754 November 18, 2026 08:35
@pietern

pietern commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

/review

Comment thread libs/cmdio/io.go
// use. Whether err supports color depends only on this cmdIO's capabilities,
// so the result does not change; the color depth is read from ctx's
// environment on that first call. (sync.OnceValue can't take ctx.)
func (c *cmdIO) stderrWriter(ctx context.Context) io.Writer {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

RenderDiagnostics in render.go:475 writes to c.err directly, and its template color functions (renderFuncMap → colorEnabled → SupportsStdoutColor) decide color the same way cmdio.Yellow does — based on stdout. So databricks bundle validate 2>/tmp/diag.log with stdout on a terminal still leaks escapes into the file. Should it also write through stderrWriter, or is that planned for a later PR in the stack?

@pietern
pietern force-pushed the pietern/charm-v2/02b-logstring-strip branch from cef5a26 to 4c21236 Compare November 18, 2026 13:07
@pietern
pietern force-pushed the pietern/charm-v2/02b-logstring-strip branch from 4c21236 to 3f78fb3 Compare November 18, 2026 15:55
@pietern
pietern force-pushed the pietern/charm-v2/02b-logstring-strip branch from 3f78fb3 to fb9edd2 Compare November 18, 2026 16:07
@pietern

pietern commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

/review

@review-oss-bot review-oss-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Automated review at fb9edd2. Each finding was verified against the code.

Not ready yet: part of the automated review did not complete.

This PR wraps cmdIO.err in a renderer-backed writer so that LogString strips ANSI escape sequences when stderr is redirected (pipe or file) while stdout is a terminal. The implementation is small and well-guarded: a mutex-protected lazy init in stderrWriter, a clear comment explaining why the strip is needed, and three new tests covering stripping, concurrency (with -race), and golden-file updates for nine pin environments. No issues surfaced during verification.

pietern and others added 2 commits November 20, 2026 11:57
LogString writes text that callers styled for stdout (cmdio.Yellow and
friends check stdout's color support). With stdout a terminal and stderr
redirected, those escapes used to reach the file. LogString now writes
through the stderr renderer's Writer, which strips escapes when stderr
does not support color. The writer is built once per cmdIO.

Stderr that is a terminal is unchanged, including under CI or
CLICOLOR=0: cmdio only checks NO_COLOR and TERM=dumb for color, and the
renderer keeps the profile termenv reports for a terminal.

Golden changes (both pins run stderr as a pipe, so the strip applies):
- TestStderrPin_LogStringStyles/stdout-tty-stderr-redirected: the helper
  row loses its SGR, "helper: red".
- TestColorPin_Swatch, the eight color environments (ci, clicolor-0,
  colorterm-yes, term-screen, term-unset, term-vt100,
  term-xterm-256color, tmux-truecolor): the helpers row loses its SGR.
  no-color and term-dumb were already plain.

Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
@pietern
pietern force-pushed the pietern/charm-v2/02b-logstring-strip branch from fb9edd2 to d83f279 Compare November 20, 2026 13:23

This branch has not been deployed

No deployments
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.

2 participants