rust-lang/cargo · error · MissingFieldError

missing field `{}`

Error message

missing field `{}`

What it means

Produced by `ConfigError::missing_field` (src/context/error.rs:105) — the `serde::de::Error` implementation for cargo's config deserialization. Serde invokes `missing_field` when a struct field declared without `#[serde(default)]` is absent from the deserialized TOML/JSON. The wrapped `MissingFieldError` formats as `missing field \"<name>\"`.

Source

Thrown at src/context/error.rs:107

impl fmt::Display for MissingFieldError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "missing field `{}`", self.0)
    }
}

impl std::error::Error for MissingFieldError {}

impl serde::de::Error for ConfigError {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        ConfigError {
            error: anyhow::Error::msg(msg.to_string()),
            definition: None,
        }
    }

    fn missing_field(field: &'static str) -> Self {
        ConfigError {
            error: anyhow::Error::new(MissingFieldError(field.to_string())),
            definition: None,
        }
    }
}

impl From<anyhow::Error> for ConfigError {
    fn from(error: anyhow::Error) -> Self {
        ConfigError {
            error,
            definition: None,
        }
    }
}

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Read the error's surrounding context (it is usually wrapped with the config key path) and add the missing field with a valid value.
  2. Consult the cargo version's documentation for the required fields of the offending table.
  3. If the field should be optional, the struct author must add `#[serde(default)]` — for end users, supply the value explicitly.

Example fix

# before — registries.custom missing index
[registries.custom]
token = "..."
# after
[registries.custom]
index = "https://example.com/index"
token = "..."
Defensive patterns

Strategy: validation

Validate before calling

// Validate config schema before deserializing with serde.
use serde::Deserialize;
#[derive(Deserialize)]
struct MyConfig { #[serde(default)] index: Option<String>, /* required: */ token: String }
// or use a config validator (e.g. `cargo`'s ConfigValue checks) to ensure required keys are present.

Try / catch

// Treat ConfigError::missing_field distinctly
match gctx.get::<MyConfig>("registries.custom") {
    Err(e) if e.to_string().contains("missing field") => { /* prompt user for the field */ }
    res => res,
}

Prevention

When it happens

Trigger: Deserializing a manifest or config struct (via serde) where a required field is absent — e.g. a `[registry]` table missing a required `index`, or a custom config value type missing a mandatory key. The error bubbles up through `with_key_context` to name the offending config key.

Common situations: Migrating cargo config schema and forgetting a newly-required field; hand-editing `.cargo/config.toml` and omitting a key the active cargo version mandates; third-party credential/network config requiring fields older cargo versions did not.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/9b7e608da465c4d4.json. Report an issue: GitHub.