Skip to content

Commit

Permalink
Merge pull request #309 from kcl-lang/fix-dep-ref-symlink
Browse files Browse the repository at this point in the history
fix: dep ref symlink
  • Loading branch information
Peefy authored Apr 30, 2024
2 parents 0d47303 + e085e5e commit 8d9958c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 58 deletions.
43 changes: 21 additions & 22 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"strings"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -836,10 +835,10 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
// Creating symbolic links in a global cache is not an optimal solution.
// This allows kclvm to locate the package by default.
// This feature is unstable and will be removed soon.
err = createDepRef(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
if err != nil {
return nil, err
}
// err = createDepRef(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
// if err != nil {
// return nil, err
// }
dep.FullName = dep.GenDepFullName()
// If the dependency is from git commit, the version is the commit id.
// If the dependency is from git tag, the version is the tag.
Expand Down Expand Up @@ -879,10 +878,10 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
// Creating symbolic links in a global cache is not an optimal solution.
// This allows kclvm to locate the package by default.
// This feature is unstable and will be removed soon.
err = createDepRef(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
if err != nil {
return nil, err
}
// err = createDepRef(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
// if err != nil {
// return nil, err
// }
}

if dep.Source.Local != nil {
Expand Down Expand Up @@ -1513,16 +1512,16 @@ func check(dep pkg.Dependency, newDepPath string) bool {
// createDepRef will create a dependency reference for the dependency saved on the local filesystem.
// On the unix-like system, it will create a symbolic link.
// On the windows system, it will create a junction.
func createDepRef(depName, refName string) error {
if runtime.GOOS == "windows" {
// 'go-getter' continuously occupies files in '.git', causing the copy operation to fail
opt := copy.Options{
Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) {
return filepath.Base(src) == constants.GitPathSuffix, nil
},
}
return copy.Copy(depName, refName, opt)
} else {
return utils.CreateSymlink(depName, refName)
}
}
// func createDepRef(depName, refName string) error {
// if runtime.GOOS == "windows" {
// // 'go-getter' continuously occupies files in '.git', causing the copy operation to fail
// opt := copy.Options{
// Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) {
// return filepath.Base(src) == constants.GitPathSuffix, nil
// },
// }
// return copy.Copy(depName, refName, opt)
// } else {
// return utils.CreateSymlink(depName, refName)
// }
// }
38 changes: 2 additions & 36 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,7 @@ func TestDownloadOci(t *testing.T) {
assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s_1.27.tar")), false)

assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s_1.27")), true)
assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s")), true)

// Check whether the reference and the dependency have the same hash.
hashDep, err := utils.HashDir(filepath.Join(downloadPath, "k8s_1.27"))
assert.Equal(t, err, nil)

depRefPath := filepath.Join(downloadPath, "k8s")
info, err := os.Lstat(depRefPath)
assert.Equal(t, err, nil)

if info.Mode()&os.ModeSymlink != 0 {
depRefPath, err = os.Readlink(depRefPath)
assert.Equal(t, err, nil)
}

hashRef, err := utils.HashDir(depRefPath)
assert.Equal(t, err, nil)
assert.Equal(t, hashDep, hashRef)
assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s")), false)
}

// TestDownloadLatestOci tests the case that the version is empty.
Expand Down Expand Up @@ -150,24 +133,7 @@ func TestDownloadLatestOci(t *testing.T) {
// Check whether the tar downloaded by `kpm add` has been deleted.
assert.Equal(t, utils.DirExists(filepath.Join(testPath, "helloworld_0.1.1.tar")), false)

assert.Equal(t, utils.DirExists(filepath.Join(getTestDir("download"), "helloworld")), true)

// Check whether the reference and the dependency have the same hash.
hashDep, err := utils.HashDir(dep.LocalFullPath)
assert.Equal(t, err, nil)

depRefPath := filepath.Join(getTestDir("download"), "helloworld")
info, err := os.Lstat(depRefPath)
assert.Equal(t, err, nil)

if info.Mode()&os.ModeSymlink != 0 {
depRefPath, err = os.Readlink(depRefPath)
assert.Equal(t, err, nil)
}

hashRef, err := utils.HashDir(depRefPath)
assert.Equal(t, err, nil)
assert.Equal(t, hashDep, hashRef)
assert.Equal(t, utils.DirExists(filepath.Join(getTestDir("download"), "helloworld")), false)
}

func TestDependencyGraph(t *testing.T) {
Expand Down

0 comments on commit 8d9958c

Please sign in to comment.