rust-lang/cargo · error

metabuild package ` ` must be specified in…

Error message

metabuild package `{}` must be specified in `build-dependencies`

What it means

Every crate named in `metabuild` must also appear under `[build-dependencies]`, because Cargo compiles those crates as part of the build script generation phase. The parser iterates `metabuild.0` and checks `normalized_toml.build_dependencies` contains each name via `bd.contains_key(name.as_str())`. A missing entry means Cargo has no way to fetch and compile the metabuild generator.

Solutions

  1. Add the missing crate to `[build-dependencies]` with a compatible version, e.g. `foo = "1.0"`.
  2. Verify the spelling in `metabuild` exactly matches the dependency key.
  3. Run `cargo fetch` to confirm the dependency resolves.

Example fix

# before
[package]
metabuild = ["foo"]

# after
[package]
metabuild = ["foo"]
[build-dependencies]
foo = "1.0"
Defensive patterns

Strategy: validation

Validate before calling

fn metabuild_deps_satisfied(manifest: &Value) -> bool {
    let metabuild: Vec<String> = manifest
        .get("package").and_then(|p| p.get("metabuild"))
        .and_then(|m| m.as_array())
        .map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect())
        .unwrap_or_default();
    if metabuild.is_empty() { return true; }
    let bdeps = manifest.get("build-dependencies");
    metabuild.iter().all(|name| {
        bdeps.and_then(|b| b.as_table()).map_or(false, |t| t.contains_key(name))
    })
}

Prevention

When it happens

Trigger: Cargo.toml has `[package] metabuild = ["foo"]` but `[build-dependencies]` either is absent or has no `foo` key. The loop at targets.rs:128-135 fails the `map_or(false, ...)` membership test and bails.

Common situations: Typo between the metabuild name and the build-dependency key. Forgetting to add the build-dependency after enabling metabuild. Renaming a crate but not updating both sections.

Related errors


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

Appendix: source

Thrown at src/workspace/parser/targets.rs:129

                "build-script-{}",
                script_path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .expect("previously normalized")
            );
            targets.push(Target::custom_build_target(
                &name,
                package_root.join(script_path),
                edition,
            ));
        }
    }
    if let Some(metabuild) = metabuild {
        // Verify names match available build deps.
        let bdeps = normalized_toml.build_dependencies.as_ref();
        for name in &metabuild.0 {
            if !bdeps.map_or(false, |bd| bd.contains_key(name.as_str())) {
                anyhow::bail!(
                    "metabuild package `{}` must be specified in `build-dependencies`",
                    name
                );
            }
        }

        targets.push(Target::metabuild_target(&format!(
            "metabuild-{}",
            package.normalized_name().expect("previously normalized")
        )));
    }

    Ok(targets)
}

#[tracing::instrument(skip_all)]
pub fn normalize_lib(
    original_lib: Option<&TomlLibTarget>,

View on GitHub (pinned to eb98b54bc9)