Skip to content

Commit

Permalink
Merge pull request #291 from zong-zhe/rm-indirect-deps
Browse files Browse the repository at this point in the history
fix: turned off update indirect dependencies in kcl.mod
  • Loading branch information
Peefy authored Mar 28, 2024
2 parents bede720 + eda9409 commit 7ed45e5
Show file tree
Hide file tree
Showing 27 changed files with 88 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "bbb_0.0.1"
version = "0.0.1"
sum = "/PGJJE6Rp0lAsq3n9RUZ34jPGfwzYIZESs17L6kSG/Y="
path = "../bbb"
84 changes: 57 additions & 27 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ func (c *KpmClient) LoadPkgFromPath(pkgPath string) (*pkg.KclPkg, error) {
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod.lock' in '%s'", pkgPath))
}

// Align the dependencies between kcl.mod and kcl.mod.lock.
for name, dep := range modFile.Dependencies.Deps {
if dep.Local != nil {
if ldep, ok := deps.Deps[name]; ok {
var localFullPath string
if filepath.IsAbs(dep.Local.Path) {
localFullPath = dep.Local.Path
} else {
localFullPath, err = filepath.Abs(filepath.Join(pkgPath, dep.Local.Path))
if err != nil {
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}
}
ldep.LocalFullPath = localFullPath
dep.LocalFullPath = localFullPath
ldep.Source = dep.Source
deps.Deps[name] = ldep
modFile.Dependencies.Deps[name] = dep
}
}
}

return &pkg.KclPkg{
ModFile: *modFile,
HomePath: pkgPath,
Expand Down Expand Up @@ -194,20 +216,24 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
for name, dep := range kclPkg.Dependencies.Deps {
modDep, ok := kclPkg.ModFile.Dependencies.Deps[name]
if !ok || !dep.WithTheSameVersion(modDep) {
reporter.ReportMsgTo(
fmt.Sprintf("removing '%s' with version '%s'", name, dep.Version),
c.logWriter,
)
delete(kclPkg.Dependencies.Deps, name)
}
}
// add the dependencies in kcl.mod which not in kcl.mod.lock
for name, d := range kclPkg.ModFile.Dependencies.Deps {
if _, ok := kclPkg.Dependencies.Deps[name]; !ok {
reporter.ReportMsgTo(
fmt.Sprintf("adding '%s' with version '%s'", name, d.Version),
c.logWriter,
)
if len(d.Version) == 0 {
reporter.ReportMsgTo(
fmt.Sprintf("adding '%s'", name),
c.logWriter,
)
} else {
reporter.ReportMsgTo(
fmt.Sprintf("adding '%s' with version '%s'", name, d.Version),
c.logWriter,
)
}

kclPkg.Dependencies.Deps[name] = d
}
}
Expand Down Expand Up @@ -277,10 +303,12 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
}
}
if update {
// update the kcl.mod and kcl.mod.lock.
err := kclPkg.UpdateModAndLockFile()
if err != nil {
return err
// Generate file kcl.mod.lock.
if !kclPkg.NoSumCheck {
err := kclPkg.LockDepsVersion()
if err != nil {
return err
}
}
}
return nil
Expand All @@ -293,9 +321,12 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error {
return err
}

err = kclPkg.UpdateModAndLockFile()
if err != nil {
return err
// Generate file kcl.mod.lock.
if !kclPkg.NoSumCheck {
err := kclPkg.LockDepsVersion()
if err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -351,7 +382,7 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis

c.noSumCheck = opts.NoSumCheck()

kclPkg, err := pkg.LoadKclPkg(pkgPath)
kclPkg, err := c.LoadPkgFromPath(pkgPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -818,11 +849,6 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
return nil, err
}
dep.FromKclPkg(kpkg)
abspath, err := filepath.Abs(dep.GetLocalFullPath(homePath))
if err != nil {
return nil, err
}
dep.Source.Local.Path = abspath
}

var err error
Expand Down Expand Up @@ -1007,7 +1033,7 @@ func (c *KpmClient) DownloadFromOci(dep *pkg.Oci, localPath string) (string, err
return "", reporter.NewErrorEvent(
reporter.InvalidKclPkg,
err,
fmt.Sprintf("failed to find the kcl package tar from '%s'.", localPath),
fmt.Sprintf("failed to find the kcl package from '%s'.", localPath),
)
}
}
Expand All @@ -1022,7 +1048,7 @@ func (c *KpmClient) DownloadFromOci(dep *pkg.Oci, localPath string) (string, err
return "", reporter.NewErrorEvent(
reporter.FailedUntarKclPkg,
err,
fmt.Sprintf("failed to untar the kcl package tar from '%s' into '%s'.", tarPath, localPath),
fmt.Sprintf("failed to untar the kcl package from '%s' into '%s'.", tarPath, localPath),
)
}

Expand Down Expand Up @@ -1311,18 +1337,22 @@ func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencie

// Recursively download the dependencies of the new dependencies.
for _, d := range newDepsCopy {
// Load kcl.mod file of the new downloaded dependencies.
deppkg, err := pkg.LoadKclPkg(filepath.Join(c.homePath, d.FullName))
var err error
var deppkg *pkg.KclPkg
if len(d.LocalFullPath) != 0 {
deppkg, err = pkg.LoadKclPkg(d.LocalFullPath)
}
deppkg, err = c.LoadPkgFromPath(d.LocalFullPath)
} else {
// Load kcl.mod file of the new downloaded dependencies.
deppkg, err = c.LoadPkgFromPath(filepath.Join(c.homePath, d.FullName))

}
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}

