Skip to content
Open
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
19 changes: 16 additions & 3 deletions pkg/compose/build_bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
if out == nil {
out = s.stdout()
}
display, err := progressui.NewDisplay(makeConsole(out), displayMode)
display, err := newBakeDisplay(out, displayMode)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -509,14 +509,27 @@ func (s *composeService) getBuildxPlugin() (*manager.Plugin, error) {
// matters, and falls back to plain elsewhere.
func makeConsole(out io.Writer) io.Writer {
if s, ok := out.(*streams.Out); ok {
if f, ok := s.File(); ok {
if f, ok := s.File(); ok && s.IsTerminal() {
return f
}
return &_console{s}
if s.IsTerminal() {
return &_console{s}
}
return s
}
return out
}

// newBakeDisplay falls back to plain when TTY mode is requested but out is not a console
// (e.g. `docker compose build >/dev/null`, #14182).
func newBakeDisplay(out io.Writer, mode progressui.DisplayMode) (progressui.Display, error) {
d, err := progressui.NewDisplay(makeConsole(out), mode)
if err != nil && mode != progressui.PlainMode && mode != progressui.QuietMode {
d, err = progressui.NewDisplay(out, progressui.PlainMode)
}
return d, err
}

var _ console.File = &_console{}

type _console struct {
Expand Down
46 changes: 40 additions & 6 deletions pkg/compose/build_bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/streams"
"github.com/moby/buildkit/util/progress/progressui"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -105,19 +106,52 @@ func TestToBakeAttest(t *testing.T) {
// os.Stdin/Stdout/Stderr values, so a wrapper would disable the TTY progress
// rendering entirely (#14086).
func TestMakeConsole(t *testing.T) {
t.Run("stream wrapping a real file yields the file itself", func(t *testing.T) {
out := makeConsole(streams.NewOut(os.Stdout))
assert.Equal(t, out, os.Stdout)
t.Run("terminal stdout yields the real file", func(t *testing.T) {
s := streams.NewOut(os.Stdout)
out := makeConsole(s)
if s.IsTerminal() {
assert.Equal(t, out, os.Stdout)
return
}
assert.Check(t, out != os.Stdout, "non-console stdout must not be passed to ConsoleFromFile")
})

t.Run("redirected file is not treated as a console", func(t *testing.T) {
r, w, err := os.Pipe()
assert.NilError(t, err)
t.Cleanup(func() {
_ = r.Close()
_ = w.Close()
})
s := streams.NewOut(w)
out := makeConsole(s)
assert.Check(t, out != w, "pipe fd must not be handed to ConsoleFromFile")
})

t.Run("file-less stream keeps the console.File wrapper", func(t *testing.T) {
out := makeConsole(streams.NewOut(&bytes.Buffer{}))
_, ok := out.(*_console)
assert.Check(t, ok, "expected a *_console, got %T", out)
s := streams.NewOut(&bytes.Buffer{})
out := makeConsole(s)
if s.IsTerminal() {
_, ok := out.(*_console)
assert.Check(t, ok, "expected a *_console, got %T", out)
return
}
assert.Equal(t, out, io.Writer(s))
})

t.Run("plain writer is left untouched", func(t *testing.T) {
buf := &bytes.Buffer{}
assert.Equal(t, makeConsole(buf), io.Writer(buf))
})
}

func TestNewBakeDisplayFallsBackWhenNotConsole(t *testing.T) {
r, w, err := os.Pipe()
assert.NilError(t, err)
t.Cleanup(func() {
_ = r.Close()
_ = w.Close()
})
_, err = newBakeDisplay(streams.NewOut(w), progressui.TtyMode)
assert.NilError(t, err)
}