stdlib: lock when vendoring to avoid race conditions

Signed-off-by: Sam Alba <samalba@users.noreply.github.com>
This commit is contained in:
Sam Alba 2021-11-08 17:29:56 -08:00
parent 1dd538e729
commit ace8ea26f1

View File

@ -8,6 +8,8 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"github.com/gofrs/flock"
) )
var ( var (
@ -17,9 +19,17 @@ var (
PackageName = "alpha.dagger.io" PackageName = "alpha.dagger.io"
Path = path.Join("cue.mod", "pkg", PackageName) Path = path.Join("cue.mod", "pkg", PackageName)
lockFilePath = path.Join("cue.mod", "dagger.lock")
) )
func Vendor(ctx context.Context, mod string) error { func Vendor(ctx context.Context, mod string) error {
fileLock := flock.New(path.Join(mod, lockFilePath))
if err := fileLock.Lock(); err != nil {
return err
}
defer fileLock.Unlock()
// Remove any existing copy of the universe // Remove any existing copy of the universe
if err := os.RemoveAll(path.Join(mod, Path)); err != nil { if err := os.RemoveAll(path.Join(mod, Path)); err != nil {
return err return err