From 2847a4af6ff90f8163c6af3848edb4e499521bba Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 26 Mar 2024 20:53:01 +0800 Subject: [PATCH] fix: fix missing version when add dependencies from local path Signed-off-by: zongz --- pkg/client/client.go | 29 +++++----- pkg/client/client_test.go | 56 ++++++++++++++++++- .../expect/dep_pkg/kcl.mod | 5 ++ .../expect/dep_pkg/kcl.mod.lock | 0 .../add_with_local_path/expect/dep_pkg/main.k | 1 + .../add_with_local_path/expect/pkg/kcl.mod | 8 +++ .../expect/pkg/kcl.mod.lock | 15 +++++ .../add_with_local_path/expect/pkg/main.k | 1 + .../add_with_local_path/init/dep_pkg/kcl.mod | 5 ++ .../init/dep_pkg/kcl.mod.lock | 0 .../add_with_local_path/init/dep_pkg/main.k | 1 + .../add_with_local_path/init/pkg/kcl.mod | 7 +++ .../add_with_local_path/init/pkg/kcl.mod.lock | 7 +++ .../add_with_local_path/init/pkg/main.k | 1 + pkg/package/modfile.go | 14 ++++- 15 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod create mode 100644 pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod.lock create mode 100644 pkg/client/test_data/add_with_local_path/expect/dep_pkg/main.k create mode 100644 pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod create mode 100644 pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod.lock create mode 100644 pkg/client/test_data/add_with_local_path/expect/pkg/main.k create mode 100644 pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod create mode 100644 pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod.lock create mode 100644 pkg/client/test_data/add_with_local_path/init/dep_pkg/main.k create mode 100644 pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod create mode 100644 pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod.lock create mode 100644 pkg/client/test_data/add_with_local_path/init/pkg/main.k diff --git a/pkg/client/client.go b/pkg/client/client.go index b762fcc0..28b97c48 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -714,7 +714,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 @@ -753,7 +753,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 } @@ -763,7 +763,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 { @@ -788,12 +788,11 @@ 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. @@ -801,11 +800,14 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend 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 @@ -1202,7 +1204,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 } @@ -1226,7 +1228,7 @@ 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 } } @@ -1234,7 +1236,8 @@ func (c *KpmClient) dependencyExists(dep *pkg.Dependency, lockDeps *pkg.Dependen } // 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), } @@ -1260,7 +1263,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 } @@ -1325,7 +1328,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 } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 0f759e36..6173b3aa 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -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") @@ -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") @@ -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 aaa@0.0.1 as a dependency results in a cycle", )) err = os.Chdir(currentDir) @@ -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) +} diff --git a/pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod b/pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod new file mode 100644 index 00000000..9f3a6681 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "dep_pkg" +edition = "v0.8.0" +version = "0.0.1" + diff --git a/pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod.lock b/pkg/client/test_data/add_with_local_path/expect/dep_pkg/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_local_path/expect/dep_pkg/main.k b/pkg/client/test_data/add_with_local_path/expect/dep_pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/expect/dep_pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod b/pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod new file mode 100644 index 00000000..778e75b0 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod @@ -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" } diff --git a/pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod.lock b/pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod.lock new file mode 100644 index 00000000..7a5d3c5b --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/expect/pkg/kcl.mod.lock @@ -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" diff --git a/pkg/client/test_data/add_with_local_path/expect/pkg/main.k b/pkg/client/test_data/add_with_local_path/expect/pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/expect/pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod b/pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod new file mode 100644 index 00000000..9f3a6681 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "dep_pkg" +edition = "v0.8.0" +version = "0.0.1" + diff --git a/pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod.lock b/pkg/client/test_data/add_with_local_path/init/dep_pkg/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_local_path/init/dep_pkg/main.k b/pkg/client/test_data/add_with_local_path/init/dep_pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/init/dep_pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod b/pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod new file mode 100644 index 00000000..6d2aa19c --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "pkg" +edition = "v0.8.0" +version = "0.0.1" + +[dependencies] +dep_pkg = { path = "../dep_pkg" } diff --git a/pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod.lock b/pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod.lock new file mode 100644 index 00000000..0742661b --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/init/pkg/kcl.mod.lock @@ -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" diff --git a/pkg/client/test_data/add_with_local_path/init/pkg/main.k b/pkg/client/test_data/add_with_local_path/init/pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_local_path/init/pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 0509e486..d9c8d965 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -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 } @@ -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, "-", "_") @@ -205,7 +212,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 { @@ -220,6 +227,9 @@ func (dep *Dependency) FillDepInfo() error { dep.Source.Oci.Repo = urlpath } } + if dep.Source.Local != nil { + dep.LocalFullPath = dep.Source.Local.Path + } return nil }