rust-lang/cargo · error

missing config key

Error message

missing config key `{}`

What it means

`ConfigError::missing` is returned when a required config key is absent from all layers and has no default. Unlike a type error, there is no `Definition` (the field is `None`) because no source file ever provided the value.

Solutions

  1. Set the missing key in the appropriate config layer (project `.cargo/config.toml`, user `~/.cargo/config.toml`, or env).
  2. Use `cargo config get <key>` to confirm absence and where to add it.
  3. Provide the value via the `--config <key>=<value>` CLI flag for one-off runs.
  4. Check the error's surrounding context message for the full key path.

Example fix

// before
# error: missing config key `registry.default`
cargo publish

// after (.cargo/config.toml)
[registry]
default = "crates-io"
Defensive patterns

Strategy: validation

Validate before calling

fn config_key_present(gctx: &GlobalContext, key: &str) -> bool {
    gctx.get::<toml::Value>(key).is_ok()
}

Type guard

null

Try / catch

match gctx.get::<String>(&key) {
    Err(e) if e.to_string().contains("missing config key") => use_default(&key),
    r => r?,
}

Prevention

When it happens

Trigger: Cargo-internal code calls `get::<T>(&key)` for a key that must exist but wasn't set anywhere; e.g. a required `[registry]` token, or a path that defaults only in some profiles.

Common situations: First-time setup missing credentials; CI without `CARGO_REGISTRY_TOKEN`; a workspace that overrides defaults removing a required field.

Related errors


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

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