diff --git a/.github/workflows/main_darwin.yml b/.github/workflows/main_darwin.yml index 4f34fe1c..1be04083 100644 --- a/.github/workflows/main_darwin.yml +++ b/.github/workflows/main_darwin.yml @@ -17,5 +17,5 @@ jobs: with: go-version: 1.17 - - run: go run ./cmds/kcl-go run hello.k - - run: go test -p 1 ./... + # Parallel tests + - run: go test ./... diff --git a/.github/workflows/main_linux.yml b/.github/workflows/main_linux.yml index b6cbeab7..b27e4706 100644 --- a/.github/workflows/main_linux.yml +++ b/.github/workflows/main_linux.yml @@ -17,8 +17,8 @@ jobs: with: go-version: 1.17 - - run: go run ./cmds/kcl-go run hello.k - - run: go test -p 1 -v -coverprofile=profile.cov ./... + # Parallel tests + - run: go test -v -coverprofile=profile.cov ./... - uses: shogo82148/actions-goveralls@v1 with: path-to-profile: profile.cov diff --git a/.github/workflows/main_windows.yml b/.github/workflows/main_windows.yml index d01cc771..2af06544 100644 --- a/.github/workflows/main_windows.yml +++ b/.github/workflows/main_windows.yml @@ -22,6 +22,5 @@ jobs: with: python-version: '3.10' - - run: go install ./... && C:\Users\runneradmin\go\bin\kcl-go.exe run hello.k - - run: go run ./cmds/kcl-go run hello.k - - run: go test -p 1 ./... + # Parallel tests + - run: go test ./... diff --git a/go.mod b/go.mod index bdd3e5bd..2a6cbcde 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/chai2010/jsonv v1.1.3 github.com/chai2010/protorpc v1.1.4 + github.com/gofrs/flock v0.8.1 github.com/golang/protobuf v1.5.3 github.com/google/go-cmp v0.5.9 github.com/julienschmidt/httprouter v1.3.0 diff --git a/go.sum b/go.sum index adb094f2..cf41ba96 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= +github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= diff --git a/pkg/kclvm_runtime/kclvm.go b/pkg/kclvm_runtime/kclvm.go index f57c2a59..01a76b3a 100644 --- a/pkg/kclvm_runtime/kclvm.go +++ b/pkg/kclvm_runtime/kclvm.go @@ -3,20 +3,37 @@ package kclvm_runtime import ( + "context" _ "embed" "errors" "os/exec" "path/filepath" "runtime" + "time" + "github.com/gofrs/flock" kclvmArtifact "kusionstack.io/kclvm-artifact-go" "kusionstack.io/kclvm-go/pkg/logger" "kusionstack.io/kclvm-go/pkg/path" ) func init() { - - err := kclvmArtifact.InstallKclvm(path.LibPath()) + // Get the install lib path. + path := path.LibPath() + // Acquire a file lock for process synchronization + lockPath := filepath.Join(path, "init.lock") + fileLock := flock.New(lockPath) + lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + locked, err := fileLock.TryLockContext(lockCtx, time.Second) + if err == nil && locked { + defer fileLock.Unlock() + } + if err != nil { + logger.GetLogger().Warningf("install kclvm failed: %s", err.Error()) + } + // Install lib + err = kclvmArtifact.InstallKclvm(path) if err != nil { logger.GetLogger().Warningf("install kclvm failed: %s", err.Error()) } diff --git a/pkg/path/lazypath_unix.go b/pkg/path/lazypath_unix.go index b36329ad..55c15a1a 100644 --- a/pkg/path/lazypath_unix.go +++ b/pkg/path/lazypath_unix.go @@ -16,9 +16,14 @@ package path import ( - "path/filepath" + "go/build" + "os" ) func libHome() string { - return filepath.Join(HomeDir(), ".config") + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = build.Default.GOPATH + } + return gopath } diff --git a/pkg/path/lib.go b/pkg/path/lib.go index 908cda3e..819afd15 100644 --- a/pkg/path/lib.go +++ b/pkg/path/lib.go @@ -12,7 +12,7 @@ // limitations under the License. package path -const lp = lazypath("kcl") +const lp = lazypath("") // LibPath returns the path where the kcl lib installed location. func LibPath(elem ...string) string { return lp.libPath(elem...) }