source := fmt.Sprintf("%s@%s", d.Name, d.Version)
source = strings.TrimRight(source, "@")
err = depGraph.AddVertex(source)
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,4 +1364,6 @@ func TestAddWithLocalPath(t *testing.T) {
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)
assert.Equal(t, gotpkg.Dependencies.Deps["dep_pkg"].LocalFullPath, filepath.Join(tmppath, "dep_pkg"))
assert.Equal(t, gotpkg.Dependencies.Deps["dep_pkg"].Source.Local.Path, "../dep_pkg")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "dep_pkg_0.0.1"
version = "0.0.1"
sum = "C3TjaZJVdt4G8TtbiCxTUO/zAlf7fWJTAvs/CDMnlxY="
path = "../dep_pkg"
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
name = "bbb"
full_name = "bbb_"
sum = "Cgx5yepaNrS3/p4fK04RQWf1dEpG3j49ntFqzrJJsxU="
path = "../bbb"
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
full_name = "aaa_0.0.1"
version = "0.0.1"
sum = "hgAM5wYFzXo83S+OiT0LaBDnHU7Q9vqq3BPXlTDr8TY="
path = "../aaa"

2 changes: 1 addition & 1 deletion pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 The KCL Authors. All rights reserved.
// Deprecated: The entire contents of this file will be deprecated.
// Deprecated: The entire contents of this file will be deprecated.
// Please use the kcl cli - https://github.com/kcl-lang/cli.

package cmd
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 The KCL Authors. All rights reserved.
// Deprecated: The entire contents of this file will be deprecated.
// Deprecated: The entire contents of this file will be deprecated.
// Please use the kcl cli - https://github.com/kcl-lang/cli.

package cmd
Expand Down Expand Up @@ -95,8 +95,6 @@ func KpmRun(c *cli.Context, kpmcli *client.KpmClient) error {
return err
}

kpmcli.SetNoSumCheck(c.Bool(FLAG_NO_SUM_CHECK))

