diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index 9e5778e9..a8a0eac0 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -12,6 +12,7 @@ import ( "kcl-lang.io/kpm/pkg/errors" "kcl-lang.io/kpm/pkg/oci" "kcl-lang.io/kpm/pkg/opt" + pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" "kcl-lang.io/kpm/pkg/runner" "kcl-lang.io/kpm/pkg/utils" @@ -207,7 +208,10 @@ func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") } - kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath) + kclPkg, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(pkgPath), + pkg.WithSettings(kpmcli.GetSettings()), + ) if err != nil { return nil, err } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 286d8987..804d559b 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1146,7 +1146,6 @@ func testMetadataOffline(t *testing.T) { testDir := getTestDir("test_metadata_offline") kclMod := filepath.Join(testDir, "kcl.mod") uglyKclMod := filepath.Join(testDir, "ugly.kcl.mod") - BeautifulKclMod := filepath.Join(testDir, "beautiful.kcl.mod") uglyContent, err := os.ReadFile(uglyKclMod) assert.Equal(t, err, nil) @@ -1157,8 +1156,6 @@ func testMetadataOffline(t *testing.T) { assert.Equal(t, err, nil) }() - beautifulContent, err := os.ReadFile(BeautifulKclMod) - assert.Equal(t, err, nil) kclPkg, err := pkg.LoadKclPkg(testDir) assert.Equal(t, err, nil) @@ -1178,7 +1175,7 @@ func testMetadataOffline(t *testing.T) { assert.Equal(t, res, "{\"packages\":{}}") content_after_metadata, err = os.ReadFile(kclMod) assert.Equal(t, err, nil) - assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(beautifulContent))) + assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(uglyContent))) } func testAddWithNoSumCheck(t *testing.T) { diff --git a/pkg/client/metadata.go b/pkg/client/metadata.go index 9a5d66d3..3bf2f76b 100644 --- a/pkg/client/metadata.go +++ b/pkg/client/metadata.go @@ -20,6 +20,7 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro _, err = c.Update( WithUpdatedKclPkg(kclPkg), WithOffline(!update), + WithUpdateModFile(false), ) } return err diff --git a/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect index ba6a11b8..391a7e04 100644 --- a/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect +++ b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect @@ -4,4 +4,4 @@ edition = "v0.11.0-alpha.1" version = "0.0.1" [dependencies] -helloworld = "0.1.2" \ No newline at end of file +helloworld = { version = "0.1.2" } \ No newline at end of file diff --git a/pkg/client/update.go b/pkg/client/update.go index c27eeacd..1bc12d69 100644 --- a/pkg/client/update.go +++ b/pkg/client/update.go @@ -3,6 +3,7 @@ package client import ( "encoding/json" "fmt" + "path/filepath" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "kcl-lang.io/kpm/pkg/checker" @@ -20,12 +21,21 @@ import ( // Updating a package means iterating all the dependencies of the package // and updating the dependencies and selecting the version of the dependencies by MVS. type UpdateOptions struct { - kpkg *pkg.KclPkg - offline bool + kpkg *pkg.KclPkg + offline bool + updateModFile bool } type UpdateOption func(*UpdateOptions) error +// WithUpdateModFile sets the flag to update the mod file. +func WithUpdateModFile(updateModFile bool) UpdateOption { + return func(opts *UpdateOptions) error { + opts.updateModFile = updateModFile + return nil + } +} + // WithOffline sets the offline option to update the package. func WithOffline(offline bool) UpdateOption { return func(opts *UpdateOptions) error { @@ -43,7 +53,7 @@ func WithUpdatedKclPkg(kpkg *pkg.KclPkg) UpdateOption { } func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { - opts := &UpdateOptions{} + opts := &UpdateOptions{updateModFile: true} for _, option := range options { if err := option(opts); err != nil { return nil, err @@ -143,8 +153,16 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { return nil, err } - if !opts.offline { - err = kMod.UpdateModAndLockFile() + if opts.updateModFile && utils.DirExists(filepath.Join(kMod.HomePath, constants.KCL_MOD)) { + err = kMod.UpdateModFile() + if err != nil { + return nil, err + } + } + + // Generate file kcl.mod.lock. + if !kMod.NoSumCheck && utils.DirExists(filepath.Join(kMod.HomePath, constants.KCL_MOD)) { + err := kMod.LockDepsVersion() if err != nil { return nil, err } diff --git a/pkg/package/package.go b/pkg/package/package.go index 87a8d949..023124d3 100644 --- a/pkg/package/package.go +++ b/pkg/package/package.go @@ -385,9 +385,8 @@ func (kclPkg *KclPkg) LocalVendorPath() string { return filepath.Join(kclPkg.HomePath, "vendor") } -// updateModAndLockFile will update kcl.mod and kcl.mod.lock -func (kclPkg *KclPkg) UpdateModAndLockFile() error { - +// UpdateModFile will update the kcl.mod file. +func (kclPkg *KclPkg) UpdateModFile() error { // Load kcl.mod SnapShot. depSnapShot := kclPkg.depUI @@ -412,6 +411,16 @@ func (kclPkg *KclPkg) UpdateModAndLockFile() error { return err } + return nil +} + +// updateModAndLockFile will update kcl.mod and kcl.mod.lock +func (kclPkg *KclPkg) UpdateModAndLockFile() error { + err := kclPkg.UpdateModFile() + if err != nil { + return err + } + // Generate file kcl.mod.lock. if !kclPkg.NoSumCheck { err := kclPkg.LockDepsVersion()