rust-lang/cargo · error

` ` expected , but found a

Error message

`{}` expected {}, but found a {}

What it means

`ConfigError::expected` constructs a typed error when a config value's type doesn't match the declared type for its key (e.g. `[build] jobs` declared as integer but found a boolean). It includes the key, the expected type name, and `found.desc()` (the actual TOML type), plus the `Definition` pointing at the source file:line.

Solutions

  1. Open the `definition` file:line from the error and change the value to the expected type.
  2. Consult `cargo help config` / the manifest reference for the field's accepted type.
  3. Validate with `cargo config get <key>` after fixing.
  4. Use a TOML linter or LSP to catch schema mismatches early.

Example fix

// before (.cargo/config.toml)
[term]
color = "yes"   # expected bool

// after
[term]
color = true
Defensive patterns

Strategy: validation

Validate before calling

fn config_value_matches_type(key: &str, v: &toml::Value, expected: ExpectedType) -> bool {
    matches!((v, expected), (toml::Value::Boolean(_), ExpectedType::Bool) | (toml::Value::Integer(_), ExpectedType::Int) | (toml::Value::String(_), ExpectedType::Str))
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Defining a config key with the wrong TOML type: `build.jobs = true`, `term.color = "yes"` (expected enum), `[net.git-fetch-with-cli] = "maybe"` instead of bool.

Common situations: Hand-editing `.cargo/config.toml`; copying examples from docs with a different schema version; typos turning values into sections or vice versa.

Related errors


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

Appendix: source

Thrown at src/context/error.rs:25

/// Internal error for serde errors.
#[derive(Debug)]
pub struct ConfigError {
    error: anyhow::Error,
    definition: Option<Definition>,
}

impl ConfigError {
    pub(super) fn new(message: String, definition: Definition) -> ConfigError {
        ConfigError {
            error: anyhow::Error::msg(message),
            definition: Some(definition),
        }
    }

    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,
        }

View on GitHub (pinned to eb98b54bc9)