fix: automatically regenerate empty completion cache (#2773)
Fixes #2759 The completion cache validation now checks if the file is empty and automatically regenerates it. This prevents shell completions from breaking when the cache file exists but is corrupted or empty. Previously, NeedsUpdate() only checked file existence and age, but didn't validate file contents. Empty cache files would be used as-is, causing 'yay -Pc' to return no output and breaking shell completions. Changes: - Add file size check in NeedsUpdate() to detect empty cache files - Add test case for empty file scenario - Update function documentation
This commit is contained in:
2 files changed
+18
-1
No files matched your search
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// NeedsUpdate checks if the completion cache needs to be regenerated.
|
||||
// Returns true if the file doesn't exist, is older than interval days, or force is true.
|
||||
// Returns true if the file doesn't exist, is older than interval days, is empty, or force is true.
|
||||
func NeedsUpdate(completionPath string, interval int, force bool) bool {
|
||||
if force {
|
||||
return true
|
||||
@@ -25,6 +25,11 @@ func NeedsUpdate(completionPath string, interval int, force bool) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// If the file is empty or invalid, regenerate it
|
||||
if info.Size() == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
if interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -210,6 +210,18 @@ func TestNeedsUpdate(t *testing.T) {
|
||||
force: false,
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
name: "empty file returns true",
|
||||
setupFile: func(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
// Create an empty file
|
||||
err := os.WriteFile(path, []byte(""), 0o600)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
interval: 7,
|
||||
force: false,
|
||||
expectedResult: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in new issue
Block a user