Hmbown/CodeWhale · error · ValidationError

cargo metadata failed with exit code

Error message

cargo metadata failed with exit code {process.returncode}{suffix}

What it means

When no metadata file is supplied, validate-crate-publish-order.py runs `cargo metadata --locked --format-version 1 --no-deps` and raises this ValidationError if the command exits nonzero, appending cargo's stderr detail. This means cargo itself failed to produce workspace metadata.

Solutions

  1. Read the appended stderr detail to identify cargo's specific complaint
  2. If --locked fails, run cargo metadata --locked manually and then `cargo update --workspace` or commit an up-to-date Cargo.lock
  3. Run the script from the workspace root or pass a pre-generated metadata file via the metadata_file argument
  4. Validate Cargo.toml syntax; fix any malformed manifest sections

Example fix

# before (lock out of sync)
cargo metadata --locked ... # fails
# after
cargo update --workspace --locked  # or: cargo check to regenerate lock, commit it
Defensive patterns

Strategy: try-catch

Validate before calling

import subprocess
r = subprocess.run(["cargo", "metadata", "--locked", "--format-version", "1", "--no-deps"], capture_output=True, text=True)
if r.returncode != 0:
    raise SystemExit(f"fix cargo first: {r.stderr}")

Try / catch

try:
    ok = validate_publish_order()
except ValidationError as e:
    if "cargo metadata failed" in str(e):
        print(e)  # stderr detail is appended; act on it directly
        print("hint: run from workspace root; ensure Cargo.lock is committed and in sync")

Prevention

When it happens

Trigger: Running the script outside a Cargo workspace (no Cargo.toml), a malformed Cargo.toml, `--locked` failing because Cargo.lock is out of sync with manifests, or cargo not functioning in the current environment (broken toolchain).

Common situations: CI job running the validator before checkout of a workspace or in the wrong directory; a contributor bumped a dependency in Cargo.toml without updating Cargo.lock so --locked fails; invalid TOML after a manual manifest edit; minimal containers without a working cargo.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/a4ed31a3d88ab4f8. Report an issue: GitHub.

Appendix: source

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


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:
        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")

View on GitHub (pinned to 433685b202)