{"id":"9b7e608da465c4d4","repo":"rust-lang/cargo","slug":"missing-field","errorCode":null,"errorMessage":"missing field `{}`","messagePattern":"missing field `(.+?)`","errorType":"validation","errorClass":"MissingFieldError","httpStatus":null,"severity":"error","filePath":"src/context/error.rs","lineNumber":107,"sourceCode":"impl fmt::Display for MissingFieldError {\n    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {\n        write!(f, \"missing field `{}`\", self.0)\n    }\n}\n\nimpl std::error::Error for MissingFieldError {}\n\nimpl serde::de::Error for ConfigError {\n    fn custom<T: fmt::Display>(msg: T) -> Self {\n        ConfigError {\n            error: anyhow::Error::msg(msg.to_string()),\n            definition: None,\n        }\n    }\n\n    fn missing_field(field: &'static str) -> Self {\n        ConfigError {\n            error: anyhow::Error::new(MissingFieldError(field.to_string())),\n            definition: None,\n        }\n    }\n}\n\nimpl From<anyhow::Error> for ConfigError {\n    fn from(error: anyhow::Error) -> Self {\n        ConfigError {\n            error,\n            definition: None,\n        }\n    }\n}\n","sourceCodeStart":89,"sourceCodeEnd":121,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/context/error.rs#L89-L121","documentation":"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>\\\"`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the error's surrounding context (it is usually wrapped with the config key path) and add the missing field with a valid value.","Consult the cargo version's documentation for the required fields of the offending table.","If the field should be optional, the struct author must add `#[serde(default)]` — for end users, supply the value explicitly."],"exampleFix":"# before — registries.custom missing index\n[registries.custom]\ntoken = \"...\"\n# after\n[registries.custom]\nindex = \"https://example.com/index\"\ntoken = \"...\"","handlingStrategy":"validation","validationCode":"// Validate config schema before deserializing with serde.\nuse serde::Deserialize;\n#[derive(Deserialize)]\nstruct MyConfig { #[serde(default)] index: Option<String>, /* required: */ token: String }\n// or use a config validator (e.g. `cargo`'s ConfigValue checks) to ensure required keys are present.","typeGuard":null,"tryCatchPattern":"// Treat ConfigError::missing_field distinctly\nmatch gctx.get::<MyConfig>(\"registries.custom\") {\n    Err(e) if e.to_string().contains(\"missing field\") => { /* prompt user for the field */ }\n    res => res,\n}","preventionTips":["Keep required config fields documented and templated.","Add `#[serde(default)]` for genuinely optional fields when authoring config structs.","Validate config files in CI before cargo runs."],"tags":["cargo","config","serde","deserialization","manifest"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}