[BUG] Grep count mode shows contradictory output when searching a single file

Resolved 💬 2 comments Opened Jan 13, 2026 by gn00295120 Closed Feb 27, 2026

Summary

When using the Grep tool with output_mode="count" on a single file, the output shows contradictory information - the count is displayed correctly, but the summary line incorrectly shows "0 total occurrences across 0 files".

Environment

  • Product: Claude Code / Cowork
  • Version: Claude for Mac 1.0.3218 (8679c9)
  • Tool: Grep

Steps to Reproduce

  1. Create a test file:
echo -e "Line 1\nLine 2\nLine 3\nLine 4\nLine 5" > /tmp/test.txt
  1. Use Grep with count mode on the single file:
Grep(pattern="Line", path="/tmp/test.txt", output_mode="count")

Actual Output

5

Found 0 total occurrences across 0 files.

Expected Output

5

Found 5 total occurrences across 1 file.

Or alternatively:

/tmp/test.txt:5

Found 5 total occurrences across 1 file.

Additional Context

Searching a directory works correctly:

Grep(pattern="Line", path="/tmp", output_mode="count")

Output:
/tmp/test.txt:5

Found 5 total occurrences across 1 file.  ← Correct!

The issue only occurs when searching a single file directly, not when searching a directory.

Impact

  • User confusion: The output contradicts itself (shows count but says 0 occurrences)
  • Script parsing issues: Automated tools parsing grep output may get incorrect results
  • Trust: Contradictory output may cause users to question tool reliability

Suggested Fix

Update the count aggregation logic to correctly handle single-file searches, ensuring the summary line matches the actual count displayed.

---

Additional test cases that reproduce the issue:

# Test 1: Any pattern
Grep(pattern=".", path="/tmp/test.txt", output_mode="count")
→ Shows count, but "Found 0 total occurrences across 0 files"

# Test 2: Regex pattern  
Grep(pattern="Line [0-9]+", path="/tmp/test.txt", output_mode="count")
→ Shows count, but "Found 0 total occurrences across 0 files"

# Test 3: No matches (works correctly)
Grep(pattern="NONEXISTENT", path="/tmp/test.txt", output_mode="count")
→ "No matches found" (correct behavior)

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