diff --git a/.nextchanges/cli/stderr-strip-color.md b/.nextchanges/cli/stderr-strip-color.md new file mode 100644 index 00000000000..a54b7655b42 --- /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. ([#7738](https://github.com/databricks/cli/pull/7738)) 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))