From bce65f0edc8c9bc4634365052331d57e33f40f9d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 27 Jan 2022 18:42:05 +0100 Subject: [PATCH] builder: simplify error generation, and rephrase error/warning With this change: echo 'FROM busybox' | DOCKER_BUILDKIT=1 docker build - ERROR: BuildKit is enabled but the buildx component is missing or broken. Install the buildx component to build images with BuildKit: https://docs.docker.com/go/buildx/ echo 'FROM busybox' | docker build - DEPRECATED: The legacy builder is deprecated and will be removed in a future release. Install the buildx component to build images with BuildKit: https://docs.docker.com/go/buildx/ Sending build context to Docker daemon 2.048kB ... Signed-off-by: Sebastiaan van Stijn --- cli/command/image/build.go | 6 ---- cmd/docker/builder.go | 56 ++++++++++++++------------------------ e2e/image/build_test.go | 7 ++++- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/cli/command/image/build.go b/cli/command/image/build.go index 4ff64ec9ca..1ec0af48f9 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -184,12 +184,6 @@ func runBuild(dockerCli command.Cli, options buildOptions) error { remote string ) - if !options.quiet && dockerCli.ServerInfo().OSType != "windows" { - _, _ = fmt.Fprint(dockerCli.Err(), `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. - -`) - } - if options.stream { _, _ = fmt.Fprint(dockerCli.Err(), `DEPRECATED: The experimental --stream flag has been removed and the build context will be sent non-streaming. Enable BuildKit instead with DOCKER_BUILDKIT=1 diff --git a/cmd/docker/builder.go b/cmd/docker/builder.go index c1e0e58e63..faa7adbeca 100644 --- a/cmd/docker/builder.go +++ b/cmd/docker/builder.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "fmt" "os" "strconv" @@ -13,44 +12,29 @@ import ( ) const ( - builderDefaultPlugin = "buildx" - builderDefaultInstallMsg = `To install buildx, see https://docs.docker.com/go/buildx/` - builderErrorMsg = `%s: Required builder component %s is missing or broken.` + builderDefaultPlugin = "buildx" + buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. + Install the buildx component to build images with BuildKit: + https://docs.docker.com/go/buildx/ +` + + buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken. + Install the buildx component to build images with BuildKit: + https://docs.docker.com/go/buildx/ +` ) -type builderError struct { - warn bool - builder string - err error -} - -func newBuilderError(warn bool, builder string, err error) error { - return &builderError{ - warn: warn, - builder: builder, - err: err, - } -} - -func (e *builderError) Error() string { - var errorMsg bytes.Buffer - if e.warn { - errorMsg.WriteString(fmt.Sprintf(builderErrorMsg, "WARNING", e.builder)) +func newBuilderError(warn bool, err error) error { + var errorMsg string + if warn { + errorMsg = buildxMissingWarning } else { - errorMsg.WriteString(fmt.Sprintf(builderErrorMsg, "ERROR", e.builder)) + errorMsg = buildxMissingError } - if e.builder == builderDefaultPlugin { - errorMsg.WriteString(" ") - errorMsg.WriteString(builderDefaultInstallMsg) + if pluginmanager.IsNotFound(err) { + return errors.New(errorMsg) } - if pluginmanager.IsNotFound(e.err) { - return errors.New(errorMsg.String()).Error() - } - return errors.Errorf("%v\n\n%s", e.err, errorMsg.String()).Error() -} - -func (e *builderError) Unwrap() error { - return e.err + return fmt.Errorf("%w\n\n%s", err, errorMsg) } func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []string) ([]string, []string, error) { @@ -98,10 +82,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st if perr != nil { // if builder enforced with DOCKER_BUILDKIT=1, cmd fails if plugin missing or broken if enforcedBuilder { - return fwargs, fwosargs, newBuilderError(false, builderAlias, perr) + return fwargs, fwosargs, newBuilderError(false, perr) } // otherwise, display warning and continue - _, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, builderAlias, perr).Error()) + _, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, perr)) return args, osargs, nil } diff --git a/e2e/image/build_test.go b/e2e/image/build_test.go index 1d9eb64925..2adb1c120b 100644 --- a/e2e/image/build_test.go +++ b/e2e/image/build_test.go @@ -37,7 +37,12 @@ func TestBuildFromContextDirectoryWithTag(t *testing.T) { withWorkingDir(dir)) defer icmd.RunCommand("docker", "image", "rm", "myimage") - result.Assert(t, icmd.Expected{Err: "DEPRECATED: The legacy builder is deprecated and will be removed in a future release."}) + const buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. + Install the buildx component to build images with BuildKit: + https://docs.docker.com/go/buildx/ +` + + result.Assert(t, icmd.Expected{Err: buildxMissingWarning}) output.Assert(t, result.Stdout(), map[int]func(string) error{ 0: output.Prefix("Sending build context to Docker daemon"), 1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:3.6"),