diff --git a/pkg/package/package.go b/pkg/package/package.go index 46e2dafa..94a70a4d 100644 --- a/pkg/package/package.go +++ b/pkg/package/package.go @@ -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{ diff --git a/pkg/package/package_test.go b/pkg/package/package_test.go index d083aeaf..9b7d056c 100644 --- a/pkg/package/package_test.go +++ b/pkg/package/package_test.go @@ -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" ) @@ -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") +} diff --git a/pkg/package/test_data/load_from_lock/kcl.mod b/pkg/package/test_data/load_from_lock/kcl.mod new file mode 100644 index 00000000..ab8fa110 --- /dev/null +++ b/pkg/package/test_data/load_from_lock/kcl.mod @@ -0,0 +1,4 @@ +[package] +name = "load_from_lock" +edition = "v0.10.0" +version = "0.0.1" diff --git a/pkg/package/test_data/load_from_lock/kcl.mod.lock b/pkg/package/test_data/load_from_lock/kcl.mod.lock new file mode 100644 index 00000000..e64da0b4 --- /dev/null +++ b/pkg/package/test_data/load_from_lock/kcl.mod.lock @@ -0,0 +1,5 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" diff --git a/pkg/package/test_data/load_from_lock/main.k b/pkg/package/test_data/load_from_lock/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/package/test_data/load_from_lock/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file