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: 4c21236

Run: 44310894062

Env 💚​RECOVERED ✅​pass 🙈​skip Time
💚​ aws linux-2core-8gb 1 272 19 16:30
💚​ aws-windows-latest-4core-16gb 1 274 17 10:38
💚​ azure linux-2core-8gb 1 271 19 14:37
💚​ azure-windows-latest-4core-16gb 1 273 17 10:24
💚​ gcp linux-2core-8gb 1 272 19 12:53
💚​ gcp-windows-latest-4core-16gb 1 274 17 11: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 33 slowest tests (at least 2 minutes):
duration env testname
4:52 aws linux-2core-8gb TestFilerWorkspaceFilesExtensionsReadDir
4:09 azure linux-2core-8gb TestFilerWorkspaceFilesExtensionsReadDir
3:43 gcp-windows-latest-4core-16gb TestAccept
3:39 azure-windows-latest-4core-16gb TestLock
3:38 aws-windows-latest-4core-16gb TestFilerRecursiveDelete/workspace_files
3:37 azure-windows-latest-4core-16gb TestAccept
3:29 aws-windows-latest-4core-16gb TestAccept
3:22 azure linux-2core-8gb TestFilerWorkspaceFilesExtensionsDelete
3:16 gcp linux-2core-8gb TestExportDir
3:14 gcp-windows-latest-4core-16gb TestImportDirDoesNotOverwrite
3:08 azure linux-2core-8gb TestFilerRecursiveDelete/workspace_files
3:03 azure linux-2core-8gb TestFilerReadWrite/workspace_files
3:03 aws-windows-latest-4core-16gb TestImportDir
3:03 aws-windows-latest-4core-16gb TestFilerReadDir/workspace_files_extensions
2:57 aws-windows-latest-4core-16gb TestSyncIncrementalSyncFileToPythonNotebook
2:56 gcp-windows-latest-4core-16gb TestImportDirWithOverwriteFlag
2:48 gcp linux-2core-8gb TestFilerWorkspaceFilesExtensionsReadDir
2:48 azure linux-2core-8gb TestFilerReadWrite/workspace_files_extensions
2:45 azure-windows-latest-4core-16gb TestFilerWorkspaceFilesExtensionsRead
2:35 azure linux-2core-8gb TestImportDirWithOverwriteFlag
2:34 gcp linux-2core-8gb TestFilerRecursiveDelete/workspace_files
2:28 gcp-windows-latest-4core-16gb TestFilerWorkspaceFilesExtensionsReadDir
2:24 azure-windows-latest-4core-16gb TestFilerWorkspaceFilesExtensionsReadDir
2:23 aws linux-2core-8gb TestFilerWorkspaceFilesExtensionsStat
2:22 aws linux-2core-8gb TestFilerWorkspaceNotebook/sqlNb.sql
2:21 aws-windows-latest-4core-16gb TestFilerReadWrite/workspace_files_extensions
2:18 aws linux-2core-8gb TestExportWithFileFlag
2:17 aws linux-2core-8gb TestFilerRecursiveDelete/workspace_files
2:08 aws-windows-latest-4core-16gb TestFilerWorkspaceFilesExtensionsRead
2:07 aws linux-2core-8gb TestSyncIncrementalSyncFileToPythonNotebook
2:05 gcp-windows-latest-4core-16gb TestFilerWorkspaceFilesExtensionsRead
2:04 aws-windows-latest-4core-16gb TestImportDirWithOverwriteFlag
2:02 azure-windows-latest-4core-16gb TestFilerWorkspaceFilesExtensionsStat

@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 and others added 2 commits November 18, 2026 16:06
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 3f78fb3 to fb9edd2 Compare November 18, 2026 16:07

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