Skip to content

Commit

Permalink
Merge pull request #437 from zong-zhe/rm-kclmod-gen
Browse files Browse the repository at this point in the history
fix: rm the generation of kcl.mod when compile virtual package
  • Loading branch information
Peefy authored Aug 9, 2024
2 parents a110766 + 9d27b18 commit ecbd431
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions pkg/client/test_data/test_virtual_pkg_visitor/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
44 changes: 5 additions & 39 deletions pkg/client/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,14 @@ 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(vKclModPath) {
err := os.RemoveAll(vKclModPath)
if err != nil {
return err
}
}
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,
}

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.
Expand Down

0 comments on commit ecbd431

Please sign in to comment.