rust-lang/cargo · error · anyhow::Error

cannot specify both `metabuild` and `build`

Error message

cannot specify both `metabuild` and `build`

What it means

Error "cannot specify both `metabuild` and `build`" thrown in rust-lang/cargo.

Source

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

    targets.extend(to_test_targets(
        normalized_toml.test.as_deref().unwrap_or_default(),
        package_root,
        edition,
        warnings,
    )?);

    targets.extend(to_bench_targets(
        normalized_toml.bench.as_deref().unwrap_or_default(),
        package_root,
        edition,
        warnings,
    )?);

    // processing the custom build script
    if let Some(custom_build) = package.normalized_build().expect("previously normalized") {
        if metabuild.is_some() {
            anyhow::bail!("cannot specify both `metabuild` and `build`");
        }
        validate_unique_build_scripts(custom_build)?;
        for script in custom_build {
            let script_path = Path::new(script);
            let name = format!(
                "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,
            ));
        }
    }

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Choose either `metabuild` or `build` in the target/manifest, not both.
  2. Remove the `metabuild` key if a custom `build` script path is already specified.

When it happens

Trigger: Thrown at src/workspace/parser/targets.rs:105 when the library encounters an invalid state.

Common situations: Occurs when a manifest specifies both `metabuild` and a `build` script for the same package. Specify only one build mechanism: either the unstable `metabuild` key or a conventional `build` script path.


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