From fdb4b94276f9e6068b3fd2ca78bead7c9a7526b5 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Tue, 21 Dec 2021 14:25:18 -0800 Subject: [PATCH] state: moved stdlib lock to upper function to fix race condition Signed-off-by: Sam Alba --- state/project.go | 30 ++++++++++++++++++++++++------ stdlib/stdlib.go | 14 -------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/state/project.go b/state/project.go index c85a50d3..843e138e 100644 --- a/state/project.go +++ b/state/project.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" + "github.com/gofrs/flock" "github.com/rs/zerolog/log" "go.dagger.io/dagger/keychain" "go.dagger.io/dagger/plancontext" @@ -31,6 +32,7 @@ const ( planDir = "plan" manifestFile = "values.yaml" computedFile = "computed.json" + lockFilePath = "dagger.lock" ) type Project struct { @@ -357,12 +359,6 @@ func cueModInit(ctx context.Context, parentDir string) error { lg := log.Ctx(ctx) modDir := path.Join(parentDir, "cue.mod") - if err := os.Mkdir(modDir, 0755); err != nil { - if !errors.Is(err, os.ErrExist) { - return err - } - } - modFile := path.Join(modDir, "module.cue") if _, err := os.Stat(modFile); err != nil { if !errors.Is(err, os.ErrNotExist) { @@ -395,6 +391,28 @@ func VendorUniverse(ctx context.Context, p string) error { p = getCueModParent() } + cueModDir := path.Join(p, "cue.mod") + if err := os.Mkdir(cueModDir, 0755); err != nil { + if !errors.Is(err, os.ErrExist) { + return err + } + } + + if err := os.MkdirAll(cueModDir, 0755); err != nil { + return err + } + + lockFilePath := path.Join(cueModDir, lockFilePath) + fileLock := flock.New(lockFilePath) + if err := fileLock.Lock(); err != nil { + return err + } + + defer func() { + fileLock.Unlock() + os.Remove(lockFilePath) + }() + // ensure cue module is initialized if err := cueModInit(ctx, p); err != nil { return err diff --git a/stdlib/stdlib.go b/stdlib/stdlib.go index 958fe794..f14bb2b3 100644 --- a/stdlib/stdlib.go +++ b/stdlib/stdlib.go @@ -8,8 +8,6 @@ import ( "os" "path" "path/filepath" - - "github.com/gofrs/flock" ) var ( @@ -20,21 +18,9 @@ var ( ModuleName = "alpha.dagger.io" EnginePackage = fmt.Sprintf("%s/europa/dagger/engine", ModuleName) Path = path.Join("cue.mod", "pkg", ModuleName) - lockFilePath = path.Join("cue.mod", "dagger.lock") ) func Vendor(ctx context.Context, mod string) error { - lockFilePath := path.Join(mod, lockFilePath) - fileLock := flock.New(lockFilePath) - if err := fileLock.Lock(); err != nil { - return err - } - - defer func() { - fileLock.Unlock() - os.Remove(lockFilePath) - }() - // Remove any existing copy of the universe if err := os.RemoveAll(path.Join(mod, Path)); err != nil { return err