Hmbown/CodeWhale · error · ValidationError

could not read Cargo metadata: {error}

Error message

could not read Cargo metadata: {error}

What it means

load_metadata() raises this ValidationError when reading the file passed via --metadata-file fails with an OSError. The --metadata-file flag exists so the script's tests can feed canned cargo metadata JSON instead of invoking cargo; pointing it at a missing, unreadable, or wrong-type path produces this error with the underlying OS detail appended.

Source

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


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--metadata-file",
        type=Path,
        help="Read Cargo metadata from this file instead of invoking cargo (tests only).",
    )
    parser.add_argument("crates", nargs="+", help="Maintained publication order")
    return parser.parse_args()


def load_metadata(metadata_file: Path | None) -> dict[str, Any]:
    if metadata_file is not None:
        try:
            raw = metadata_file.read_text(encoding="utf-8")
        except OSError as error:
            raise ValidationError(f"could not read Cargo metadata: {error}") from error
    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:

View on GitHub (pinned to 8880682c63)

Solutions

  1. Verify the path exists and is a readable file: test -f <path>
  2. Regenerate the fixture: cargo metadata --locked --format-version 1 --no-deps > <path>
  3. Omit --metadata-file entirely to have the script invoke cargo metadata directly
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

metadata_file = Path(args_metadata_file)
if metadata_file is not None and not metadata_file.is_file():
    raise SystemExit(f"metadata file not readable: {metadata_file}")

Prevention

When it happens

Trigger: Passing --metadata-file with a path that does not exist; the file or a parent directory lacks read permission; the path is a directory; a test fixture was moved or renamed without updating the test.

Common situations: Running the script or its unit tests with a stale fixture path after a repo reorganization, or a typo in the --metadata-file argument.

Related errors


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