@@ -23,6 +23,7 @@ import (
23
23
"github.com/docker/cli/cli"
24
24
"github.com/docker/cli/cli/command"
25
25
compose "github.com/docker/cli/cli/compose/types"
26
+ "github.com/docker/cli/cli/streams"
26
27
"github.com/docker/cnab-to-oci/remotes"
27
28
"github.com/docker/distribution/reference"
28
29
"github.com/moby/buildkit/client"
@@ -75,6 +76,30 @@ func Cmd(dockerCli command.Cli) *cobra.Command {
75
76
return cmd
76
77
}
77
78
79
+ // FIXME: DO NOT SET THIS VARIABLE DIRECTLY! Use `getOutputFile`
80
+ // This global var prevents the file to be garbage collected and by that invalidated
81
+ // A an alternative fix for this would be writing the output to a bytes buffer and flushing to stdout.
82
+ // The impossibility here is that os.File is not an interface that a buffer can implement.
83
+ // Maybe `progress.NewPrinter` should implement an "os.File-like" interface just for its needs.
84
+ // See https://github.com/golang/go/issues/14106
85
+ var _outputFile * os.File
86
+
87
+ func getOutputFile (realOut * streams.Out , quiet bool ) (* os.File , error ) {
88
+ if _outputFile != nil {
89
+ return _outputFile , nil
90
+ }
91
+ if quiet {
92
+ var err error
93
+ _outputFile , err = os .Create (os .DevNull )
94
+ if err != nil {
95
+ return nil , err
96
+ }
97
+ return _outputFile , nil
98
+ }
99
+ _outputFile = os .NewFile (realOut .FD (), os .Stdout .Name ())
100
+ return _outputFile , nil
101
+ }
102
+
78
103
func runBuild (dockerCli command.Cli , contextPath string , opt buildOptions ) error {
79
104
err := checkMinimalEngineVersion (dockerCli )
80
105
if err != nil {
@@ -160,13 +185,9 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
160
185
},
161
186
}
162
187
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" )
188
+ out , err := getOutputFile (dockerCli .Out (), opt .quiet )
189
+ if err != nil {
190
+ return nil , err
170
191
}
171
192
172
193
pw := progress .NewPrinter (ctx , out , opt .progress )
0 commit comments