From c47eb3bf5a470ae88c24be00a96334d6922e91eb Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 22 Aug 2023 12:16:19 +0100 Subject: [PATCH 1/2] git: propogate failure to locate git binary Signed-off-by: Justin Chadwell --- util/gitutil/gitutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/gitutil/gitutil.go b/util/gitutil/gitutil.go index 8ab76ab200d..22aa72ee009 100644 --- a/util/gitutil/gitutil.go +++ b/util/gitutil/gitutil.go @@ -49,7 +49,7 @@ func New(opts ...Option) (*Git, error) { c.gitpath, err = gitPath(c.wd) if err != nil { - return nil, errors.New("git not found in PATH") + return nil, err } return c, nil From a43d9a67c7f45a5d2129bd62d1c61d3aff83f324 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 22 Aug 2023 12:17:14 +0100 Subject: [PATCH 2/2] git: fix error wrapping to ensure internal errors are propogated Also, tidy up the error printing, so that now we always print out the "current commit information was not captured by the build" message, instead of just for not locating the git binary. Before: WARNING: buildx: git was not found in the system. Current commit information was not captured by the build After: WARNING: current commit information was not captured by the build: git was not found in the system: Signed-off-by: Justin Chadwell --- build/build.go | 2 +- build/git.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build/build.go b/build/build.go index 619bf132f24..5104e8db4d8 100644 --- a/build/build.go +++ b/build/build.go @@ -737,7 +737,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s hasMobyDriver := false gitattrs, err := getGitAttributes(ctx, opt.Inputs.ContextPath, opt.Inputs.DockerfilePath) if err != nil { - logrus.Warn(err) + logrus.WithError(err).Warn("current commit information was not captured by the build") } for i, np := range m[k] { node := nodes[np.driverIndex] diff --git a/build/git.go b/build/git.go index d80a00bab25..1e0a284de59 100644 --- a/build/git.go +++ b/build/git.go @@ -51,21 +51,21 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st gitc, err := gitutil.New(gitutil.WithContext(ctx), gitutil.WithWorkingDir(wd)) if err != nil { - if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() { - return res, errors.New("buildx: git was not found in the system. Current commit information was not captured by the build") + if st, err1 := os.Stat(path.Join(wd, ".git")); err1 == nil && st.IsDir() { + return res, errors.Wrap(err, "git was not found in the system") } return } if !gitc.IsInsideWorkTree() { if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() { - return res, errors.New("buildx: failed to read current commit information with git rev-parse --is-inside-work-tree") + return res, errors.New("failed to read current commit information with git rev-parse --is-inside-work-tree") } return res, nil } if sha, err := gitc.FullCommit(); err != nil && !gitutil.IsUnknownRevision(err) { - return res, errors.Wrapf(err, "buildx: failed to get git commit") + return res, errors.Wrap(err, "failed to get git commit") } else if sha != "" { checkDirty := false if v, ok := os.LookupEnv("BUILDX_GIT_CHECK_DIRTY"); ok { @@ -95,7 +95,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st if setGitLabels { if root, err := gitc.RootDir(); err != nil { - return res, errors.Wrapf(err, "buildx: failed to get git root dir") + return res, errors.Wrap(err, "failed to get git root dir") } else if root != "" { if dockerfilePath == "" { dockerfilePath = filepath.Join(wd, "Dockerfile")