opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(ripgrep): handle malformed JSON lines gracefully in search results

Open edlsh opened this issue 3 weeks ago • 0 comments

Problem

Ripgrep.search crashes with SyntaxError: JSON Parse error when ripgrep outputs malformed JSON lines. This can happen due to:

  • Corrupted output
  • Binary file detection messages
  • Interrupted streams

Currently JSON.parse(line) is called without error handling, causing the entire search to fail on a single bad line.

Solution

Extract JSON parsing into a parseJsonLines helper with try/catch that:

  • Skips malformed JSON lines instead of crashing
  • Logs warnings with context (first 200 chars) for debugging
  • Uses Result.safeParse for schema validation failures
  • Returns only successfully parsed matches

Also skip Ripgrep.search tests when rg binary is unavailable to prevent network downloads during test runs.

Testing

bun test test/file/ripgrep.test.ts

All 8 tests pass:

  • 3 integration tests for Ripgrep.search (skipped if rg unavailable)
  • 5 unit tests for Ripgrep.parseJsonLines (always run)

edlsh avatar Dec 28 '25 02:12 edlsh