defer func() {
// release the lock of the package cache after the function returns.
releaseErr := kpmcli.ReleasePackageCacheLock()
Expand All @@ -106,6 +104,7 @@ func KpmRun(c *cli.Context, kpmcli *client.KpmClient) error {
}()

kclOpts := CompileOptionFromCli(c)
kclOpts.SetNoSumCheck(c.Bool(FLAG_NO_SUM_CHECK))
runEntry, errEvent := runner.FindRunEntryFrom(c.Args().Slice())
if errEvent != nil {
return errEvent
Expand Down
4 changes: 2 additions & 2 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (d Dependency) WithTheSameVersion(other Dependency) bool {

// GetLocalFullPath will get the local path of a dependency.
func (dep *Dependency) GetLocalFullPath(rootpath string) string {
if dep.IsFromLocal() {
if len(dep.LocalFullPath) == 0 && dep.IsFromLocal() {
if filepath.IsAbs(dep.Source.Local.Path) {
return dep.Source.Local.Path
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (dep *Dependency) GenDepFullName() string {
type Source struct {
*Git
*Oci
*Local
*Local `toml:"-"`
}

type Local struct {
Expand Down
19 changes: 18 additions & 1 deletion pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ func LoadKclPkg(pkgPath string) (*KclPkg, error) {
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod.lock' in '%s'", pkgPath))
}

// Align the dependencies between kcl.mod and kcl.mod.lock.
for name, dep := range modFile.Dependencies.Deps {
if dep.Local != nil {
if ldep, ok := deps.Deps[name]; ok {
abs, err := filepath.Abs(filepath.Join(pkgPath, dep.Local.Path))
if err != nil {
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}
ldep.LocalFullPath = abs
dep.LocalFullPath = abs
ldep.Source = dep.Source
deps.Deps[name] = ldep
modFile.Dependencies.Deps[name] = dep
}
}
}

return &KclPkg{
ModFile: *modFile,
HomePath: pkgPath,
Expand Down Expand Up @@ -165,7 +182,7 @@ func (kclPkg *KclPkg) UpdateModAndLockFile() error {

// Generate file kcl.mod.lock.
if !kclPkg.NoSumCheck {
err = kclPkg.LockDepsVersion()
err := kclPkg.LockDepsVersion()
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
adding 'k8s' with version '1.27'
downloading 'test/k8s:1.27' from 'localhost:5001/test/k8s:1.27'
apiVersion: v1
kind: Pod
metadata:
name: web-app
spec:
containers:
- image: nginx
name: main-container
ports:
- containerPort: 80
adding 'helloworld' with version '0.1.1'
downloading 'test/helloworld:0.1.1' from 'localhost:5001/test/helloworld:0.1.1'
a: Hello World!
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
removing 'k8s' with version '1.27'
adding 'k8s' with version '1.14'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
full_name = "a_kcl_pkg_dep_pkg_name_0.0.1"
version = "0.0.1"
sum = "CzqJerovhD4TlBCWUTU9ceUlJhNPkhar+HURpeu48NE="
path = "../a_kcl_pkg_dep_one_pkg_3"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.27"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
full_name = "a_kcl_pkg_dep_pkg_name_0.0.1"
version = "0.0.1"
sum = "AfdOtDGXfOoJvpmJ4uQ/YllHoP2wAzbtQQeXo4s4C/U="
path = "../a_kcl_pkg_dep_one_pkg_4"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.27"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "dep-with-line_0.0.1"
version = "0.0.1"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "./dep-with-line"
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
name = "dep-with-line"
full_name = "dep-with-line_"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "./dep-with-line"
[dependencies.dep_with-line]
name = "dep_with-line"
full_name = "dep_with-line_"
sum = "hiIX1252Xn0QvmGlLhuM5ZsuuV8EpY/fuCgcRKLk3iE="
path = "./dep_with-line"
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
name = "dep-with-line"
full_name = "dep-with-line_"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "./dep-with-line"
[dependencies.dep_with-line]
name = "dep_with-line"
full_name = "dep_with-line_"
sum = "hiIX1252Xn0QvmGlLhuM5ZsuuV8EpY/fuCgcRKLk3iE="
path = "./dep_with-line"
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
full_name = "pkg2_0.0.1"
version = "0.0.1"
sum = "JbfIAXPJb3L6xX7hi/A5mrXzjpB8eFoKfEmJMdHsewY="
path = "../pkg2"
[dependencies.pkg3]
name = "pkg3"
full_name = "pkg3_0.0.1"
version = "0.0.1"
sum = "T29gAv6K/tLithhP5jVHyurV5zRFui+i1ulyMt/ncnM="
path = "../pkg3"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "pkg3_0.0.1"
version = "0.0.1"
sum = "T29gAv6K/tLithhP5jVHyurV5zRFui+i1ulyMt/ncnM="
path = "../pkg3"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "pkg3_0.0.1"
version = "0.0.1"
sum = "T29gAv6K/tLithhP5jVHyurV5zRFui+i1ulyMt/ncnM="
path = "../pkg3"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "pkg2_0.0.1"
version = "0.0.1"
sum = "JbfIAXPJb3L6xX7hi/A5mrXzjpB8eFoKfEmJMdHsewY="
path = "../pkg/pkg2"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "dep-with-line-0.0.1"
version = "0.0.1"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "../dep-with-line"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
full_name = "kcl2_0.0.1"
version = "0.0.1"
sum = "Q+ilCTK5EXPn7stwrtY2La1MQdP+wiAq5wHiG91NoB8="
path = "../kcl2"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.27"
helloworld = "0.1.1"
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import k8s.api.core.v1 as k8core
import helloworld

k8core.Pod {
metadata.name = "web-app"
spec.containers = [{
name = "main-container"
image = "nginx"
ports = [{containerPort = 80}]
}]
}
a = helloworld.The_first_kcl_program
Loading

0 comments on commit 7ed45e5

Please sign in to comment.