rust-lang/cargo · error

manifest validated

Error message

manifest validated

What it means

This panic fires in the cargo add code path when adding an optional dependency. After inserting the dep into the manifest's dependency table, cargo checks if the [features] table already has explicit dep: activation and, if not, tries to get the [features] table mutably via get_table_mut(&[String::from("features")]).expect("manifest validated"). The invariant is that the manifest was earlier validated to support namespaced features and have or allow a [features] table.

Source

Thrown at src/ops/cargo_add/mod.rs:259

        print_dep_table_msg(&mut options.gctx.shell(), &dep)?;

        manifest.insert_into_table(
            &dep_table,
            &dep,
            workspace.gctx(),
            workspace.root(),
            options.spec.manifest().unstable_features(),
        )?;
        if dep.optional == Some(true) {
            let is_namespaced_features_supported =
                check_rust_version_for_optional_dependency(options.spec.rust_version())?;
            if is_namespaced_features_supported {
                let dep_key = dep.toml_key();
                if !manifest.is_explicit_dep_activation(dep_key) {
                    let table = manifest
                        .get_table_mut(&[String::from("features")])
                        .expect("manifest validated");
                    let dep_name = dep.rename.as_deref().unwrap_or(&dep.name);
                    let new_feature: toml_edit::Value =
                        [format!("dep:{dep_name}")].iter().collect();
                    table[dep_key] = toml_edit::value(new_feature);
                    options
                        .gctx
                        .shell()
                        .status("Adding", format!("feature `{dep_key}`"))?;
                }
            }
        }
        manifest.gc_dep(dep.toml_key());
    }

    if was_sorted {
        if let Some(table) = manifest
            .get_table_mut(&dep_table)
            .and_then(TomlItem::as_table_like_mut)

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Manually inspect Cargo.toml and ensure [features] is a proper TOML table or absent (not a scalar/array).
  2. Remove the --optional flag if you don't need optional dependency activation features.
  3. Fix the manifest to valid structure and re-run cargo add.

Example fix

# before (broken — features as scalar)
features = "default"
[dependencies]
# after
[features]
default = []
[dependencies]
Defensive patterns

Strategy: validation

Validate before calling

// Before running cargo add --optional, check [features] is a table
let manifest_str = std::fs::read_to_string("Cargo.toml")?;
let doc: toml_edit::DocumentMut = manifest_str.parse()?;
if let Some(features) = doc.get("features") {
    if !features.is_table() {
        return Err("[features] must be a TOML table".into());
    }
}

Type guard

fn features_table_is_valid(doc: &toml_edit::DocumentMut) -> bool {
    doc.get("features").map_or(true, |f| f.is_table())
}

Prevention

When it happens

Trigger: Running cargo add --optional <crate> on a manifest that passed the earlier validation check (is_namespaced_features_supported) but whose [features] table cannot be retrieved — e.g., the manifest has a [features] key that is not a table (set to a string or array), or the manifest was mutated in a way that removed the table between validation and this call.

Common situations: A Cargo.toml where [features] is misused as a non-table value; a manifest with inconsistent structure after partial edits; running cargo add on a manifest that has [features] defined as an inline value.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/fba2214a389a5008.json. Report an issue: GitHub.