From 4b2ecd7f7e95324e4e95d3c17441b357e4ec6817 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 17 Nov 2026 20:16:00 +0000 Subject: [PATCH 1/2] cmdio: strip styles from LogString output on a color-less stderr 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 --- .nextchanges/cli/stderr-strip-color.md | 1 + .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-swatch.styled.golden | 2 +- .../01-stderr-bytes.golden | 2 +- libs/cmdio/io.go | 18 +++++++ libs/cmdio/log.go | 6 ++- libs/cmdio/log_test.go | 49 +++++++++++++++++++ 13 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 .nextchanges/cli/stderr-strip-color.md diff --git a/.nextchanges/cli/stderr-strip-color.md b/.nextchanges/cli/stderr-strip-color.md new file mode 100644 index 00000000000..60f578eec11 --- /dev/null +++ b/.nextchanges/cli/stderr-strip-color.md @@ -0,0 +1 @@ +* Messages cmdio writes to stderr (warnings, hints and similar) no longer carry color codes when stderr is redirected to a file or pipe while stdout is a terminal. Stderr that is a terminal keeps its colors, including with `CI` or `CLICOLOR=0` set. diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_ci/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_ci/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_ci/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_ci/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_clicolor-0/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_clicolor-0/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_clicolor-0/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_clicolor-0/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_colorterm-yes/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_colorterm-yes/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_colorterm-yes/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_colorterm-yes/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-screen/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-screen/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-screen/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-screen/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-unset/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-unset/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-unset/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-unset/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-vt100/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-vt100/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-vt100/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-vt100/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-xterm-256color/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-xterm-256color/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-xterm-256color/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_term-xterm-256color/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_tmux-truecolor/01-swatch.styled.golden b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_tmux-truecolor/01-swatch.styled.golden index 9bed9ae1f6f..94b6b2bd0ff 100644 --- a/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_tmux-truecolor/01-swatch.styled.golden +++ b/libs/cmdio/cmdiotest/testdata/TestColorPin_Swatch_tmux-truecolor/01-swatch.styled.golden @@ -1,3 +1,3 @@ renderer color: false -helpers: {fg=1}red{/} {fg=2}green{/} {fg=4}blue{/} {fg=8}hiblack{/} {fg=12}hiblue{/} {bold}bold{/} {faint}faint{/} +helpers: red green blue hiblack hiblue bold faint renderer: bold reverse diff --git a/libs/cmdio/cmdiotest/testdata/TestStderrPin_LogStringStyles_stdout-tty-stderr-redirected/01-stderr-bytes.golden b/libs/cmdio/cmdiotest/testdata/TestStderrPin_LogStringStyles_stdout-tty-stderr-redirected/01-stderr-bytes.golden index b7e667488c9..33f150e17a6 100644 --- a/libs/cmdio/cmdiotest/testdata/TestStderrPin_LogStringStyles_stdout-tty-stderr-redirected/01-stderr-bytes.golden +++ b/libs/cmdio/cmdiotest/testdata/TestStderrPin_LogStringStyles_stdout-tty-stderr-redirected/01-stderr-bytes.golden @@ -1 +1 @@ -"renderer color: false\nhelper: \x1b[31mred\x1b[0m\nrenderer: bold\n" +"renderer color: false\nhelper: red\nrenderer: bold\n" diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 5a0385d664a..da88f2ecf73 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -64,6 +64,24 @@ type cmdIO struct { // user input is processed. Populated only by NewTestIO so pipe-backed // test runs receive a synthetic WindowSizeMsg. teaWindowSize *tea.WindowSizeMsg + + // errWriter wraps err for LogString; built once by stderrWriter. + errWriterMu sync.Mutex + errWriter io.Writer +} + +// stderrWriter returns err wrapped to match its color support, built on first +// 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 { + c.errWriterMu.Lock() + defer c.errWriterMu.Unlock() + if c.errWriter == nil { + r, _ := NewRenderer(ctx, c.err) + c.errWriter = r.Writer(c.err) + } + return c.errWriter } func NewIO(ctx context.Context, outputFormat flags.Output, in io.Reader, out, err io.Writer, headerTemplate, template string) *cmdIO { diff --git a/libs/cmdio/log.go b/libs/cmdio/log.go index 8b61664c945..64cd0868292 100644 --- a/libs/cmdio/log.go +++ b/libs/cmdio/log.go @@ -13,8 +13,10 @@ func Log(ctx context.Context, str fmt.Stringer) { // LogString writes str to the error writer, followed by a newline. func LogString(ctx context.Context, str string) { - c := fromContext(ctx) - _, _ = io.WriteString(c.err, str+"\n") + // Callers style str with cmdio.Yellow and friends, which check stdout's + // color support, but str goes to stderr. With stdout a terminal and stderr + // redirected, the escapes are stripped here. + _, _ = io.WriteString(fromContext(ctx).stderrWriter(ctx), str+"\n") } type quietKey struct{} diff --git a/libs/cmdio/log_test.go b/libs/cmdio/log_test.go index c3873444572..23d12e91a34 100644 --- a/libs/cmdio/log_test.go +++ b/libs/cmdio/log_test.go @@ -1,9 +1,14 @@ package cmdio_test import ( + "bytes" + "io" + "strings" + "sync" "testing" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/stretchr/testify/assert" ) @@ -34,6 +39,50 @@ func TestLogStringNotAffectedByQuiet(t *testing.T) { assert.Contains(t, stderr.String(), "Resources: 1 created") } +// Callers style LogString input for stdout (cmdio.Yellow checks stdout), so a +// stderr without color must still get plain text. +func TestLogStringStripsStylesOnColorlessStderr(t *testing.T) { + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + + cmdio.LogString(ctx, "\x1b[33mhost key changed\x1b[0m: "+cmdio.Hyperlink("https://example.test", "docs")) + + assert.Equal(t, "host key changed: docs\n", stderr.String()) +} + +// lockedBuffer is a bytes.Buffer safe for concurrent writes, standing in for +// a stderr that is. +type lockedBuffer struct { + mu sync.Mutex + b bytes.Buffer +} + +func (l *lockedBuffer) Write(p []byte) (int, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.b.Write(p) +} + +func (l *lockedBuffer) String() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.b.String() +} + +// The stderr writer is built once per cmdIO; concurrent first calls must +// agree on it (run with -race). +func TestLogStringConcurrent(t *testing.T) { + stderr := &lockedBuffer{} + ctx := cmdio.InContext(t.Context(), cmdio.NewIO(t.Context(), flags.OutputText, nil, io.Discard, stderr, "", "")) + + var wg sync.WaitGroup + for range 8 { + wg.Go(func() { cmdio.LogString(ctx, "\x1b[33mline\x1b[0m") }) + } + wg.Wait() + + assert.Equal(t, strings.Repeat("line\n", 8), stderr.String()) +} + func TestIsQuiet(t *testing.T) { ctx := t.Context() assert.False(t, cmdio.IsQuiet(ctx)) From d83f279074e5da9fef9084fa2a58c447c95f2b7a Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 17 Nov 2026 22:29:13 +0000 Subject: [PATCH 2/2] Link changelog fragments to the PR Co-authored-by: Isaac --- .nextchanges/cli/stderr-strip-color.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/stderr-strip-color.md b/.nextchanges/cli/stderr-strip-color.md index 60f578eec11..a54b7655b42 100644 --- a/.nextchanges/cli/stderr-strip-color.md +++ b/.nextchanges/cli/stderr-strip-color.md @@ -1 +1 @@ -* Messages cmdio writes to stderr (warnings, hints and similar) no longer carry color codes when stderr is redirected to a file or pipe while stdout is a terminal. Stderr that is a terminal keeps its colors, including with `CI` or `CLICOLOR=0` set. +* Messages cmdio writes to stderr (warnings, hints and similar) no longer carry color codes when stderr is redirected to a file or pipe while stdout is a terminal. Stderr that is a terminal keeps its colors, including with `CI` or `CLICOLOR=0` set. ([#7738](https://github.com/databricks/cli/pull/7738))