From 3d7701fa357f3cd2b0825f933e16cb0b7990dadd Mon Sep 17 00:00:00 2001 From: Jo Date: Fri, 30 Jan 2026 10:33:43 +0100 Subject: [PATCH] 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 --- pkg/completion/completion.go | 7 ++++++- pkg/completion/completion_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/completion/completion.go b/pkg/completion/completion.go index e8265046..2c5d6ecc 100644 --- a/pkg/completion/completion.go +++ b/pkg/completion/completion.go @@ -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 } diff --git a/pkg/completion/completion_test.go b/pkg/completion/completion_test.go index acb85f07..9f284d27 100644 --- a/pkg/completion/completion_test.go +++ b/pkg/completion/completion_test.go @@ -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 {