From bbd3a1c6bf38c12f6e7a761c937349dc4f047dbc Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 7 Aug 2024 18:12:26 +0800 Subject: [PATCH 1/2] fix: rm the generation of kcl.mod when compile virtual package Signed-off-by: zongz --- pkg/client/visitor.go | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/pkg/client/visitor.go b/pkg/client/visitor.go index b042eb01..6e4a5444 100644 --- a/pkg/client/visitor.go +++ b/pkg/client/visitor.go @@ -83,12 +83,6 @@ func (vpv *VirtualPkgVisitor) Visit(s *downloader.Source, v visitFunc) error { // After the visitFunc is executed, clean the virtual kcl.mod file. defer func() error { vKclModLockPath := filepath.Join(sourcePath, constants.KCL_MOD_LOCK) - if utils.DirExists(vKclModPath) { - err := os.RemoveAll(vKclModPath) - if err != nil { - return err - } - } if utils.DirExists(vKclModLockPath) { err := os.RemoveAll(vKclModLockPath) if err != nil { @@ -97,28 +91,16 @@ func (vpv *VirtualPkgVisitor) Visit(s *downloader.Source, v visitFunc) error { } return nil }() - initOpts := opt.InitOptions{ - Name: "vPkg_" + uuid.New().String(), - InitPath: sourcePath, - } - modfile := pkg.NewModFile(&initOpts) - logWriter := vpv.kpmcli.GetLogWriter() - vpv.kpmcli.SetLogWriter(nil) - err = vpv.kpmcli.createIfNotExist(modfile.GetModFilePath(), modfile.StoreModFile) - if err != nil { - return err - } - vpv.kpmcli.SetLogWriter(logWriter) } - - kclPkg, err := vpv.kpmcli.LoadPkgFromPath(sourcePath) - if err != nil { - return err + initOpts := opt.InitOptions{ + Name: "vPkg_" + uuid.New().String(), + InitPath: sourcePath, } + kpkg := pkg.NewKclPkg(&initOpts) // If the required files are present, proceed with the visitFunc - return v(kclPkg) + return v(&kpkg) } // RemoteVisitor is the visitor for visiting a remote package. From 9d27b180d1d1e064ad7e3dc7abe64688ac56910f Mon Sep 17 00:00:00 2001 From: zongz Date: Fri, 9 Aug 2024 14:56:40 +0800 Subject: [PATCH 2/2] fix: rm kcl.mod.lock gen when there is no dependencies in kcl.mod Signed-off-by: zongz --- pkg/client/client.go | 2 +- pkg/client/client_test.go | 24 +++++++++++++++++++ .../test_data/test_virtual_pkg_visitor/main.k | 1 + pkg/client/visitor.go | 16 ------------- 4 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 pkg/client/test_data/test_virtual_pkg_visitor/main.k diff --git a/pkg/client/client.go b/pkg/client/client.go index 631ffb2d..ea3bd0fd 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -414,7 +414,7 @@ func (c *KpmClient) resolvePkgDeps(kclPkg *pkg.KclPkg, lockDeps *pkg.Dependencie } // Generate file kcl.mod.lock. - if !kclPkg.NoSumCheck || !update { + if kclPkg.ModFile.Dependencies.Deps.Len() > 0 && !kclPkg.NoSumCheck || !update { err := kclPkg.LockDepsVersion() if err != nil { return err diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index be34c35e..97799b9e 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1965,3 +1965,27 @@ func TestRunWithLogger(t *testing.T) { assert.Equal(t, err, nil) assert.Equal(t, logbuf.String(), "Hello, World!\n") } + +func TestVirtualPackageVisiter(t *testing.T) { + pkgPath := getTestDir("test_virtual_pkg_visitor") + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + pkgSource, err := downloader.NewSourceFromStr(pkgPath) + assert.Equal(t, err, nil) + + v := NewVisitor(*pkgSource, kpmcli) + err = v.Visit(pkgSource, func(p *pkg.KclPkg) error { + assert.Contains(t, p.GetPkgName(), "vPkg_") + _, err = os.Stat(filepath.Join(pkgPath, "kcl.mod")) + assert.Equal(t, os.IsNotExist(err), true) + _, err = os.Stat(filepath.Join(pkgPath, "kcl.mod.lock")) + assert.Equal(t, os.IsNotExist(err), true) + return nil + }) + assert.Equal(t, err, nil) + _, err = os.Stat(filepath.Join(pkgPath, "kcl.mod")) + assert.Equal(t, os.IsNotExist(err), true) + _, err = os.Stat(filepath.Join(pkgPath, "kcl.mod.lock")) + assert.Equal(t, os.IsNotExist(err), true) +} diff --git a/pkg/client/test_data/test_virtual_pkg_visitor/main.k b/pkg/client/test_data/test_virtual_pkg_visitor/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_virtual_pkg_visitor/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/visitor.go b/pkg/client/visitor.go index 6e4a5444..91772cc7 100644 --- a/pkg/client/visitor.go +++ b/pkg/client/visitor.go @@ -77,22 +77,6 @@ func (vpv *VirtualPkgVisitor) Visit(s *downloader.Source, v visitFunc) error { return err } - // If the source path does not contain a kcl.mod file, create a virtual kcl.mod file. - vKclModPath := filepath.Join(sourcePath, constants.KCL_MOD) - if !utils.DirExists(vKclModPath) { - // After the visitFunc is executed, clean the virtual kcl.mod file. - defer func() error { - vKclModLockPath := filepath.Join(sourcePath, constants.KCL_MOD_LOCK) - if utils.DirExists(vKclModLockPath) { - err := os.RemoveAll(vKclModLockPath) - if err != nil { - return err - } - } - return nil - }() - - } initOpts := opt.InitOptions{ Name: "vPkg_" + uuid.New().String(), InitPath: sourcePath,