diff --git a/internal/tools/builtin/find.go b/internal/tools/builtin/find.go index e5de98bb5..fd7d3cc4f 100644 --- a/internal/tools/builtin/find.go +++ b/internal/tools/builtin/find.go @@ -13,7 +13,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "os" "os/exec" @@ -239,9 +238,12 @@ func runFindGoFallback(ctx context.Context, cwd, searchPath string, p FindParams return nil }) - if err != nil && !errors.Is(err, ctx.Err()) { + if err != nil { return Result{Content: fmt.Sprintf("find error: %v", err), IsError: true}, nil } + if ctxErr := ctx.Err(); ctxErr != nil { + return Result{Content: fmt.Sprintf("find error: search did not complete: %v", ctxErr), IsError: true}, nil + } if len(matches) == 0 { return Result{Content: "no matches found"}, nil diff --git a/internal/tools/builtin/find_test.go b/internal/tools/builtin/find_test.go index 62df32843..370562b30 100644 --- a/internal/tools/builtin/find_test.go +++ b/internal/tools/builtin/find_test.go @@ -136,6 +136,31 @@ func TestFindFallback_NotADirectory(t *testing.T) { } } +// A cancelled or timed-out walk must be reported as an error, not silently +// returned as "no matches found" (or as whatever partial results happened to +// accumulate before cancellation). The WalkDir callback swallows every +// per-entry error and returns nil, so the only error that can ever reach the +// caller is ctx.Err() from the cancellation select -- the old guard compared +// that against itself and never fired. +func TestFindFallback_CancelledWalkReportsError(t *testing.T) { + tmp := t.TempDir() + createTestFile(t, tmp, "foo.go", "package main") + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + res, err := runFindGoFallback(ctx, tmp, tmp, FindParams{}) + if err != nil { + t.Fatalf("runFindGoFallback returned an error: %v", err) + } + if !res.IsError { + t.Fatalf("expected an error result for a cancelled walk, got: %q", res.Content) + } + if res.Content == "no matches found" { + t.Fatal("cancelled walk reported as 'no matches found': the search never finished") + } +} + func createTestFile(t *testing.T, base, relPath, content string) { t.Helper() full := filepath.Join(base, relPath)