Skip to content

Commit

Permalink
build: fix build details link in experimental mode
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Oct 8, 2024
1 parent d353f5f commit a51e219
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
11 changes: 10 additions & 1 deletion cmd/buildx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/docker/buildx/commands"
controllererrors "github.com/docker/buildx/controller/errdefs"
"github.com/docker/buildx/util/desktop"
"github.com/docker/buildx/version"
"github.com/docker/cli/cli"
Expand All @@ -16,6 +17,7 @@ import (
cliflags "github.com/docker/cli/cli/flags"
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/util/stack"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"

//nolint:staticcheck // vendored dependencies may still use this
Expand Down Expand Up @@ -106,8 +108,15 @@ func main() {
} else {
fmt.Fprintf(cmd.Err(), "ERROR: %v\n", err)
}
if ebr, ok := err.(*desktop.ErrorWithBuildRef); ok {

var ebr *desktop.ErrorWithBuildRef
if errors.As(err, &ebr) {
ebr.Print(cmd.Err())
} else {
var be *controllererrors.BuildError
if errors.As(err, &be) {
be.PrintBuildDetails(cmd.Err())
}
}

os.Exit(1)
Expand Down
18 changes: 16 additions & 2 deletions controller/errdefs/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package errdefs

import (
"io"

"github.com/containerd/typeurl/v2"
"github.com/docker/buildx/util/desktop"
"github.com/moby/buildkit/util/grpcerrors"
)

Expand All @@ -22,11 +25,22 @@ func (e *BuildError) ToProto() grpcerrors.TypedErrorProto {
return e.Build
}

func WrapBuild(err error, ref string) error {
func (e *BuildError) PrintBuildDetails(w io.Writer) error {
if e.BuildRef == "" {
return nil
}
ebr := &desktop.ErrorWithBuildRef{
Ref: e.BuildRef,
Err: e.error,
}
return ebr.Print(w)
}

func WrapBuild(err error, ref string, buildRef string) error {
if err == nil {
return nil
}
return &BuildError{Build: &Build{Ref: ref}, error: err}
return &BuildError{Build: &Build{Ref: ref, BuildRef: buildRef}, error: err}
}

func (b *Build) WrapError(err error) error {
Expand Down
23 changes: 16 additions & 7 deletions controller/errdefs/errdefs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions controller/errdefs/errdefs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ option go_package = "github.com/docker/buildx/controller/errdefs";

message Build {
string Ref = 1;
string BuildRef = 2;
}
8 changes: 7 additions & 1 deletion controller/local/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
controllererrors "github.com/docker/buildx/controller/errdefs"
controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/controller/processes"
"github.com/docker/buildx/util/desktop"
"github.com/docker/buildx/util/ioset"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -56,7 +57,12 @@ func (b *localController) Build(ctx context.Context, options *controllerapi.Buil
buildOptions: options,
}
if buildErr != nil {
buildErr = controllererrors.WrapBuild(buildErr, b.ref)
var buildRef string
var ebr *desktop.ErrorWithBuildRef
if errors.As(buildErr, &ebr) {
buildRef = ebr.Ref
}
buildErr = controllererrors.WrapBuild(buildErr, b.ref, buildRef)
}
}
if buildErr != nil {
Expand Down
8 changes: 7 additions & 1 deletion controller/remote/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
controllererrors "github.com/docker/buildx/controller/errdefs"
"github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/controller/processes"
"github.com/docker/buildx/util/desktop"
"github.com/docker/buildx/util/ioset"
"github.com/docker/buildx/util/progress"
"github.com/docker/buildx/version"
Expand Down Expand Up @@ -210,7 +211,12 @@ func (m *Server) Build(ctx context.Context, req *pb.BuildRequest) (*pb.BuildResp
s.buildOptions = req.Options
m.session[ref] = s
if buildErr != nil {
buildErr = controllererrors.WrapBuild(buildErr, ref)
var buildRef string
var ebr *desktop.ErrorWithBuildRef
if errors.As(buildErr, &ebr) {
buildRef = ebr.Ref
}
buildErr = controllererrors.WrapBuild(buildErr, ref, buildRef)
}
}
} else {
Expand Down
5 changes: 0 additions & 5 deletions tests/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,6 @@ RUN echo foo > /bar`)
require.NoError(t, err, string(out))
require.True(t, buildDetailsPattern.MatchString(string(out)), fmt.Sprintf("expected build details link in output, got %q", out))

if isExperimental() {
// FIXME: https://github.com/docker/buildx/issues/2382
t.Skip("build details link not displayed in experimental mode when build fails: https://github.com/docker/buildx/issues/2382")
}

// build erroneous dockerfile
dockerfile = []byte(`FROM busybox:latest
RUN exit 1`)
Expand Down
1 change: 0 additions & 1 deletion util/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func hyperlink(url string) string {
type ErrorWithBuildRef struct {
Ref string
Err error
Msg string
}

func (e *ErrorWithBuildRef) Error() string {
Expand Down

0 comments on commit a51e219

Please sign in to comment.