Skip to content

Commit

Permalink
Merge pull request #286 from zong-zhe/fix-dep-path
Browse files Browse the repository at this point in the history
fix: fix missing version when add dependencies from local path
  • Loading branch information
Peefy authored Mar 27, 2024
2 parents ed2f17c + e8e2f8a commit 90314fd
Show file tree
Hide file tree
Showing 29 changed files with 201 additions and 25 deletions.
46 changes: 28 additions & 18 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,15 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
searchFullPath := filepath.Join(searchPath, d.FullName)
if !update {
if d.IsFromLocal() {
searchFullPath = d.GetLocalFullPath(kclPkg.HomePath)
depPkg, err := c.LoadPkgFromPath(d.GetLocalFullPath(kclPkg.HomePath))
if err != nil {
return err
}
d.FromKclPkg(depPkg)
} else {
d.LocalFullPath = searchFullPath
}

// Find it and update the local path of the dependency.
d.LocalFullPath = searchFullPath
kclPkg.Dependencies.Deps[name] = d

} else {
if utils.DirExists(searchFullPath) && (c.GetNoSumCheck() || utils.CheckPackageSum(d.Sum, searchFullPath)) {
// Find it and update the local path of the dependency.
Expand All @@ -241,6 +243,11 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
return reporter.NewErrorEvent(reporter.CalSumFailed, err, fmt.Sprintf("failed to calculate checksum for '%s' in '%s'", d.Name, searchFullPath))
}
d.Sum = sum
depPkg, err := c.LoadPkgFromPath(d.GetLocalFullPath(kclPkg.HomePath))
if err != nil {
return err
}
d.FromKclPkg(depPkg)
kclPkg.Dependencies.Deps[name] = d
} else {
// Otherwise, re-vendor it.
Expand Down Expand Up @@ -714,7 +721,7 @@ func (c *KpmClient) VendorDeps(kclPkg *pkg.KclPkg) error {
}

// FillDepInfo will fill registry information for a dependency.
func (c *KpmClient) FillDepInfo(dep *pkg.Dependency) error {
func (c *KpmClient) FillDepInfo(dep *pkg.Dependency, homepath string) error {
if dep.Source.Local != nil {
dep.LocalFullPath = dep.Source.Local.Path
return nil
Expand Down Expand Up @@ -753,7 +760,7 @@ func (c *KpmClient) FillDepInfo(dep *pkg.Dependency) error {
// FillDependenciesInfo will fill registry information for all dependencies in a kcl.mod.
func (c *KpmClient) FillDependenciesInfo(modFile *pkg.ModFile) error {
for k, v := range modFile.Deps {
err := c.FillDepInfo(&v)
err := c.FillDepInfo(&v, modFile.HomePath)
if err != nil {
return err
}
Expand All @@ -763,7 +770,7 @@ func (c *KpmClient) FillDependenciesInfo(modFile *pkg.ModFile) error {
}

// Download will download the dependency to the local path.
func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Dependency, error) {
func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*pkg.Dependency, error) {
if dep.Source.Git != nil {
_, err := c.DownloadFromGit(dep.Source.Git, localPath)
if err != nil {
Expand All @@ -788,24 +795,26 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend
}

if dep.Source.Oci != nil {
pkg, err := c.DownloadPkgFromOci(dep.Source.Oci, localPath)
kpkg, err := c.DownloadPkgFromOci(dep.Source.Oci, localPath)
if err != nil {
return nil, err
}
dep.Version = pkg.GetPkgVersion()
dep.LocalFullPath = pkg.HomePath
dep.FromKclPkg(kpkg)
// 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
}
dep.FullName = pkg.GetPkgFullName()
}

if dep.Source.Local != nil {
dep.LocalFullPath = dep.Source.Local.Path
kpkg, err := pkg.FindFirstKclPkgFrom(dep.GetLocalFullPath(homePath))
if err != nil {
return nil, err
}
dep.FromKclPkg(kpkg)
}

var err error
Expand Down Expand Up @@ -1202,7 +1211,7 @@ func (c *KpmClient) InitGraphAndDownloadDeps(kclPkg *pkg.KclPkg) (*pkg.Dependenc
return nil, nil, err
}

changedDeps, err := c.downloadDeps(kclPkg.ModFile.Dependencies, kclPkg.Dependencies, depGraph, root)
changedDeps, err := c.downloadDeps(kclPkg.ModFile.Dependencies, kclPkg.Dependencies, depGraph, kclPkg.HomePath, root)
if err != nil {
return nil, nil, err
}
Expand All @@ -1226,15 +1235,16 @@ func (c *KpmClient) dependencyExists(dep *pkg.Dependency, lockDeps *pkg.Dependen
if !c.noSumCheck && present {
// If the dependent package does not exist locally, then method 'check' will return false.
if c.noSumCheck || check(lockDep, filepath.Join(c.homePath, dep.FullName)) {
return dep
return &lockDep
}
}

return nil
}

// downloadDeps will download all the dependencies of the current kcl package.
func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencies, depGraph graph.Graph[string, string], parent string) (*pkg.Dependencies, error) {
func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencies, depGraph graph.Graph[string, string], pkghome, parent string) (*pkg.Dependencies, error) {

newDeps := pkg.Dependencies{
Deps: make(map[string]pkg.Dependency),
}
Expand All @@ -1260,7 +1270,7 @@ func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencie
os.RemoveAll(dir)

// download dependencies
lockedDep, err := c.Download(&d, dir)
lockedDep, err := c.Download(&d, pkghome, dir)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1325,7 +1335,7 @@ func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencie
}

// Download the dependencies.
nested, err := c.downloadDeps(deppkg.ModFile.Dependencies, lockDeps, depGraph, source)
nested, err := c.downloadDeps(deppkg.ModFile.Dependencies, lockDeps, depGraph, deppkg.HomePath, source)
if err != nil {
return nil, err
}
Expand Down
56 changes: 53 additions & 3 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestDownloadOci(t *testing.T) {
}
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
dep, err := kpmcli.Download(&depFromOci, testPath)
dep, err := kpmcli.Download(&depFromOci, "", testPath)
assert.Equal(t, err, nil)
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "k8s_1.27")
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestDownloadLatestOci(t *testing.T) {
}
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
dep, err := kpmcli.Download(&depFromOci, testPath)
dep, err := kpmcli.Download(&depFromOci, "", testPath)
assert.Equal(t, err, nil)
assert.Equal(t, dep.Name, "helloworld")
assert.Equal(t, dep.FullName, "helloworld_0.1.1")
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestCyclicDependency(t *testing.T) {

_, _, err = kpmcli.InitGraphAndDownloadDeps(kclPkg)
assert.Equal(t, err, reporter.NewErrorEvent(
reporter.CircularDependencyExist, nil, "adding bbb as a dependency results in a cycle",
reporter.CircularDependencyExist, nil, "adding [email protected] as a dependency results in a cycle",
))

err = os.Chdir(currentDir)
Expand Down Expand Up @@ -1315,3 +1315,53 @@ func TestLoadPkgFormOci(t *testing.T) {
assert.Equal(t, kclpkg.GetPkgName(), tc.Name)
}
}

func TestAddWithLocalPath(t *testing.T) {

testpath := getTestDir("add_with_local_path")

initpath := filepath.Join(testpath, "init")
tmppath := filepath.Join(testpath, "tmp")
expectpath := filepath.Join(testpath, "expect")

defer func() {
err := os.RemoveAll(tmppath)
assert.Equal(t, err, nil)
}()

err := copy.Copy(initpath, tmppath)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kpmcli.SetLogWriter(nil)

tmpPkgPath := filepath.Join(tmppath, "pkg")
opts := opt.AddOptions{
LocalPath: tmpPkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang",
PkgName: "helloworld",
Tag: "0.1.1",
},
},
}

kclPkg, err := kpmcli.LoadPkgFromPath(tmpPkgPath)
assert.Equal(t, err, nil)

_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)

gotpkg, err := kpmcli.LoadPkgFromPath(tmpPkgPath)
assert.Equal(t, err, nil)
expectpath = filepath.Join(expectpath, "pkg")
expectpkg, err := kpmcli.LoadPkgFromPath(expectpath)
assert.Equal(t, err, nil)

assert.Equal(t, len(gotpkg.Dependencies.Deps), len(expectpkg.Dependencies.Deps))
assert.Equal(t, gotpkg.Dependencies.Deps["dep_pkg"].FullName, expectpkg.Dependencies.Deps["dep_pkg"].FullName)
assert.Equal(t, gotpkg.Dependencies.Deps["dep_pkg"].Version, expectpkg.Dependencies.Deps["dep_pkg"].Version)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "dep_pkg"
edition = "v0.8.0"
version = "0.0.1"

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
8 changes: 8 additions & 0 deletions pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "pkg"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
dep_pkg = { path = "../dep_pkg" }
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.1" }
15 changes: 15 additions & 0 deletions pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[dependencies]
[dependencies.dep_pkg]
name = "dep_pkg"
full_name = "dep_pkg_0.0.1"
version = "0.0.1"
sum = "C3TjaZJVdt4G8TtbiCxTUO/zAlf7fWJTAvs/CDMnlxY="
path = "../dep_pkg"
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
version = "0.1.1"
sum = "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ="
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.1"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_local_path/expect/pkg/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
5 changes: 5 additions & 0 deletions pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "dep_pkg"
edition = "v0.8.0"
version = "0.0.1"

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
7 changes: 7 additions & 0 deletions pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "pkg"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
dep_pkg = { path = "../dep_pkg" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.dep_pkg]
name = "dep_pkg"
full_name = "dep_pkg_0.0.1"
version = "0.0.1"
sum = "C3TjaZJVdt4G8TtbiCxTUO/zAlf7fWJTAvs/CDMnlxY="
path = "../dep_pkg"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_local_path/init/pkg/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
2 changes: 1 addition & 1 deletion pkg/client/test_data/resolve_metadata/kcl.mod.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
name = "konfig"
full_name = "konfig_v0.0.1"
version = "v0.0.1"
sum = "bcUvvcR21AFUE/Zf63wx6HkqzFYH1vihmE+SCd8OOCs="
sum = "XFvHdBAoY/+qpJWmj8cjwOwZO8a3nX/7SE35cTxQOFU="
url = "https://github.com/awesome-kusion/konfig.git"
git_tag = "v0.0.1"
20 changes: 17 additions & 3 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (profile *Profile) GetEntries() []string {
// FillDependenciesInfo will fill registry information for all dependencies in a kcl.mod.
func (modFile *ModFile) FillDependenciesInfo() error {
for k, v := range modFile.Deps {
err := v.FillDepInfo()
err := v.FillDepInfo(modFile.HomePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,6 +168,13 @@ type Dependency struct {
Source `json:"-"`
}

func (d *Dependency) FromKclPkg(pkg *KclPkg) {
d.Name = pkg.GetPkgName()
d.FullName = pkg.GetPkgFullName()
d.Version = pkg.GetPkgVersion()
d.LocalFullPath = pkg.HomePath
}

// SetName will set the name and alias name of a dependency.
func (d *Dependency) GetAliasName() string {
return strings.ReplaceAll(d.Name, "-", "_")
Expand All @@ -176,8 +183,12 @@ func (d *Dependency) GetAliasName() string {
// WithTheSameVersion will check whether two dependencies have the same version.
func (d Dependency) WithTheSameVersion(other Dependency) bool {

sameNameAndVersion := d.Name == other.Name && d.Version == other.Version
var sameVersion = true
if len(d.Version) != 0 && len(other.Version) != 0 {
sameVersion = d.Version == other.Version

}
sameNameAndVersion := d.Name == other.Name && sameVersion
sameGitSrc := true
if d.Source.Git != nil && other.Source.Git != nil {
sameGitSrc = d.Source.Git.Url == other.Source.Git.Url &&
Expand Down Expand Up @@ -205,7 +216,7 @@ func (dep *Dependency) IsFromLocal() bool {
}

// FillDepInfo will fill registry information for a dependency.
func (dep *Dependency) FillDepInfo() error {
func (dep *Dependency) FillDepInfo(homepath string) error {
if dep.Source.Oci != nil {
settings := settings.GetSettings()
if settings.ErrorEvent != nil {
Expand All @@ -220,6 +231,9 @@ func (dep *Dependency) FillDepInfo() error {
dep.Source.Oci.Repo = urlpath
}
}
if dep.Source.Local != nil {
dep.LocalFullPath = dep.Source.Local.Path
}
return nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run <workspace>/pkg1
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program: Hello World!
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "pkg1"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
pkg2 = { path = "../pkg2" }
pkg3 = { path = "../pkg3" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[dependencies]
[dependencies.pkg2]
name = "pkg2"
full_name = "pkg2_0.0.1"
version = "0.0.1"
sum = "JbfIAXPJb3L6xX7hi/A5mrXzjpB8eFoKfEmJMdHsewY="
path = "../pkg2"
[dependencies.pkg3]
name = "pkg3"
full_name = "pkg3_0.0.1"
version = "0.0.1"
sum = "T29gAv6K/tLithhP5jVHyurV5zRFui+i1ulyMt/ncnM="
path = "../pkg3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg2

The_first_kcl_program = pkg2.The_first_kcl_program
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "pkg2"
edition = "v0.8.0"
version = "0.0.1"

[dependencies]
pkg3 = { path = "../pkg3" }
Loading

0 comments on commit 90314fd

Please sign in to comment.