Skip to content

Commit

Permalink
fix: fix missing version when add dependencies from local path
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Mar 27, 2024
1 parent ed2f17c commit 2847a4a
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 18 deletions.
29 changes: 16 additions & 13 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -788,24 +788,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 +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
}
Expand All @@ -1226,15 +1228,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 +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
}
Expand Down Expand Up @@ -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
}
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 aaa@0.0.1 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!'
14 changes: 12 additions & 2 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 Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 2847a4a

Please sign in to comment.