Skip to main content

gen_cargo/
error.rs

1//! Typed errors emitted by the cargo adapter. Every parse failure
2//! goes through one variant — operators get a structured cause +
3//! filename + line where possible, never a stringly-typed message.
4
5use std::path::PathBuf;
6use thiserror::Error;
7
8#[derive(Debug, Error)]
9pub enum CargoError {
10    #[error("failed to read {path}: {source}")]
11    Io {
12        path: PathBuf,
13        #[source]
14        source: std::io::Error,
15    },
16    #[error("failed to parse {path} as TOML: {source}")]
17    Toml {
18        path: PathBuf,
19        #[source]
20        source: toml::de::Error,
21    },
22    #[error("workspace root at {root} declared member {member} but the path does not exist")]
23    MissingWorkspaceMember { root: PathBuf, member: String },
24    #[error("Cargo.toml at {path} has neither [package] nor [workspace]")]
25    EmptyManifest { path: PathBuf },
26    #[error("dependency `{name}` in {path} uses an unsupported source shape: {detail}")]
27    UnsupportedDepSource {
28        name: String,
29        path: PathBuf,
30        detail: String,
31    },
32    #[error("Cargo.lock at {path} entry {entry} is missing a required field: {field}")]
33    LockfileMissingField {
34        path: PathBuf,
35        entry: String,
36        field: &'static str,
37    },
38    #[error("version `{raw}` for {context} could not be parsed")]
39    BadVersion { raw: String, context: String },
40    #[error("dependency `{name}` requirement `{raw}` could not be parsed")]
41    BadVersionReq { name: String, raw: String },
42    #[error(
43        "external path-dep `{name}` (version {version}) at {abs_dir} escapes the workspace root \
44({workspace_root}) AND could not be resolved as a git source: {reason}. \
45Substrate's nix lockfile-builder cannot consume `path = \"../…\"` deps — the sibling repo's \
46source is not in the build sandbox. Either:\n\
47  1. Convert the dep to a git source in Cargo.toml:\n\
48       {name} = {{ git = \"https://github.com/<owner>/<repo>\" }}\n\
49  2. Ensure the sibling directory is a git repo with an `origin` remote pointing at a public host \
50(GitHub) so gen can auto-resolve it.\n\
51Once the Cargo.toml dep is git-based, run `gen build --if-stale` to regenerate the spec."
52    )]
53    UnresolvableExternalPath {
54        name: String,
55        version: String,
56        abs_dir: PathBuf,
57        workspace_root: PathBuf,
58        reason: String,
59    },
60    #[error(
61        "failed to prefetch sha256 for git source {url}#{rev}: {reason}. \
62The build spec MUST carry a fixed sha256 for every git source; emitting one \
63without sha256 would produce a non-FOD `pkgs.fetchgit` derivation that is \
64denied network access in the substrate sandbox and explodes downstream with \
65cryptic 'Could not resolve host' errors during nixos-rebuild. The prefetcher \
66is pure-Rust (gix + nix-nar) — check the remote is reachable (host DNS, \
67auth tokens for private repos) and the rev exists, then re-run `gen build`."
68    )]
69    PrefetchSha256Failed {
70        url: String,
71        rev: String,
72        reason: String,
73    },
74    #[error("fleet-sweep scheduler error: {0}")]
75    FleetSweep(String),
76}
77
78pub type Result<T> = std::result::Result<T, CargoError>;