Skip to content

fix(ls): keep JSON delimiters when --quiet is used - #1219

Open
pcbeingused333 wants to merge 1 commit into
Backblaze:masterfrom
pcbeingused333:fix/ls-json-quiet-1215
Open

pcbeingused333 wants to merge 1 commit into
Backblaze:masterfrom
pcbeingused333:fix/ls-json-quiet-1215

Conversation

@pcbeingused333

Copy link
Copy Markdown

Fixes #1215

Cause. b2 ls --json prints the array brackets and the separating commas with self._print(...), which is suppressed by --quiet, while each entry goes through _print_json (which sets enforce_output=True). With --quiet the output is therefore a run of bare objects without [, , and ], which is not valid JSON.

Fix. Pass enforce_output=True for the [, , and ] delimiters in ls --json, so the whole document is treated as data output like the entries themselves. Nothing else changes.

Tests. New test_ls__json__valid_with_quiet in test/unit/console_tool/test_ls.py: 0, 1 and 2 files, with and without --quiet, parsing stdout with json.loads. The --quiet cases fail on main with a JSON decode error. A news fragment is added in changelog.d/1215.fixed.md.

Reproduction. b2 ls --json --quiet b2://my-bucket | python -m json.tool on a bucket with at least one file (or an empty one, which prints nothing instead of []) fails on main and succeeds with this change.


This change was prepared with the help of an AI coding assistant (Claude) and submitted on Alex's behalf; the author has not independently reviewed or run it beyond the automated tests.

Fixes Backblaze#1215

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

BaseLs._run now prints JSON array delimiters and commas even when quiet mode is enabled. File-version JSON serialization is unchanged. Tests cover quiet and non-quiet listings with zero, one, or two files.

Severity of issue fixed: Medium

Merge Risk: Minimal · up to 2cb82

This fixes b2 ls --json --quiet so it emits valid JSON, and tests cover the empty, single and multi-file cases. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: JSON delimiters for b2 ls --json are kept when --quiet is used. This matches the raw summary, the description, and the objectives. The title uses the conventional fix(l…
Description check Passed The description states the cause (the [, ,, and ] delimiters go through self._print and are suppressed by --quiet) and the fix (enforce_output=True on those delimiters). It also lists the …
Linked Issues check Passed [#1215] Requirement: with --json and --quiet, b2 ls must emit a valid JSON array, including [] for an empty listing and brackets and commas around file objects. The reviewed diff in `BaseLs._r…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes three files. (1) b2/_internal/console_tool.py: only the four _print calls in the ls --json branch gain enforce_output=True. This is the fix for…
Full details: Title check

Explanation

The title names the change: JSON delimiters for b2 ls --json are kept when --quiet is used. This matches the raw summary, the description, and the objectives. The title uses the conventional fix(ls) prefix and is specific enough for a reader scanning history.

Full details: Description check

Explanation

The description states the cause (the [, ,, and ] delimiters go through self._print and are suppressed by --quiet) and the fix (enforce_output=True on those delimiters). It also lists the new test and the changelog fragment, so it relates directly to the changeset.

Full details: Linked Issues check

Explanation

[#1215] Requirement: with --json and --quiet, b2 ls must emit a valid JSON array, including [] for an empty listing and brackets and commas around file objects. The reviewed diff in BaseLs._run (b2/_internal/console_tool.py, around line 2591) passes enforce_output=True to the ',', '[', and ']' writes, and to the '[]' empty-listing branch. The _print_json entries already force output. Together, the whole document is emitted under --quiet. The changed code path matches the issue's description of the defect. The test test_ls__json__valid_with_quiet (0, 1, and 2 files, with and without --quiet, parsed with json.loads) covers the requirement. I did not read the test file or the _print implementation in this review. The enforce_output keyword is used as the issue describes _print_json using it, so its behavior is taken from the issue and the diff, not from a direct read of _print. No non-coding requirements exist in #1215. #941 is background only and no requirement from it applies.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes three files. (1) b2/_internal/console_tool.py: only the four _print calls in the ls --json branch gain enforce_output=True. This is the fix for #1215. (2) test/unit/console_tool/test_ls.py: adds the regression test for #1215. (3) changelog.d/1215.fixed.md: the news fragment for #1215. Each change connects to #1215. No unrelated behavior change appears in the reviewed diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 94384c96-0b4e-4d5b-8983-4602bd235d60
Commits

Reviewing files that changed from the base of the PR and between 9194ec5 and 2cb8214.

Files selected for processing: 3

  • b2/_internal/console_tool.py
  • changelog.d/1215.fixed.md
  • test/unit/console_tool/test_ls.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

b2 ls --json -q prints invalid JSON (no brackets or commas)

1 participant