fix(migrate): accept trailing commas in jsonc input (#276)#280
Merged
BartWaardenburg merged 2 commits intofallow-rs:mainfrom May 5, 2026
Merged
Conversation
Closes fallow-rs#276. real-world JSONC files (knip.jsonc, tsconfig.json, .vscode/settings.json, etc.) routinely use trailing commas. `load_json_or_jsonc` ran the input through `json_comments::StripComments` and then handed the result to serde_json, which rejects trailing commas with "trailing comma at line N column M". Add a final pass that drops `,` immediately before `}` or `]` while leaving commas inside string literals untouched. The pass runs only when the comment-stripped parse fails, so already-valid input has zero overhead. The new `strip_trailing_commas` helper is a small byte-level state machine (no extra deps) that tracks in-string + escaped state, so strings like "hello, world," survive verbatim.
Collaborator
|
Merged in 47306d3. Thanks @ChrisJr404. |
Collaborator
|
Released in v2.65.0. Thanks @ChrisJr404. |
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.
Closes #276.
Summary
Real-world JSONC files (knip.jsonc, tsconfig.json, .vscode/settings.json, etc.) routinely use trailing commas.
load_json_or_jsoncran the input throughjson_comments::StripCommentsand then handed the result to serde_json, which rejects trailing commas withtrailing comma at line N column M— exactly the symptom in the issue.Fix
Add a final pass that drops
,immediately before}or]while leaving commas inside string literals untouched. The pass runs only when the comment-stripped parse fails, so already-valid input pays zero cost on the happy path:serde_json::from_str— works for normal JSON.json_comments::StripComments, retry parse.strip_trailing_commason the comment-stripped text and retry.strip_trailing_commasis a small byte-level state machine (no new deps) that walks the input, tracks in-string + escaped state, and emits the original text minus any,that's followed only by whitespace before}or]. Multi-byte UTF-8 chars are preserved verbatim because their continuation bytes never collide with the,/\"/\\/}/]markers we look at.Tests
crates/cli/src/migrate/tests.rsadds:load_json_or_jsonc_accepts_trailing_commas— reproduces the issue exactly: aknip.jsoncshaped file with trailing commas after"src/main.ts"and"dist/**"and the outermost object closer. Was failing before, parses correctly now.load_json_or_jsonc_handles_comments_and_trailing_commas_together—// line comment,/* block comment */, and trailing commas all in the same file."hello, world,"), escaped quotes ("he said \"hi,\","), and pretty-printed-with-newlines input.cargo test -p fallow-cli— 1745 passed, 0 failed.Backwards compatibility
Strict JSON inputs hit branch 1 and bypass the new pass entirely, so anything that worked before still works. The new pass only runs when both branch 1 and branch 2 already failed, so it's a strictly additive fallback.