Skip to content

fix: list --format json outputs [] for empty collections#9

Merged
hummer98 merged 1 commit into
masterfrom
claude/fix-issue-6-list-json-empty-collection
Jul 8, 2026
Merged

fix: list --format json outputs [] for empty collections#9
hummer98 merged 1 commit into
masterfrom
claude/fix-issue-6-list-json-empty-collection

Conversation

@hummer98

@hummer98 hummer98 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • firex list <path> --format json did not produce valid JSON for an empty collection: it printed the human-readable text No matching documents found (or nothing at all with --quiet) instead of [].
  • OutputFormatter.formatDocuments already returns '[]' for an empty array (see src/presentation/output-formatter.ts), so the non-empty JSON path was already correct — the bug was that ListCommand#queryCollection short-circuited on an empty result before reaching that formatter, printing text unconditionally instead.

Root cause

In src/commands/list.ts, queryCollection had:

if (documents.length === 0) {
  if (!quiet) {
    console.log('No matching documents found');
    console.log(`\nExecution time: ${executionTimeMs}ms`);
  }
  return;
}

This branch ran regardless of --format, so --format json on an empty collection either printed nothing (--quiet) or the text message — neither of which is parseable JSON.

Fix

Check format === 'json' first in the empty-result branch and print the literal [] (matching the existing convention already used by OutputFormatter.formatDocuments for non-empty JSON output), regardless of --quiet. Non-JSON formats (yaml/table/toon) keep their existing "No matching documents found" text behavior unchanged — only the JSON contract is fixed.

Tests

Added to src/commands/list.test.ts:

  • --format json + empty collection, quiet=false → stdout is exactly []
  • --format json + empty collection, quiet=true → stdout is exactly []
  • --format json + non-empty collection → unchanged (regression)
  • non-json format + empty collection, quiet=false → still prints No matching documents found (unchanged)
  • non-json format + empty collection, quiet=true → no output (unchanged)

src/mcp/tools/list.ts (MCP tool) was checked and does not share this bug — it always wraps output in { count, executionTimeMs, documents: [...] }, which is valid JSON even when documents is empty.

Local verification

  • npm run typecheck — pass
  • npm test — 1044/1044 tests pass (59 files)
  • npm run build — pass
  • npm run lintcould not verify: this repo has no ESLint config file anywhere in git history (confirmed via git log --all --diff-filter=A -- '*eslintrc*' 'eslint.config*'), so npm run lint fails with "ESLint couldn't find a configuration file" on a clean checkout of master itself, unrelated to this change.

Note on CI status (pre-existing, unrelated issue)

CI on master has been failing for several pushes now (including the 0.9.0 and 0.9.1 release commits) at the Install dependencies step (npm ci) with:

npm error Invalid: lock file's picomatch@2.3.2 does not satisfy picomatch@4.0.4

This is a pre-existing package-lock.json/peer-dependency issue orthogonal to this fix (reproduced on a pristine master checkout without legacy-peer-deps). This PR's CI run is expected to hit the same failure. Flagging it here rather than bundling an unrelated lockfile fix into this PR.

Fixes #6

list --format json previously fell through to a human-readable
"No matching documents found" branch when a query returned zero
documents, printing that text (or nothing, with --quiet) instead of
valid JSON. This broke the JSON output contract that non-empty
results already honor (OutputFormatter.formatDocuments returns '[]'
for an empty array).

Make the empty-result branch in ListCommand#queryCollection check
format === 'json' first and print the literal "[]", matching the
existing convention, regardless of --quiet. Non-JSON formats keep
their prior text-message behavior unchanged.

Fixes #6
@hummer98 hummer98 merged commit 843a4db into master Jul 8, 2026
2 of 8 checks passed
@github-actions github-actions Bot deleted the claude/fix-issue-6-list-json-empty-collection branch July 8, 2026 11:14
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.

list --format json が空コレクションで [] を出力しない(空出力/テキストになる)

1 participant