Conversation
grep read lines into a String, so the first non-UTF-8 file failed read_line with InvalidData, and the `?` in cmd_grep aborted the file loop: the command exited 1 and every later file was never scanned. Read lines as bytes and treat binary input like GNU grep: a NUL in the first buffer marks the file binary, and invalid-UTF-8 lines are matched lossily but never printed. Either way a match reports "grep: FILE: binary file matches" on stderr. Add -a/--text, -I and --binary-files=TYPE. Per-file read errors are reported and grep moves on; BrokenPipe still stops it. Fixes strands-agents#123 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AXaT7zwomYh4CtqSGrCY1Y
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
grepread every line into aString, so the first file that wasn't valid UTF-8 maderead_linefail withInvalidData.cmd_grepthen propagated that with?, which aborted the whole file loop: the command exited 1 withgrep: stream did not contain valid UTF-8, and every file after the offending one (in directory order) was silently never scanned.This change makes
grepbyte-oriented and handles binary input the way GNU grep (3.11, UTF-8 locale) does:read_until(b'\n'), so file content can no longer makegrepfail.grep: FILE: binary file matcheson stderr, with exit status 0, and reading stops after the first match.-c,-l,-Land-qcount binary matches as usual and print no note.-A/-B/-Cwindow, so context output never shows lines from outside the requested range.-a/--text,-I, and--binary-files=binary|text|without-match(a smallBinaryFilesenum).grep: PATH: ERRandgrepmoves on to the next file, the same way open errors are already handled. A closed stdout (BrokenPipe, e.g.grep -r … | head -n 1) still stopsgrepstraight away.greptakes stderr once up front and shares it across files. Previously it calledio::stderr()for each open error, and the second call failed because the writer had already been taken.Before (0.3.3) and after, using the repro from #123 (Node binding,
directread-only bind):Known differences from GNU grep (not addressed here)
BufReaderfill (8 KiB); GNU uses a larger buffer. A NUL that appears later hides only the line it is on.-adecodes invalid bytes lossily (U+FFFD) rather than writing the raw bytes.-oskips matches on binary lines, where GNU prints them.-B/-Clayouts where a hidden binary line splits two groups, the--separators differ slightly from GNU.Out of scope, possible follow-ups
-rvisits files in unsorted directory order, as before.cargo clippy --workspace --all-targets -- -D warningsalready fails onmain(find.rs, lua.rs, ls.rs, exec.rs). This PR adds no new warnings.Related Issues
Fixes #123
Documentation PR
None yet. The command reference page could list the new
-a,-Iand--binary-filesoptions.Type of Change
Bug fix
Testing
17 new integration tests in
tests/shell_integration.rs. They bind a host directory of raw-byte fixtures (\0,\xff) and cover: the [BUG]grep -raborts the whole command (and silently drops matches from other files) when a bound directory contains a non-UTF-8 file #123 repro (recursive search continues past a binary file), invalid UTF-8 lines being hidden,-I/-a/--binary-files,-c/-l/-q, the stdin(standard input)note, context windows around hidden lines, a binary file with no match (exit 1, no note), andBrokenPipestopping after the first file.Ran the [BUG]
grep -raborts the whole command (and silently drops matches from other files) when a bound directory contains a non-UTF-8 file #123 repro end to end against a localnpm run build:debugof the Node addon (output above).Compared against GNU grep 3.11 (
debian:stable-slim,LC_ALL=C.UTF-8) and BSD grep 2.6.0.I ran the relevant test suites for the bindings I touched (
cargo test --workspace --all-targets,pytest tests/python,npm test): Rust core only, no binding surface changed. Rancargo test(all pass) andnpm test(41/41); pytest not run locally.If I touched Rust, I ran
cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
🤖 Generated with Claude Code
https://claude.ai/code/session_01AXaT7zwomYh4CtqSGrCY1Y