Skip to content

Commit

Permalink
Merge pull request #494 from zong-zhe/fix-load-bug
Browse files Browse the repository at this point in the history
fix: fix missing dependency information load from kcl.mod.lock
  • Loading branch information
Peefy authored Oct 9, 2024
2 parents 1422034 + 29c6475 commit ea2faeb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,28 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) {
return nil, fmt.Errorf("could not load 'kcl.mod' in '%s'\n%w", pkgPath, err)
}
// 3. Sync the dependencies information in kcl.mod.lock with the dependencies in kcl.mod.
for _, name := range modFile.Dependencies.Deps.Keys() {
modDep, ok := modFile.Dependencies.Deps.Get(name)
for _, name := range deps.Deps.Keys() {
lockDep, ok := deps.Deps.Get(name)
if !ok {
return nil, fmt.Errorf("could not load 'kcl.mod' in '%s'\n%w", pkgPath, err)
}
if lockDep, ok := deps.Deps.Get(name); ok {
if modDep, ok := modFile.Dependencies.Deps.Get(name); ok {
lockDep.Source = modDep.Source
lockDep.LocalFullPath = modDep.LocalFullPath
deps.Deps.Set(name, lockDep)
} else {
// If there is no source in the lock file, fill the default oci registry.
if lockDep.Source.IsNilSource() {
lockDep.Source.Registry = &downloader.Registry{
Name: lockDep.Name,
Oci: &downloader.Oci{
Reg: opts.Settings.DefaultOciRegistry(),
Repo: utils.JoinPath(opts.Settings.DefaultOciRepo(), lockDep.Name),
Tag: lockDep.Version,
},
}
}
}
deps.Deps.Set(name, lockDep)
}

return &KclPkg{
Expand Down
18 changes: 18 additions & 0 deletions pkg/package/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"kcl-lang.io/kpm/pkg/opt"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/runner"
"kcl-lang.io/kpm/pkg/settings"
"kcl-lang.io/kpm/pkg/utils"
)

Expand Down Expand Up @@ -137,3 +138,20 @@ func TestLoadKclPkgFromTar(t *testing.T) {
err = os.RemoveAll(filepath.Join(testDir, "kcl1-v0.0.3"))
assert.Equal(t, err, nil)
}

// Test load package whose dependencies in kcl.mod and kcl.mod.lock is different
func TestLoadPkgFromLock(t *testing.T) {
pkgPath := getTestDir("load_from_lock")
kpkg, err := LoadKclPkgWithOpts(
WithPath(pkgPath),
WithSettings(settings.GetSettings()),
)

assert.Equal(t, err, nil)
assert.Equal(t, kpkg.Dependencies.Deps.Len(), 1)
assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Name, "helloworld")
assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).FullName, "helloworld_0.1.2")
assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Registry.Oci.Reg, "ghcr.io")
assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Registry.Oci.Repo, "kcl-lang/helloworld")
assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Registry.Oci.Tag, "0.1.2")
}
4 changes: 4 additions & 0 deletions pkg/package/test_data/load_from_lock/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "load_from_lock"
edition = "v0.10.0"
version = "0.0.1"
5 changes: 5 additions & 0 deletions pkg/package/test_data/load_from_lock/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
1 change: 1 addition & 0 deletions pkg/package/test_data/load_from_lock/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'

0 comments on commit ea2faeb

Please sign in to comment.