Skip to content

Published --json CLI crashes with misleading "Error: database error:" prefix and exit code 3 on trimmed build #147

Description

@Widthdom

Context

On the published v1.9.0 binary (built with PublishTrimmed=true), any CLI command invoked with --json crashes because reflection-based System.Text.Json serialization is disabled under trimming. CLOUD_BOOTSTRAP_PROMPT.md acknowledges this as a known caveat. The end-user visible shape of the crash is what this issue is about.

Repro

curl -fsSL https://raw.githubusercontent.com/Widthdom/CodeIndex/main/install.sh | bash
export PATH="$HOME/.local/bin:$PATH"

cdidx . --db /tmp/smoke.db
cdidx status --db /tmp/smoke.db --json
echo "exit=$?"

Observed

Error: database error: Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.
exit=3

Also reproduces with cdidx <projectPath> --json, cdidx search "..." --json, cdidx files --json, etc.

Why this is worse than just "--json is broken"

The surface behavior looks like a database problem to anyone who does not already know about the trimming caveat:

  1. The error is classified as database error: — the underlying InvalidOperationException from System.Text.Json is caught somewhere that assumes a SQLite/IO failure, and prepends a "database error:" label to a message that has nothing to do with the database.
  2. The exit code is 3 (database error) per CommandExitCodes.DbError, not a separate "internal"/"unsupported" code, so AI clients and CI scripts that branch on exit codes treat this as a legitimate DB failure and e.g. trigger reindex recovery paths.
  3. The caveat is only mentioned in CLOUD_BOOTSTRAP_PROMPT.md, which most end users never see. The README advertises --json extensively (AI-first positioning), and there is no user-visible hint that it is currently non-functional on the published release.

Why this matters

  • --json is the documented AI/automation contract for CLI consumers (the non-MCP path). On the release users actually install via the one-liner, that entire contract is down. MCP is unaffected because it hand-rolls JSON.
  • The "database error" prefix sends the first-time debugger the wrong direction (rebuild the DB, inspect schema, etc.) instead of toward the real root cause (trimming + reflection-based serialization).
  • Exit code 3 overloads the "DB is broken; run --rebuild" signal onto a case where the DB is fine.

Suggested directions

  1. Fix the root cause: wire a source-generator JsonSerializerContext (JsonSerializable(typeof(...))) for the DTOs emitted by CLI JSON paths, or set JsonSerializerOptions.TypeInfoResolver = new DefaultJsonTypeInfoResolver() where safe, or disable PublishTrimmed in the release build. This is the only real fix.
  2. In the meantime, improve the observable failure:
    • Detect the InvalidOperationException: Reflection-based serialization has been disabled message at the CLI JSON boundary and emit a dedicated error — e.g. Error: --json is not available on this build (trimmed release). Use MCP for structured output, or default human-readable output otherwise.
    • Use a distinct exit code (not 3/DbError) — this is neither a usage error (1), not-found (2), nor a database error (3); a new "build limitation" / "feature unavailable" code would match the existing structured exit-code design better.
    • Note the limitation in README.md under the --json documentation so users do not discover it by crash.

Scope

  • Either a real serialization fix, or a small error-mapping + doc fix. Both are additive.
  • No change to MCP (already works).

Environment

  • cdidx: v1.9.0 (installed via install.sh one-liner)
  • Platform: linux-x64 container
  • Filed during a cloud Claude Code session per CLOUD_BOOTSTRAP_PROMPT.md.

Notes

Searched open/closed issues for "trimmed"/"PublishTrimmed"/"reflection-based serialization"/"database error" misclassification — no existing match. The known-caveat note is in CLOUD_BOOTSTRAP_PROMPT.md only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions