fix(grep): distinguish ripgrep exit 1 from a failed search

An errorlint autofix rewrote the ExitError check to match any exit
code, so a search that never ran (bad pattern, unreadable path) was
reported as "no matches found". Keep errors.As, restore the exit-code
check, and add a regression test.
This commit is contained in:
sam committed 2026-07-30 00:14:51 +10:00
1 parent 6108fc5548
commit 52d21d6607
2 files changed
+31 -2

No files matched your search

+7 -2
View File
@@ -212,8 +212,13 @@ func makeGrepExecutor(cwd string, workspaceIndex GrepIndex) Executor {
output = stdout.String()
}
if output == "" && err != nil {
exitErr := &exec.ExitError{}
if errors.As(err, &exitErr) {
// Exit 1 means "no matches"; exit 2 means a real failure (bad
// pattern, unreadable path). Reporting the latter as an empty
// result would have the model state a file contains nothing when
// the search never ran, so the code must be checked, not just the
// error type.
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
return grepBackendResult(Result{Content: "no matches found"}, searchBackend, clamped), nil
}
errMsg := stderr.String()
+24
View File
@@ -32,6 +32,30 @@ func TestGrepUsesIndexCandidatesAsAdvisoryFileSet(t *testing.T) {
}
}
// TestGrepReportsSearchFailureRatherThanNoMatches guards the distinction
// between ripgrep's exit 1 (no matches) and exit 2 (the search never ran). An
// errorlint autofix once rewrote the check to match any *exec.ExitError and
// dropped the exit code, which reported a failed search as an empty result --
// the model would then state that a file contains nothing on the strength of a
// search that never happened. An unterminated group is a parse error, so
// ripgrep exits 2 here.
func TestGrepReportsSearchFailureRatherThanNoMatches(t *testing.T) {
tmp := t.TempDir()
createGrepTestFile(t, tmp, "haystack.txt", "needle\n")
tool := NewGrepTool(tmp, nil)
result, err := tool.Execute(context.Background(), json.RawMessage(`{"pattern":"(unclosed"}`), nil)
if err != nil {
t.Fatalf("execute: %v", err)
}
if !result.IsError {
t.Fatalf("a failed search must be reported as an error, got %#v", result)
}
if strings.Contains(result.Content, "no matches found") {
t.Fatalf("a failed search was reported as an empty result: %s", result.Content)
}
}
func TestGrepRetriesDirectSearchWhenIndexedCandidatesAreStale(t *testing.T) {
tmp := t.TempDir()
createGrepTestFile(t, tmp, "live.txt", "needle\n")