fix(cli): treat json v2 truncated-input error as unexpected EOF (Go 1.27) (#301) - #302
Open
chiliec wants to merge 1 commit into
Open
fix(cli): treat json v2 truncated-input error as unexpected EOF (Go 1.27) (#301)#302chiliec wants to merge 1 commit into
chiliec wants to merge 1 commit into
Conversation
….27)
Go 1.27 backs encoding/json with the v2 implementation, which returns a
*json.SyntaxError ("unexpected end of JSON input") from (*Decoder).Token when
the stream is truncated mid-value, where Go 1.26 and earlier returned io.EOF.
The --stream path in cli/stream.go only recognized io.EOF, so on Go 1.27 the
raw v2 error leaked out with a different message and caret column, breaking the
"stream option with unterminated input" test.
Normalize both representations to io.ErrUnexpectedEOF via a small helper that
also accepts a *json.SyntaxError whose offset is at the end of consumed input,
so genuine mid-stream syntax errors are unaffected. Output is now identical on
Go 1.24 through 1.27. Fixes itchyny#301.
BankPansuwan
approved these changes
Aug 24, 2026
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.
Fixes #301.
Problem
Go 1.27 backs
encoding/jsonwith the v2 implementation. Marshal/unmarshal behavior is preserved, but the exact error values differ. Specifically, when(*json.Decoder).Token()hits a stream that is truncated in the middle of a value, Go ≤1.26 returnedio.EOF, while Go 1.27 returns a*json.SyntaxError("unexpected end of JSON input").cli/stream.goonly recognizedio.EOFwhen converting a mid-structure end-of-input intoio.ErrUnexpectedEOF:On Go 1.27 that check is false, so the raw v2
*json.SyntaxErrorleaks up tocli/inputs.go, which takes the*json.SyntaxErrorbranch (offset one column earlier) and prints the v2 message. Thestream option with unterminated inputtest then fails:Fix
Normalize both representations to
io.ErrUnexpectedEOFvia a small helper. It keeps the originalio.EOFcase and additionally accepts a*json.SyntaxErrorwhose offset is at the end of the consumed input (se.Offset >= dec.InputOffset()) — that offset condition is what distinguishes a truncation from a genuine mid-stream syntax error (e.g.[1 2],{"a" bad}), which keep their original error untouched. Because the pre-existingio.ErrUnexpectedEOFpath incli/inputs.goalready seeks to end-of-input for the caret, the rendered position and message are now identical across Go versions.One file changed, no new test needed — the existing
stream option with unterminated inputgolden case is the regression test.Verification
Ran the full suite on both toolchains (
GOTOOLCHAIN=local):Go 1.27.0 (reproduces the bug without the patch):
--- FAIL: TestCliRun/stream_option_with_unterminated_inputok github.com/itchyny/gojq/cli—go test ./...all green.Go 1.24.6 (no regression):
go test ./...→ok github.com/itchyny/gojqandok github.com/itchyny/gojq/cli.gofmt -lclean,go vet ./cli/clean.Also spot-checked the discriminator directly: truncation inputs (
{"a":1,,[1,,{"a":1,[1) all reportSyntaxError.Offset == len(input)on Go 1.27 and are normalized; genuine syntax errors ([1 2],{"a" bad},{"a":@}) haveOffset < lenand pass through unchanged.AI assistance disclosure
This change was prepared with an AI coding assistant; the root-cause analysis, fix, and cross-version verification above were all run against real Go 1.24.6 and Go 1.27.0 toolchains, not generated.