Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.
Merged
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
35 changes: 28 additions & 7 deletions internal/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
compose "github.com/docker/cli/cli/compose/types"
"github.com/docker/cli/cli/streams"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -75,6 +76,30 @@ func Cmd(dockerCli command.Cli) *cobra.Command {
return cmd
}

// FIXME: DO NOT SET THIS VARIABLE DIRECTLY! Use `getOutputFile`
// This global var prevents the file to be garbage collected and by that invalidated
// A an alternative fix for this would be writing the output to a bytes buffer and flushing to stdout.
// The impossibility here is that os.File is not an interface that a buffer can implement.
// Maybe `progress.NewPrinter` should implement an "os.File-like" interface just for its needs.
// See https://github.com/golang/go/issues/14106
var _outputFile *os.File

func getOutputFile(realOut *streams.Out, quiet bool) (*os.File, error) {
if _outputFile != nil {
return _outputFile, nil
}
if quiet {
var err error
_outputFile, err = os.Create(os.DevNull)
if err != nil {
return nil, err
}
return _outputFile, nil
}
_outputFile = os.NewFile(realOut.FD(), os.Stdout.Name())
return _outputFile, nil
}

func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) error {
err := checkMinimalEngineVersion(dockerCli)
if err != nil {
Expand Down Expand Up @@ -160,13 +185,9 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
},
}

var out *os.File
if opt.quiet {
if out, err = os.Create(os.DevNull); err != nil {
return nil, err
}
} else {
out = os.NewFile(dockerCli.Out().FD(), "/dev/stdout")
out, err := getOutputFile(dockerCli.Out(), opt.quiet)
if err != nil {
return nil, err
}

pw := progress.NewPrinter(ctx, out, opt.progress)
Expand Down