rust-lang/cargo · error
missing config key `{}`
Error message
missing config key `{}` What it means
ConfigError::missing (error.rs:39-44) is produced when a required config key is requested but is not present in any source (config files, environment, or CLI). The message is 'missing config key `<key>`' with no definition (definition is None because no source provided it). This is the serde/`get::<T>()` path when a mandatory value is absent.
Source
Thrown at src/context/error.rs:41
pub(super) fn expected(key: &ConfigKey, expected: &str, found: &ConfigValue) -> ConfigError {
ConfigError {
error: anyhow::anyhow!(
"`{}` expected {}, but found a {}",
key,
expected,
found.desc()
),
definition: Some(found.definition().clone()),
}
}
pub(super) fn is_missing_field(&self) -> bool {
self.error.downcast_ref::<MissingFieldError>().is_some()
}
pub(super) fn missing(key: &ConfigKey) -> ConfigError {
ConfigError {
error: anyhow::anyhow!("missing config key `{}`", key),
definition: None,
}
}
pub(super) fn with_key_context(
self,
key: &ConfigKey,
definition: Option<Definition>,
) -> ConfigError {
ConfigError {
error: anyhow::Error::from(self)
.context(format!("could not load config key `{}`", key)),
definition: definition,
}
}
pub(super) fn with_array_item_key_context(
self,View on GitHub (pinned to 0e07a15537)
Solutions
- Provide the key in the appropriate config.toml, env var, or CLI flag as documented.
- If the value is optional in your use case, use `gctx.get::<OptValue<T>>(key)` instead of the required form.
- Check Cargo version compatibility for that key.
Example fix
// before (consuming cargo as a library)
let jobs: u32 = gctx.get("build.jobs")?; // required, bails if unset
// after
let jobs: Option<u32> = gctx.get("build.jobs")?; // optional Defensive patterns
Strategy: validation
Validate before calling
// When consuming cargo as a library, request optional values for keys
// that may be unset:
let jobs: Option<u32> = gctx.get("build.jobs")?; Try / catch
// Provide a default when a required key is missing:
let jobs: u32 = gctx.get("build.jobs")
.unwrap_or_else(|_| Ok(default_jobs()))?; Prevention
- Prefer OptValue<T> / Option<T> for config keys that are not strictly required.
- Document mandatory config keys prominently for users of your cargo-library tool.
- Check the Cargo version that introduced a key before depending on it.
When it happens
Trigger: Calling `gctx.get::<T>("some.required.key")` (non-optional) when the key is unset everywhere; a manifest/config schema field that is mandatory but was never provided.
Common situations: Cargo-internal code or a cargo-library consumer fetching a required config value that the user never set; a feature expecting a config key added in a newer Cargo version.
Related errors
- `{}` expected {}, but found a {}
- missing field `{}`
- subcommand is required, add a subcommand to the command alia
- alias {} has unresolvable recursive definition: {} -> {}
- subcommand is required, but `{alias_name}` is empty
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/af95e7ca89d21181.json.
Report an issue: GitHub.