Hmbown/CodeWhale · error · ValidationError

Cargo metadata is not valid JSON: {error}

Error message

Cargo metadata is not valid JSON: {error}

What it means

The metadata text failed json.loads with a JSONDecodeError. Real 'cargo metadata --format-version 1' always emits valid JSON, so in practice this means the --metadata-file fixture is corrupted, truncated, or contains non-JSON content.

Source

Thrown at scripts/release/validate-crate-publish-order.py:56

    else:
        process = subprocess.run(
            ["cargo", "metadata", "--locked", "--format-version", "1", "--no-deps"],
            check=False,
            capture_output=True,
            text=True,
        )
        if process.returncode != 0:
            detail = process.stderr.strip()
            suffix = f": {detail}" if detail else ""
            raise ValidationError(
                f"cargo metadata failed with exit code {process.returncode}{suffix}"
            )
        raw = process.stdout

    try:
        metadata = json.loads(raw)
    except json.JSONDecodeError as error:
        raise ValidationError(f"Cargo metadata is not valid JSON: {error}") from error
    if not isinstance(metadata, dict):
        raise ValidationError("Cargo metadata root must be an object")
    return metadata


def workspace_packages(metadata: dict[str, Any]) -> list[dict[str, Any]]:
    members = metadata.get("workspace_members")
    packages = metadata.get("packages")
    if not isinstance(members, list) or not isinstance(packages, list):
        raise ValidationError("Cargo metadata must contain workspace_members and packages lists")

    packages_by_id = {
        package.get("id"): package
        for package in packages
        if isinstance(package, dict) and isinstance(package.get("id"), str)
    }
    missing_ids = [member for member in members if member not in packages_by_id]
    if missing_ids:

View on GitHub (pinned to 8880682c63)

Solutions

  1. Locate the syntax error: python3 -m json.tool <metadata-file>
  2. Regenerate the fixture cleanly: cargo metadata --locked --format-version 1 --no-deps > <metadata-file>
  3. Make sure only stdout was redirected, with no shell diagnostics mixed in
Defensive patterns

Strategy: validation

Validate before calling

import json

if args.metadata_file is not None:
    json.loads(args.metadata_file.read_text(encoding="utf-8"))  # fail early with a clear location

Prevention

When it happens

Trigger: A fixture file that was hand-edited and broke syntax; a truncated redirect; stderr or shell noise captured into the file (e.g. '2>&1' or wrong fd order); an empty file.

Common situations: Maintaining test fixtures for validate-crate-publish-order.py and saving a partial or annotated copy of cargo metadata output.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/b517124cc1151871. Report an issue: GitHub.