Skip to content

Commit

Permalink
Merge pull request #226 from zong-zhe/support-no-lockfile
Browse files Browse the repository at this point in the history
feat: add flag '--no_sum_check' to support no kcl.mod.lock generation
  • Loading branch information
Peefy authored Dec 14, 2023
2 parents 915c433 + a085461 commit 626632d
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 21 deletions.
4 changes: 3 additions & 1 deletion pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func getAbsInputPath(pkgPath string, inputPath string) (string, error) {
// RunPkgWithOpt will compile the kcl package with the compile options.
func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -140,6 +141,7 @@ func RunTarPkg(tarPath string, opts *opt.CompileOptions) (*kcl.KCLResultList, er

opts.SetPkgPath(destDir)
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
if err != nil {
return nil, err
}
Expand All @@ -157,7 +159,7 @@ func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResult
if err != nil {
return nil, err
}

kpmcli.SetNoSumCheck(opts.NoSumCheck())
ociOpts, err := kpmcli.ParseOciOptionFromString(ociRef, version)

if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,23 @@ func TestRunTarPkg(t *testing.T) {
os.RemoveAll(untarPath)
}
}

func TestRunWithNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_run_with_nosumcheck")
opts := opt.DefaultCompileOptions()
opts.SetPkgPath(pkgPath)
opts.SetNoSumCheck(true)
_, err := RunPkgInPath(opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)

opts = opt.DefaultCompileOptions()
opts.SetPkgPath(pkgPath)
opts.SetNoSumCheck(false)
_, err = RunPkgInPath(opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)
defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()
}
7 changes: 7 additions & 0 deletions pkg/api/test_data/test_run_with_nosumcheck/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "aaa"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.27"
1 change: 1 addition & 0 deletions pkg/api/test_data/test_run_with_nosumcheck/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
60 changes: 49 additions & 11 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type KpmClient struct {
homePath string
// The settings of kpm loaded from the global configuration file.
settings settings.Settings
// The flag of whether to check the checksum of the package and update kcl.mod.lock.
noSumCheck bool
}

// NewKpmClient will create a new kpm client with default settings.
Expand All @@ -56,6 +58,16 @@ func NewKpmClient() (*KpmClient, error) {
}, nil
}

// SetNoSumCheck will set the 'noSumCheck' flag.
func (c *KpmClient) SetNoSumCheck(noSumCheck bool) {
c.noSumCheck = noSumCheck
}

// GetNoSumCheck will return the 'noSumCheck' flag.
func (c *KpmClient) GetNoSumCheck() bool {
return c.noSumCheck
}

func (c *KpmClient) SetLogWriter(writer io.Writer) {
c.logWriter = writer
}
Expand Down Expand Up @@ -148,6 +160,8 @@ func (c *KpmClient) ResolveDepsIntoMap(kclPkg *pkg.KclPkg) (map[string]string, e
// If the package does not exist, it will re-download to the local.
func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) error {
var searchPath string
kclPkg.NoSumCheck = c.noSumCheck

if kclPkg.IsVendorMode() {
// In the vendor mode, the search path is the vendor subdirectory of the current package.
err := c.VendorDeps(kclPkg)
Expand Down Expand Up @@ -193,7 +207,7 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
kclPkg.Dependencies.Deps[name] = d
}
} else {
if utils.DirExists(searchFullPath) && utils.CheckPackageSum(d.Sum, searchFullPath) {
if utils.DirExists(searchFullPath) && (c.GetNoSumCheck() || utils.CheckPackageSum(d.Sum, searchFullPath)) {
// Find it and update the local path of the dependency.
d.LocalFullPath = searchFullPath
kclPkg.Dependencies.Deps[name] = d
Expand Down Expand Up @@ -299,6 +313,8 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}

c.noSumCheck = opts.NoSumCheck()

kclPkg, err := pkg.LoadKclPkg(pkgPath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -483,6 +499,9 @@ func (c *KpmClient) InitEmptyPkg(kclPkg *pkg.KclPkg) error {

// AddDepWithOpts will add a dependency to the current kcl package.
func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pkg.KclPkg, error) {
c.noSumCheck = opt.NoSumCheck
kclPkg.NoSumCheck = opt.NoSumCheck

// 1. get the name and version of the repository from the input arguments.
d, err := pkg.ParseOpt(&opt.RegistryOpts)
if err != nil {
Expand Down Expand Up @@ -1006,6 +1025,29 @@ func (c *KpmClient) ParseOciOptionFromString(oci string, tag string) (*opt.OciOp
return ociOpt, nil
}

// dependencyExists will check whether the dependency exists in the local filesystem.
func (c *KpmClient) dependencyExists(dep *pkg.Dependency, lockDeps *pkg.Dependencies) *pkg.Dependency {

// If the flag '--no_sum_check' is set, skip the checksum check.
if c.noSumCheck {
// If the dependent package does exist locally
if utils.DirExists(filepath.Join(c.homePath, dep.FullName)) {
return dep
}
}

lockDep, present := lockDeps.Deps[dep.Name]
// Check if the sum of this dependency in kcl.mod.lock has been chanaged.
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 nil
}

// downloadDeps will download all the dependencies of the current kcl package.
func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencies) (*pkg.Dependencies, error) {
newDeps := pkg.Dependencies{
Expand All @@ -1018,16 +1060,12 @@ func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencie
return nil, errors.InvalidDependency
}

lockDep, present := lockDeps.Deps[d.Name]

// Check if the sum of this dependency in kcl.mod.lock has been chanaged.
if present {
// If the dependent package does not exist locally, then method 'check' will return false.
if check(lockDep, filepath.Join(c.homePath, d.FullName)) {
newDeps.Deps[d.Name] = lockDep
continue
}
existDep := c.dependencyExists(&d, &lockDeps)
if existDep != nil {
newDeps.Deps[d.Name] = *existDep
continue
}

expectedSum := lockDeps.Deps[d.Name].Sum
// Clean the cache
if len(c.homePath) == 0 || len(d.FullName) == 0 {
Expand All @@ -1044,7 +1082,7 @@ func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencie
}

if !lockedDep.IsFromLocal() {
if expectedSum != "" && lockedDep.Sum != expectedSum && lockDep.FullName == d.FullName {
if !c.noSumCheck && expectedSum != "" && lockedDep.Sum != expectedSum && existDep.FullName == d.FullName {
return nil, reporter.NewErrorEvent(
reporter.CheckSumMismatch,
errors.CheckSumMismatchError,
Expand Down
90 changes: 90 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,93 @@ func TestMetadataOffline(t *testing.T) {
assert.Equal(t, err, nil)
assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(beautifulContent)))
}

func TestAddWithNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_add_no_sum_check")
err := copy.Copy(filepath.Join(pkgPath, "kcl.mod.bak"), filepath.Join(pkgPath, "kcl.mod"))
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang",
PkgName: "helloworld",
Tag: "0.1.0",
},
},
NoSumCheck: true,
}

_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)

opts.NoSumCheck = false
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)
defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod"))
}()
}

func TestRunWithNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_run_no_sum_check")

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

opts := opt.DefaultCompileOptions()
opts.SetNoSumCheck(true)
opts.SetPkgPath(pkgPath)

_, err = kpmcli.CompileWithOpts(opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)

opts = opt.DefaultCompileOptions()
opts.SetPkgPath(pkgPath)
opts.SetNoSumCheck(false)
_, err = kpmcli.CompileWithOpts(opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)

defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()
}

func TestUpdateWithNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_update_no_sum_check")

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

kpmcli.SetNoSumCheck(true)
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)

err = kpmcli.UpdateDeps(kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)

kpmcli.SetNoSumCheck(false)
kclPkg, err = kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)

err = kpmcli.UpdateDeps(kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)

defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()
}
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_add_no_sum_check/kcl.mod.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_add_no_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
1 change: 1 addition & 0 deletions pkg/client/test_data/test_add_no_sum_check/main.k
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/test_run_no_sum_check/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_run_no_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
1 change: 1 addition & 0 deletions pkg/client/test_data/test_run_no_sum_check/main.k
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/test_update_no_sum_check/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_run_no_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
1 change: 1 addition & 0 deletions pkg/client/test_data/test_update_no_sum_check/main.k
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/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func NewAddCmd(kpmcli *client.KpmClient) *cli.Command {
Name: "tag",
Usage: "Git repository tag",
},
&cli.BoolFlag{
Name: FLAG_NO_SUM_CHECK,
Usage: "do not check the checksum of the package and update kcl.mod.lock",
},
},

Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -120,6 +124,7 @@ func onlyOnceOption(c *cli.Context, name string) (*string, *reporter.KpmEvent) {

// parseAddOptions will parse the user cli inputs.
func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) (*opt.AddOptions, error) {
noSumCheck := c.Bool(FLAG_NO_SUM_CHECK)
// parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'.
if c.NArg() == 0 {
gitOpts, err := parseGitRegistryOptions(c)
Expand All @@ -132,6 +137,7 @@ func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string)
return &opt.AddOptions{
LocalPath: localPath,
RegistryOpts: *gitOpts,
NoSumCheck: noSumCheck,
}, nil
} else {
localPkg, err := parseLocalPathOptions(c)
Expand All @@ -144,11 +150,13 @@ func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string)
return &opt.AddOptions{
LocalPath: localPath,
RegistryOpts: *ociReg,
NoSumCheck: noSumCheck,
}, nil
} else {
return &opt.AddOptions{
LocalPath: localPath,
RegistryOpts: *localPkg,
NoSumCheck: noSumCheck,
}, nil
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/cmd_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ const FLAG_OVERRIDES = "overrides"
const FLAG_SORT_KEYS = "sort_keys"

const FLAG_QUIET = "quiet"
const FLAG_NO_SUM_CHECK = "no_sum_check"
7 changes: 7 additions & 0 deletions pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func NewRunCmd(kpmcli *client.KpmClient) *cli.Command {
Name: FLAG_VENDOR,
Usage: "run in vendor mode",
},
// --no_sum_check
&cli.BoolFlag{
Name: FLAG_NO_SUM_CHECK,
Usage: "do not check the checksum of the package and update kcl.mod.lock",
},

// KCL arg: --setting, -Y
&cli.StringSliceFlag{
Expand Down Expand Up @@ -87,6 +92,8 @@ 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 Down
Loading

0 comments on commit 626632d

Please sign in to comment.