Skip to content

Commit 9a5cbac

Browse files
authored
Merge pull request #3278 from crazy-max/fix-exit-after-defer
cmd: fix possible skipped defers for build and bake
2 parents 2b5a8c6 + a711b8f commit 9a5cbac

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ linters:
4949
- "assignOp"
5050
- "appendAssign"
5151
- "singleCaseSwitch"
52-
- "exitAfterDefer" # FIXME
5352
gosec:
5453
excludes:
5554
- G204

cmd/buildx/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88

99
"github.com/docker/buildx/commands"
10+
"github.com/docker/buildx/util/cobrautil"
1011
"github.com/docker/buildx/util/desktop"
1112
"github.com/docker/buildx/version"
1213
"github.com/docker/cli/cli"
@@ -101,6 +102,13 @@ func main() {
101102
os.Exit(sterr.StatusCode)
102103
}
103104

105+
// Check for ExitCodeError, which is used to exit with a specific code
106+
// without printing an error message.
107+
var exitCodeErr cobrautil.ExitCodeError
108+
if errors.As(err, &exitCodeErr) {
109+
os.Exit(int(exitCodeErr))
110+
}
111+
104112
for _, s := range solvererrdefs.Sources(err) {
105113
s.Print(cmd.Err())
106114
}

commands/bake.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/docker/buildx/builder"
2525
"github.com/docker/buildx/localstate"
2626
"github.com/docker/buildx/util/buildflags"
27+
"github.com/docker/buildx/util/cobrautil"
2728
"github.com/docker/buildx/util/cobrautil/completion"
2829
"github.com/docker/buildx/util/confutil"
2930
"github.com/docker/buildx/util/desktop"
@@ -448,7 +449,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
448449
}
449450

450451
if exitCode != 0 {
451-
os.Exit(exitCode)
452+
return cobrautil.ExitCodeError(exitCode)
452453
}
453454

454455
return nil

commands/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
384384
}
385385
}
386386
if opts.CallFunc != nil {
387-
if exitcode, err := printResult(dockerCli.Out(), opts.CallFunc, resp.ExporterResponse, options.target, inputs); err != nil {
387+
if exitCode, err := printResult(dockerCli.Out(), opts.CallFunc, resp.ExporterResponse, options.target, inputs); err != nil {
388388
return err
389-
} else if exitcode != 0 {
390-
os.Exit(exitcode)
389+
} else if exitCode != 0 {
390+
return cobrautil.ExitCodeError(exitCode)
391391
}
392392
}
393393
if v, ok := resp.ExporterResponse["frontend.result.inlinemessage"]; ok {

util/cobrautil/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cobrautil
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type ExitCodeError int
8+
9+
func (e ExitCodeError) Error() string {
10+
return fmt.Sprintf("exiting with code %d", int(e))
11+
}
12+
13+
func (e ExitCodeError) Unwrap() error {
14+
return nil
15+
}

0 commit comments

Comments
 (0)