Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/stderr-strip-color.md
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"renderer color: false\nhelper: \x1b[31mred\x1b[0m\nrenderer: bold\n"
"renderer color: false\nhelper: red\nrenderer: bold\n"
18 changes: 18 additions & 0 deletions libs/cmdio/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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?

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 {
Expand Down
6 changes: 4 additions & 2 deletions libs/cmdio/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
49 changes: 49 additions & 0 deletions libs/cmdio/log_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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))
Expand Down
Loading