{"record":{"id":"a4ed31a3d88ab4f8","repo":"Hmbown/CodeWhale","slug":"cargo-metadata-failed-with-exit-code-process-returncode","errorCode":null,"errorMessage":"cargo metadata failed with exit code {process.returncode}{suffix}","messagePattern":"cargo metadata failed with exit code (.+?)(.+?)","errorType":"exception","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"scripts/release/validate-crate-publish-order.py","lineNumber":48,"sourceCode":"\n\ndef load_metadata(metadata_file: Path | None) -> dict[str, Any]:\n    if metadata_file is not None:\n        try:\n            raw = metadata_file.read_text(encoding=\"utf-8\")\n        except OSError as error:\n            raise ValidationError(f\"could not read Cargo metadata: {error}\") from error\n    else:\n        process = subprocess.run(\n            [\"cargo\", \"metadata\", \"--locked\", \"--format-version\", \"1\", \"--no-deps\"],\n            check=False,\n            capture_output=True,\n            text=True,\n        )\n        if process.returncode != 0:\n            detail = process.stderr.strip()\n            suffix = f\": {detail}\" if detail else \"\"\n            raise ValidationError(\n                f\"cargo metadata failed with exit code {process.returncode}{suffix}\"\n            )\n        raw = process.stdout\n\n    try:\n        metadata = json.loads(raw)\n    except json.JSONDecodeError as error:\n        raise ValidationError(f\"Cargo metadata is not valid JSON: {error}\") from error\n    if not isinstance(metadata, dict):\n        raise ValidationError(\"Cargo metadata root must be an object\")\n    return metadata\n\n\ndef workspace_packages(metadata: dict[str, Any]) -> list[dict[str, Any]]:\n    members = metadata.get(\"workspace_members\")\n    packages = metadata.get(\"packages\")\n    if not isinstance(members, list) or not isinstance(packages, list):\n        raise ValidationError(\"Cargo metadata must contain workspace_members and packages lists\")","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/release/validate-crate-publish-order.py#L30-L66","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Read the appended stderr detail to identify cargo's specific complaint","If --locked fails, run cargo metadata --locked manually and then `cargo update --workspace` or commit an up-to-date Cargo.lock","Run the script from the workspace root or pass a pre-generated metadata file via the metadata_file argument","Validate Cargo.toml syntax; fix any malformed manifest sections"],"exampleFix":"# before (lock out of sync)\ncargo metadata --locked ... # fails\n# after\ncargo update --workspace --locked  # or: cargo check to regenerate lock, commit it","handlingStrategy":"try-catch","validationCode":"import subprocess\nr = subprocess.run([\"cargo\", \"metadata\", \"--locked\", \"--format-version\", \"1\", \"--no-deps\"], capture_output=True, text=True)\nif r.returncode != 0:\n    raise SystemExit(f\"fix cargo first: {r.stderr}\")","typeGuard":null,"tryCatchPattern":"try:\n    ok = validate_publish_order()\nexcept ValidationError as e:\n    if \"cargo metadata failed\" in str(e):\n        print(e)  # stderr detail is appended; act on it directly\n        print(\"hint: run from workspace root; ensure Cargo.lock is committed and in sync\")","preventionTips":["Always run release scripts from the workspace root","Keep Cargo.lock committed and run cargo check before release validation","Pin the CI cargo toolchain version"],"tags":["python","cargo","subprocess","release"],"backgroundTag":"command-not-found","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}