{"record":{"id":"5d00aed1800450c5","repo":"Hmbown/CodeWhale","slug":"could-not-read-cargo-metadata-error","errorCode":null,"errorMessage":"could not read Cargo metadata: {error}","messagePattern":"could not read Cargo metadata: (.+?)","errorType":"exception","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"scripts/release/validate-crate-publish-order.py","lineNumber":37,"sourceCode":"\n\ndef parse_args() -> argparse.Namespace:\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\n        \"--metadata-file\",\n        type=Path,\n        help=\"Read Cargo metadata from this file instead of invoking cargo (tests only).\",\n    )\n    parser.add_argument(\"crates\", nargs=\"+\", help=\"Maintained publication order\")\n    return parser.parse_args()\n\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:","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/release/validate-crate-publish-order.py#L19-L55","documentation":"scripts/release/validate-crate-publish-order.py loads Cargo workspace metadata, either from a provided file or by invoking cargo metadata. This ValidationError wraps an OSError raised while reading the user-supplied metadata file, so the exact filesystem failure (missing file, permissions, etc.) is surfaced as a validation error.","triggerScenarios":"Calling load_metadata with a metadata_file Path that does not exist, is a directory, or is unreadable due to permissions, causing read_text to raise OSError.","commonSituations":"Typoed path to a saved cargo metadata JSON; passing a path relative to the wrong working directory in CI; a prior pipeline step that was supposed to dump cargo metadata output failed silently, leaving no file; read-only or restrictive permissions on the artifact.","solutions":["Verify the metadata file path exists and is a regular file (ls -l <path>)","Regenerate the file with cargo metadata --locked --format-version 1 --no-deps > metadata.json","Run the validator from the directory where the relative path resolves, or pass an absolute path","Check file permissions if the file exists but cannot be read"],"exampleFix":"# before\npython scripts/release/validate-crate-publish-order.py meta.json\n# after\npython scripts/release/validate-crate-publish-order.py ./cargo-metadata.json  # verified existing path","handlingStrategy":"try-catch","validationCode":"from pathlib import Path\np = Path(metadata_file)\nif not p.is_file():\n    raise SystemExit(f\"metadata file not found: {p.resolve()}\")\nif not os.access(p, os.R_OK):\n    raise SystemExit(f\"metadata file not readable: {p}\")","typeGuard":null,"tryCatchPattern":"try:\n    result = subprocess.run([\"python\", \"scripts/release/validate-crate-publish-order.py\", str(path)], check=False)\nexcept ValidationError as e:\n    if str(e).startswith(\"could not read Cargo metadata\"):\n        print(\"regenerate with: cargo metadata --locked --format-version 1 --no-deps > cargo-metadata.json\")","preventionTips":["Use absolute paths for the metadata file in CI","Regenerate the file in the same job that validates it; don't rely on stale artifacts","Check file existence/permissions before invoking the validator"],"tags":["python","filesystem","cargo","release"],"backgroundTag":"file-read-failed","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"}