Skip to content

Commit 4aff891

Browse files
committed
fix cache check for empty folder
Signed-off-by: Lan <[email protected]>
1 parent f5c7dec commit 4aff891

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

pkg/downloader/downloader.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
292292

293293
localPath := opts.LocalPath
294294
cacheFullPath := opts.CachePath
295-
if ok, err := features.Enabled(features.SupportNewStorage); err == nil && !ok && opts.EnableCache {
295+
if ok, err := features.Enabled(features.SupportNewStorage); err == nil &&
296+
!ok &&
297+
opts.EnableCache &&
298+
utils.DirExists(filepath.Join(localPath, constants.KCL_MOD)) {
296299
if utils.DirExists(cacheFullPath) &&
297300
// If the version in modspec is empty, meanings the latest version is needed.
298301
// The latest version should be requested first and the cache should be updated.

pkg/downloader/downloader_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,40 @@ func testGitDownloader(t *testing.T) {
8686
assert.Equal(t, utils.DirExists(filepath.Join(path_git, "git", "src", gitHash, "kcl.mod")), true)
8787
}
8888

89+
func testDepDownloader(t *testing.T) {
90+
path_git := getTestDir("test_dep_downloader")
91+
if err := os.MkdirAll(path_git, os.ModePerm); err != nil {
92+
t.Fatal(err)
93+
}
94+
95+
defer func() {
96+
_ = os.RemoveAll(path_git)
97+
}()
98+
dep := NewOciDownloader("linux/amd64")
99+
err := dep.Download(&DownloadOptions{
100+
LocalPath: path_git,
101+
CachePath: path_git,
102+
EnableCache: true,
103+
Source: Source{
104+
ModSpec: &ModSpec{
105+
Name: "k8s",
106+
Version: "1.28.1",
107+
},
108+
Oci: &Oci{
109+
Reg: "ghcr.io",
110+
Repo: "kcl-lang/k8s",
111+
},
112+
},
113+
})
114+
assert.Equal(t, err, nil)
115+
existFile, err := utils.Exists(path_git + "/kcl.mod")
116+
assert.Equal(t, err, nil)
117+
assert.Equal(t, existFile, true)
118+
}
119+
120+
// go test -timeout 30s -run ^TestWithGlobalLock$ kcl-lang.io/kpm/pkg/downloader -v
89121
func TestWithGlobalLock(t *testing.T) {
90122
test.RunTestWithGlobalLock(t, "TestOciDownloader", testOciDownloader)
91123
test.RunTestWithGlobalLock(t, "TestGitDownloader", testGitDownloader)
124+
test.RunTestWithGlobalLock(t, "TestDepDownloader", testDepDownloader)
92125
}

pkg/downloader/utils.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,40 +59,41 @@ func FindPackageByModSpec(root string, modSpec *ModSpec) (string, error) {
5959
if err != nil {
6060
return err
6161
}
62-
if info.IsDir() {
63-
kclModPath := filepath.Join(path, constants.KCL_MOD)
64-
if _, err := os.Stat(kclModPath); err == nil {
65-
// If the package name and version are specified,
66-
// we can directly check if the kcl.mod file matches the package.
67-
if matchesPackageSpec(kclModPath, modSpec) {
68-
result = path
69-
return filepath.SkipAll
70-
} else if modSpec.Version == "" {
71-
// If the package name specified, but version are not specified,
72-
if utils.MatchesPackageName(kclModPath, modSpec.Name) {
73-
// load the version from the kcl.mod file
74-
tmpSpec, err := loadModSpecFromKclMod(kclModPath)
75-
if err != nil {
76-
return err
77-
}
78-
// Remember the local path with the highest version
79-
tmpVer, err := version.NewSemver(tmpSpec.Version)
62+
if !info.IsDir() {
63+
return nil
64+
}
65+
kclModPath := filepath.Join(path, constants.KCL_MOD)
66+
if _, err := os.Stat(kclModPath); err == nil {
67+
// If the package name and version are specified,
68+
// we can directly check if the kcl.mod file matches the package.
69+
if matchesPackageSpec(kclModPath, modSpec) {
70+
result = path
71+
return filepath.SkipAll
72+
} else if modSpec.Version == "" {
73+
// If the package name specified, but version are not specified,
74+
if utils.MatchesPackageName(kclModPath, modSpec.Name) {
75+
// load the version from the kcl.mod file
76+
tmpSpec, err := loadModSpecFromKclMod(kclModPath)
77+
if err != nil {
78+
return err
79+
}
80+
// Remember the local path with the highest version
81+
tmpVer, err := version.NewSemver(tmpSpec.Version)
82+
if err != nil {
83+
return err
84+
}
85+
if modVersion != "" {
86+
modVer, err := version.NewSemver(modVersion)
8087
if err != nil {
8188
return err
8289
}
83-
if modVersion != "" {
84-
modVer, err := version.NewSemver(modVersion)
85-
if err != nil {
86-
return err
87-
}
88-
if tmpVer.GreaterThan(modVer) {
89-
modVersion = tmpSpec.Version
90-
result = path
91-
}
92-
} else {
90+
if tmpVer.GreaterThan(modVer) {
9391
modVersion = tmpSpec.Version
9492
result = path
9593
}
94+
} else {
95+
modVersion = tmpSpec.Version
96+
result = path
9697
}
9798
}
9899
}

0 commit comments

Comments
 (0)