Skip to content

Commit

Permalink
feat: add context info to docker build errors (#3920)
Browse files Browse the repository at this point in the history
its too hard to debug docker build issues... sometimes is just a typo in
the binary name, and you might end debugging it for way too long...

this prints the full path to the build context (so, locally at least,
you can cd into it) and also all the files available there when the
error seems to be one of the "file not found" kind.

Hopefully this helps fixing things easier :)

closes #3912

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Apr 9, 2023
1 parent 8a6de5c commit d5a413f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/pipe/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -198,6 +199,26 @@ func process(ctx *context.Context, docker config.Docker, artifacts []*artifact.A

log.Info("building docker image")
if err := imagers[docker.Use].Build(ctx, tmp, images, buildFlags); err != nil {
if strings.Contains(err.Error(), "file not found") || strings.Contains(err.Error(), "not found: not found") {
var files []string
_ = filepath.Walk(tmp, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
files = append(files, info.Name())
return nil
})
return fmt.Errorf(`seems like you tried to copy a file that is not available in the build context.
Here's more information about the build context:
dir: %q
files in that dir:
%s
Previous error:
%w`, tmp, strings.Join(files, "\n "), err)
}
return err
}

Expand Down
14 changes: 14 additions & 0 deletions internal/pipe/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,20 @@ func TestRunPipe(t *testing.T) {
pubAssertError: shouldNotErr,
manifestAssertError: shouldNotErr,
},
"wrong binary name": {
dockers: []config.Docker{
{
ImageTemplates: []string{
registry + "goreleaser/wrong_bin_name:v1",
},
Goos: "linux",
Goarch: "amd64",
Dockerfile: "testdata/Dockerfile.wrongbin",
},
},
assertError: shouldErr("seems like you tried to copy a file that is not available in the build context"),
assertImageLabels: noLabels,
},
"templated-dockerfile-invalid": {
dockers: []config.Docker{
{
Expand Down
3 changes: 3 additions & 0 deletions internal/pipe/docker/testdata/Dockerfile.wrongbin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM alpine
# a typo:
ADD mybyn /

0 comments on commit d5a413f

Please sign in to comment.