Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix missing dependency information load from kcl.mod.lock #494

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!'