Repository navigation
Conversation
✅ Integration test reportCommit: d83f279
Top 5 slowest tests (at least 2 minutes):
|
|
/review |
| // 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 { |
There was a problem hiding this comment.
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?
cef5a26 to
4c21236
Compare
4c21236 to
3f78fb3
Compare
3f78fb3 to
fb9edd2
Compare
|
/review |
There was a problem hiding this comment.
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.
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>
fb9edd2 to
d83f279
Compare
Changes
cmdio.LogStringwrites through the stderr renderer'sWriter, 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 withcmdio.Yellowand 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
CIorCLICOLOR=0set. cmdio checks onlyNO_COLORandTERM=dumbfor 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, includingTestLogStringConcurrent,TestLogStringStripsStylesOnColorlessStderr.TestStderrPin_LogStringStyles/stdout-tty-stderr-redirected: the helper row loses its SGR (helper: \x1b[31mred\x1b[0mbecomeshelper: 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-colorandterm-dumbwere already plain.selftest tui colors(default stream, which goes throughLogString), stdout a terminal, stderr redirected to a file: before this PR the file had SGR, after it had none, with the default environment,CI=1andCLICOLOR=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 with2>/tmp/err.txtstrips.This pull request and its description were written by Isaac.