Hmbown/CodeWhale · error · ValidationError
Cargo metadata is not valid JSON
Error message
Cargo metadata is not valid JSON: {error} What it means
validate-crate-publish-order.py parses the raw cargo metadata output as JSON and raises this ValidationError if json.loads fails, wrapping the JSONDecodeError detail. The metadata source (file or cargo stdout) did not contain valid JSON.
Solutions
- Regenerate the metadata file with cargo metadata --locked --format-version 1 --no-deps > metadata.json
- Validate the file with `python -m json.tool metadata.json` to locate the syntax problem
- Omit the file argument so the script fetches fresh metadata from cargo directly
- Check the file is not truncated (compare size / tail of file)
Example fix
# before (bad artifact) python validate-crate-publish-order.py stale.txt # after cargo metadata --locked --format-version 1 --no-deps > cargo-metadata.json python validate-crate-publish-order.py cargo-metadata.json
Defensive patterns
Strategy: validation
Validate before calling
import json
raw = open("cargo-metadata.json", encoding="utf-8").read()
try:
json.loads(raw)
except json.JSONDecodeError as e:
raise SystemExit(f"metadata file is not valid JSON (line {e.lineno}): regenerate with cargo metadata") Try / catch
try:
validate_publish_order(metadata_file)
except ValidationError as e:
if "not valid JSON" in str(e):
print("regenerate: cargo metadata --locked --format-version 1 --no-deps > cargo-metadata.json") Prevention
- Only feed the validator files produced by cargo metadata --format-version 1
- Run python -m json.tool on the artifact before validation in CI
- Avoid hand-editing metadata JSON; regenerate instead
When it happens
Trigger: A hand-edited or truncated metadata file is passed via metadata_file; the file contains TOML or human-readable text instead of JSON; a wrapper script captured cargo's progress/log output mixed into stdout.
Common situations: Saving `cargo metadata` output without --format-version 1 or accidentally redirecting warnings into the file; an old metadata artifact from a different tool version; partial file writes from a failed CI artifact upload.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- Cargo metadata must contain workspace_members and packages…
- Cargo metadata root must be an object
- Cannot parse portable data; use JSON for OpenCode or plain…
- Cargo metadata dependencies for
- cargo metadata failed with exit code
AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15).
Data as JSON: /api/errors/b517124cc1151871.
Report an issue: GitHub.
Appendix: 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 433685b202)