Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions buildcontext/detectbuildfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package buildcontext

import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/EarthBuild/earthbuild/domain"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/pkg/errors"
)

// EarthfileNotExistError is the struct indicating that file does not exist.
Expand Down Expand Up @@ -38,12 +38,12 @@ func detectBuildFile(ref domain.Reference, localDir string) (string, error) {
if os.IsNotExist(err) {
return "", EarthfileNotExistError{Target: ref.String()}
} else if err != nil {
return "", errors.Wrapf(err, "stat file %s", buildEarthPath)
return "", fmt.Errorf("stat file %s: %w", buildEarthPath, err)
}

return buildEarthPath, nil
} else if err != nil {
return "", errors.Wrapf(err, "stat file %s", earthfilePath)
return "", fmt.Errorf("stat file %s: %w", earthfilePath, err)
}

return earthfilePath, nil
Expand Down Expand Up @@ -89,7 +89,7 @@ func fileExists(ctx context.Context, ref gwclient.Reference, fpath string) (bool
IncludePattern: file,
})
if err != nil {
return false, errors.Wrapf(err, "cannot read dir %s", dir)
return false, fmt.Errorf("cannot read dir %s: %w", dir, err)
}

for _, fstat := range fstats {
Expand Down
13 changes: 7 additions & 6 deletions buildcontext/excludes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package buildcontext

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/EarthBuild/earthbuild/util/fileutil"
"github.com/moby/patternmatcher/ignorefile"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -46,15 +47,15 @@ func readExcludes(dir string, noImplicitIgnore bool, useDockerIgnore bool) ([]st

earthExists, err := fileutil.FileExists(earthIgnoreFilePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to check if %s exists", earthIgnoreFilePath)
return nil, fmt.Errorf("failed to check if %s exists: %w", earthIgnoreFilePath, err)
}

// earthlyIgnoreFile
earthlyIgnoreFilePath := filepath.Join(dir, earthlyIgnoreFile)

earthlyExists, err := fileutil.FileExists(earthlyIgnoreFilePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to check if %s exists", earthlyIgnoreFilePath)
return nil, fmt.Errorf("failed to check if %s exists: %w", earthlyIgnoreFilePath, err)
}

// dockerIgnoreFile
Expand All @@ -64,7 +65,7 @@ func readExcludes(dir string, noImplicitIgnore bool, useDockerIgnore bool) ([]st
if useDockerIgnore {
dockerExists, err = fileutil.FileExists(dockerIgnoreFilePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to check if %s exists", dockerIgnoreFilePath)
return nil, fmt.Errorf("failed to check if %s exists: %w", dockerIgnoreFilePath, err)
}
}

Expand Down Expand Up @@ -94,13 +95,13 @@ func readExcludes(dir string, noImplicitIgnore bool, useDockerIgnore bool) ([]st

f, err := os.Open(filePath) // #nosec G304
if err != nil {
return nil, errors.Wrapf(err, "read %s", filePath)
return nil, fmt.Errorf("read %s: %w", filePath, err)
}
defer f.Close()

excludes, err := ignorefile.ReadAll(f)
if err != nil {
return nil, errors.Wrapf(err, "parse %s", filePath)
return nil, fmt.Errorf("parse %s: %w", filePath, err)
}

return append(excludes, defaultExcludes...), nil
Expand Down
42 changes: 22 additions & 20 deletions buildcontext/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/moby/buildkit/client/llb"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
buildkitgitutil "github.com/moby/buildkit/util/gitutil"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -72,13 +71,13 @@ func (gr *gitResolver) expandWildcard(
ctx context.Context, gwClient gwclient.Client, platr *platutil.Resolver, target domain.Target, pattern string,
) ([]string, error) {
if !target.IsRemote() {
return nil, errors.Errorf("unexpected local reference %s", target.String())
return nil, fmt.Errorf("unexpected local reference %s", target.String())
}

rgp, _, subDir, err := gr.resolveGitProject(ctx, gwClient, platr, target)
if err != nil {
return nil, errors.Wrapf(err, "failed resolving git project [platform: %s/%s]",
platr.LLBNative().OS, platr.LLBNative().Architecture)
return nil, fmt.Errorf("failed resolving git project [platform: %s/%s]: %w",
platr.LLBNative().OS, platr.LLBNative().Architecture, err)
}

fullPattern := filepath.Join(subDir, pattern)
Expand Down Expand Up @@ -114,13 +113,13 @@ func (gr *gitResolver) resolveEarthProject(
featureFlagOverrides string,
) (*Data, error) {
if !ref.IsRemote() {
return nil, errors.Errorf("unexpected local reference %s", ref.String())
return nil, fmt.Errorf("unexpected local reference %s", ref.String())
}

rgp, gitURL, subDir, err := gr.resolveGitProject(ctx, gwClient, platr, ref)
if err != nil {
return nil, errors.Wrapf(err, "failed resolving git project [platform: %s/%s]",
platr.LLBNative().OS, platr.LLBNative().Architecture)
return nil, fmt.Errorf("failed resolving git project [platform: %s/%s]: %w",
platr.LLBNative().OS, platr.LLBNative().Architecture, err)
}

var buildContextFactory llbfactory.Factory
Expand All @@ -143,7 +142,7 @@ func (gr *gitResolver) resolveEarthProject(
rgp.state, []string{subDir}, platr.Scratch(), "./", false, false, false, "root:root", nil, false, false, false,
llb.WithCustomNamef("%sCOPY git context %s", vm.ToVertexPrefix(), ref.String()))
if err != nil {
return nil, errors.Wrap(err, "copyOp failed in resolveEarthProject")
return nil, fmt.Errorf("copyOp failed in resolveEarthProject: %w", err)
}

buildContextFactory = llbfactory.PreconstructedState(copyState)
Expand All @@ -162,7 +161,7 @@ func (gr *gitResolver) resolveEarthProject(
localBuildFileValue, err := gr.buildFileCache.Do(ctx, key, func(ctx context.Context, _ any) (any, error) {
earthfileTmpDir, inErr := os.MkdirTemp(os.TempDir(), "earthly-git")
if inErr != nil {
return nil, errors.Wrap(inErr, "create temp dir for Earthfile")
return nil, fmt.Errorf("create temp dir for Earthfile: %w", inErr)
}

gr.cleanCollection.Add(func() error {
Expand All @@ -171,9 +170,10 @@ func (gr *gitResolver) resolveEarthProject(

gitState, inErr := llbutil.StateToRef(
ctx, gwClient, rgp.state, false,
platr.SubResolver(platutil.NativePlatform), nil)
platr.SubResolver(platutil.NativePlatform), nil,
)
if inErr != nil {
return nil, errors.Wrap(inErr, "state to ref git meta")
return nil, fmt.Errorf("state to ref git meta: %w", inErr)
}

bf, inErr := detectBuildFileInRef(ctx, ref, gitState, subDir)
Expand All @@ -185,14 +185,14 @@ func (gr *gitResolver) resolveEarthProject(
Filename: bf,
})
if inErr != nil {
return nil, errors.Wrap(inErr, "read build file")
return nil, fmt.Errorf("read build file: %w", inErr)
}

localBuildFilePath := filepath.Join(earthfileTmpDir, path.Base(bf))

inErr = os.WriteFile(localBuildFilePath, bfBytes, 0o700) // #nosec G306
if inErr != nil {
return nil, errors.Wrapf(inErr, "write build file to tmp dir at %s", localBuildFilePath)
return nil, fmt.Errorf("write build file to tmp dir at %s: %w", localBuildFilePath, inErr)
}

var ftrs *features.Features
Expand Down Expand Up @@ -257,7 +257,7 @@ func (gr *gitResolver) resolveGitProject(

gitURL, subDir, keyScans, sshCommand, err = gr.gitLookup.GetCloneURL(ctx, ref.GetGitURL())
if err != nil {
return nil, "", "", errors.Wrap(err, "failed to get url for cloning")
return nil, "", "", fmt.Errorf("failed to get url for cloning: %w", err)
}

// Check the cache first.
Expand Down Expand Up @@ -300,7 +300,8 @@ func (gr *gitResolver) resolveGitProject(

opImg := pllb.Image(
gitImage, llb.MarkImageInternal, llb.ResolveModePreferLocal,
llb.Platform(platr.LLBNative()))
llb.Platform(platr.LLBNative()),
)

// Get git hash.
gitHashOpts := []llb.RunOption{
Expand Down Expand Up @@ -338,9 +339,10 @@ func (gr *gitResolver) resolveGitProject(

gitMetaRef, err = llbutil.StateToRef(
ctx, gwClient, gitMetaState, noCache,
platr.SubResolver(platutil.NativePlatform), nil)
platr.SubResolver(platutil.NativePlatform), nil,
)
if err != nil {
return nil, errors.Wrap(err, "state to ref git meta")
return nil, fmt.Errorf("state to ref git meta: %w", err)
}

var unameM []byte
Expand All @@ -349,7 +351,7 @@ func (gr *gitResolver) resolveGitProject(
Filename: "uname-m",
})
if err != nil {
return nil, errors.Wrap(err, "read uname-m")
return nil, fmt.Errorf("read uname-m: %w", err)
}

var imgArch string
Expand Down Expand Up @@ -381,15 +383,15 @@ func (gr *gitResolver) resolveGitProject(
Filename: name,
})
if err != nil {
return nil, errors.Wrap(err, "read "+name)
return nil, fmt.Errorf("%s: %w", "read "+name, err)
Comment thread
janishorsts marked this conversation as resolved.
}
}

var gitBranch string

gitBranch, err = gr.readGitBranch(ctx, gitMetaRef)
if err != nil {
return nil, errors.Wrap(err, "read git-branch")
return nil, fmt.Errorf("read git-branch: %w", err)
}

isNotHead := func(s string) bool {
Expand Down
Loading
Loading