escargs: report a scan error instead of silently truncating - #95
Open
youdie006 wants to merge 1 commit into
Open
escargs: report a scan error instead of silently truncating#95youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
bufio.Scanner stops and sets an error on an item at or above bufio.MaxScanTokenSize (64 KiB). escargs never read scanner.Err(), so such an input made it print the items it had already consumed and exit 0. When the oversized item is the first one, the whole output is dropped and the exit status still says success. Extract the scan loop into escape() so it can return the error, and let main() report it through the existing log.Fatal path, as the -a open error already does.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 5 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
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.
escargsnever readsscanner.Err(), so a scan error looks like end of input.bufio.Scannerstops and recordsbufio.ErrTooLongon an item at or abovebufio.MaxScanTokenSize(64 KiB). The loop inmainends, the process exits 0, and theitems already written are the whole output. When the oversized item comes first, nothing
is written at all:
xargs, whichescargsis modelled on, fails loudly on the same shape:This matters more here than in a typical filter: the output is meant to be substituted into
a shell command line, so a silently shortened argument list is a command that runs with the
wrong arguments and a success status.
The repo already treats this as an error everywhere else
fuzz_test.go:248raises the cap withscanner.Buffer, andfuzz_test.go:256treatsscanner.Err() != nilast.Fatalf- the test harness knows about both the 64 KiB limitand the error.
cmd/escargs/escargs.goalready routes its other error source,os.Openfor-a,through
log.Fatal..agents/rules/code-style.md, under Error Handling: "Never ignore returned errorssilently unless explicitly documented", and CLI errors go through
log.Fatalwith theescargs:prefix - which is what this change does.The change
mainis not testable as it stands, so the scan loop moves intoescape(w io.Writer, r io.Reader) error, which ends withreturn scanner.Err().mainreports it through theexisting
log.Fatalpath. Reading and writing behaviour is otherwise unchanged - the writeskeep the same
_, _ =treatment used byusage().Normal input is byte-identical before and after.
I did not raise the scanner buffer. Failing loudly matches
xargsand keeps the memorypolicy your call; a
scanner.Bufferbump can follow separately if you want oversized itemsto pass through.
Test
cmd/escargs/escargs_test.gois new - table-driven witht.Parallel(), per.agents/rules/testing-benchmarks.md. Reverting onlyreturn scanner.Err()toreturn nil:go test -race ./...passes on both packages,go vetandgofmtare clean.One incidental change
.gitignoreline 28 wasescargs, which has no slash and so matches thecmd/escargsdirectory, not just the built binary - any new file added there is ignored. Anchoring it
to
/escargskeeps the root binary ignored (make escargsandmake cleanboth use theroot path) and lets the test file be tracked.
Disclosure: this was found and prepared with AI assistance (Claude). The failing input, the
red-green check, and the full
go test -race ./...run were executed locally.