From f023318b883ba46467896a581ac54106860d01a7 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Sun, 5 Dec 2021 21:11:54 +0000 Subject: [PATCH] Use one Yarn cache per build fixes #72 fixes #71 --- pkg/leeway/build.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/leeway/build.go b/pkg/leeway/build.go index f48e8a9..f5b2261 100644 --- a/pkg/leeway/build.go +++ b/pkg/leeway/build.go @@ -2,6 +2,7 @@ package leeway import ( "context" + "crypto/rand" "encoding/base64" "encoding/json" "fmt" @@ -13,6 +14,7 @@ import ( "regexp" "strings" "sync" + "time" "github.com/gitpod-io/leeway/pkg/gokart" log "github.com/sirupsen/logrus" @@ -45,6 +47,7 @@ const ( type buildContext struct { buildOptions buildDir string + buildID string mu sync.Mutex newlyBuiltPackages map[string]*Package @@ -99,9 +102,17 @@ func newBuildContext(options buildOptions) (ctx *buildContext, err error) { buildLimit = semaphore.NewWeighted(options.MaxConcurrentTasks) } + b := make([]byte, 4) + _, err = rand.Read(b) + if err != nil { + return nil, xerrors.Errorf("cannot produce random build ID: %w", err) + } + buildID := fmt.Sprintf("%d-%x", time.Now().UnixNano(), b) + ctx = &buildContext{ buildOptions: options, buildDir: buildDir, + buildID: buildID, newlyBuiltPackages: make(map[string]*Package), pkgLockCond: sync.NewCond(&sync.Mutex{}), pkgLocks: make(map[string]struct{}), @@ -808,7 +819,7 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (err erro log.Debugf("%s is not set, defaulting to \"network\"", EnvvarYarnMutex) yarnMutex = "network" } - yarnCache := filepath.Join(buildctx.BuildDir(), "yarn-cache") + yarnCache := filepath.Join(buildctx.BuildDir(), fmt.Sprintf("yarn-cache-%s", buildctx.buildID)) if len(cfg.Commands.Install) == 0 { commands = append(commands, []string{"yarn", "install", "--mutex", yarnMutex, "--cache-folder", yarnCache}) } else {