fix: --json emits invalid JSON when a column is skipped by --only or --tree - #958
Open
VXNCXNX wants to merge 1 commit into
Open
fix: --json emits invalid JSON when a column is skipped by --only or --tree#958VXNCXNX wants to merge 1 commit into
VXNCXNX wants to merge 1 commit into
Conversation
display_json decided separators by index over ALL columns but only emitted visible non-empty ones. --only and --tree thus left trailing or leading commas. Fixed by collecting emitted fields and joining.
VXNCXNX
force-pushed
the
fix-json-trailing-comma
branch
from
August 24, 2026 13:45
8b88c0f to
bfa2c02
Compare
Author
|
Rebased onto current On the new base: Dropping the |
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.
What's broken
--jsonemits invalid JSON whenever a column is skipped, which is what--onlyand--treeboth do.--tree --jsonfails the same way, from the other end:Every consumer of
--jsonis a program, so this is the one output mode where a stray comma is fatal rather than cosmetic.Cause
The separator was decided by position among all columns:
while the field itself was only written for visible columns. Two ways to diverge:
--onlyleaves later columns invisible, so the last emitted field is not the last indexed one and still gets a trailing", ".display_jsonreturns an empty string, so the branch runs, writes nothing, and contributes a separator anyway. That one lands at the front, giving{, "PID": ....#839 fixed the
--only/--treepanic and #763 taught this same loop to skipSeparatorcolumns, so the shape of the problem is known. What neither touched is the index the comma is keyed on, which is why the invalid output survived both.The fix
Collect the fields that are actually emitted, drop the empty ones, and
join(", "). A separator can then only appear between two real fields, which is the invariant that was missing.After, all three modes are valid JSON, and the default
procs --jsonis unchanged at the same 227 rows on this machine.Verification
A unit test on the new
json_objecthelper covering the empty case, one field, a field with empties on both sides, and two fields.Reverting the change fails it:
End to end, before and after binaries built from this same checkout, piping each mode through a JSON parser:
cargo test --releaseis 14 passed, 0 failed.cargo clippyandcargo fmt --checkare clean.