Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit fd60f83

Browse files
author
Ulysses Souza
committed
Create a global variable to hold output file
With a function scoped `os.File`, next time the GC passes the instance is collected, calling the finalizer and triggering the invalidation of the FD, that cannot be used anymore. Signed-off-by: Ulysses Souza <[email protected]>
1 parent e3a9aed commit fd60f83

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

internal/commands/build/build.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/docker/cli/cli"
2424
"github.com/docker/cli/cli/command"
2525
compose "github.com/docker/cli/cli/compose/types"
26+
"github.com/docker/cli/cli/streams"
2627
"github.com/docker/cnab-to-oci/remotes"
2728
"github.com/docker/distribution/reference"
2829
"github.com/moby/buildkit/client"
@@ -49,6 +50,8 @@ type buildOptions struct {
4950
const buildExample = `- $ docker app build .
5051
- $ docker app build --file myapp.dockerapp --tag myrepo/myapp:1.0.0 .`
5152

53+
var outputFile *os.File
54+
5255
func Cmd(dockerCli command.Cli) *cobra.Command {
5356
var opts buildOptions
5457
cmd := &cobra.Command{
@@ -136,6 +139,22 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) error
136139
return err
137140
}
138141

142+
func getOutputFile(realOut *streams.Out, quiet bool) (*os.File, error) {
143+
if outputFile != nil {
144+
return outputFile, nil
145+
}
146+
if quiet {
147+
var err error
148+
outputFile, err = os.Create(os.DevNull)
149+
if err != nil {
150+
return nil, err
151+
}
152+
return outputFile, nil
153+
}
154+
outputFile = os.NewFile(realOut.FD(), os.Stdout.Name())
155+
return outputFile, nil
156+
}
157+
139158
func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions, dockerCli command.Cli) (*bundle.Bundle, error) {
140159
buildopts, pulledServices, err := parseCompose(app, contextPath, opt)
141160
if err != nil {
@@ -160,13 +179,9 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
160179
},
161180
}
162181

163-
var out *os.File
164-
if opt.quiet {
165-
if out, err = os.Create(os.DevNull); err != nil {
166-
return nil, err
167-
}
168-
} else {
169-
out = os.NewFile(dockerCli.Out().FD(), "/dev/stdout")
182+
out, err := getOutputFile(dockerCli.Out(), opt.quiet)
183+
if err != nil {
184+
return nil, err
170185
}
171186

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

0 commit comments

Comments
 (0)