rust-lang/cargo · error · MissingFieldError

missing field

Error message

missing field `{}`

What it means

`ConfigError` implements `serde::de::Error`. The `missing_field` method is invoked by serde's derive machinery when a struct field declared required (non-optional, no `default`) is absent during deserialization. It wraps a `MissingFieldError(field.to_string())` into an `anyhow::Error` with `definition: None`. This is Cargo's config-deserialization path surfacing a missing required key as a typed error.

Solutions

  1. Add the missing field named in the error to the relevant config table.
  2. Check the Cargo version's expected schema for that config section and supply all required keys.
  3. Validate the config file against the documented structure for your Cargo version.

Example fix

# before — registry entry missing required field
[registries.myreg]
index = "https://example.com/crates/"
# (missing a required field reported by the error)

# after — add the field named in the error
[registries.myreg]
index = "https://example.com/crates/"
token = "..."
Defensive patterns

Strategy: validation

Validate before calling

// before deserializing a config table, ensure required fields are present
fn has_required(tbl: &toml::value::Table, required: &[&str]) -> Result<(), String> {
    for k in required {
        if !tbl.contains_key(*k) { return Err(format!("missing field `{k}`")); }
    }
    Ok(())
}
// validate against the schema for your Cargo version, then deserialize

Prevention

When it happens

Trigger: Deserializing a Cargo config value (table) into a struct where a required field is absent. Serde invokes `<ConfigError as serde::de::Error>::missing_field("FieldName")`, producing this error before any definition-location context is attached.

Common situations: A config table missing a mandatory key (e.g. a registry entry without its required field), schema changes between Cargo versions that make a previously-optional field required, or malformed/legacy config files.

Related errors


AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11). Data as JSON: /api/errors/9b7e608da465c4d4. Report an issue: GitHub.

Appendix: 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 eb98b54bc9)