rust-lang/cargo · error

Cannot specify both 'bin' and 'bin

Error message

Cannot specify both 'bin' and 'bin:<name>' binary artifacts, as 'bin' selects all available binaries.

What it means

ArtifactKind::validate rejects combining the bare `bin` (all binaries) with one or more `bin:<name>` selections, because `bin` already implies every binary — listing specifics alongside it is ambiguous/redundant.

Solutions

  1. Use only `bin` if you want all binaries.
  2. Or list each specific `bin:<name>` you need, omitting the bare `bin`.

Example fix

# before (Cargo.toml)
# [dependencies]
# foo = { version = "1", artifact = ["bin", "bin:cli"] }

# after
# [dependencies]
# foo = { version = "1", artifact = "bin" }
# or: foo = { version = "1", artifact = ["bin:cli"] }
Defensive patterns

Strategy: validation

Validate before calling

fn validate_artifact_kinds(kinds: &[String]) -> Result<(), String> {
    let has_all = kinds.iter().any(|k| k == "bin");
    let has_specific = kinds.iter().any(|k| k.starts_with("bin:"));
    if has_all && has_specific {
        return Err("cannot mix 'bin' with 'bin:<name>'".into());
    }
    Ok(())
}

Type guard

fn artifact_kinds_consistent(kinds: &[String]) -> bool {
    let has_all = kinds.iter().any(|k| k == "bin");
    let has_specific = kinds.iter().any(|k| k.starts_with("bin:"));
    !(has_all && has_specific)
}

Prevention

When it happens

Trigger: Declaring a dependency artifact list mixing both forms: `artifact = ["bin", "bin:mytool"]`.

Common situations: Trying to be explicit while also selecting all binaries; merging two manifest snippets naively.

Related errors


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

Appendix: source

Thrown at src/workspace/dependency.rs:660

            "staticlib" => ArtifactKind::Staticlib,
            _ => {
                return kind
                    .strip_prefix("bin:")
                    .map(|bin_name| ArtifactKind::SelectedBinary(bin_name.into()))
                    .ok_or_else(|| {
                        anyhow::anyhow!("'{}' is not a valid artifact specifier", kind)
                    });
            }
        })
    }

    fn validate(kinds: Vec<ArtifactKind>) -> CargoResult<Vec<ArtifactKind>> {
        if kinds.iter().any(|k| matches!(k, ArtifactKind::AllBinaries))
            && kinds
                .iter()
                .any(|k| matches!(k, ArtifactKind::SelectedBinary(_)))
        {
            anyhow::bail!(
                "Cannot specify both 'bin' and 'bin:<name>' binary artifacts, as 'bin' selects all available binaries."
            );
        }
        let mut kinds_without_dupes = kinds.clone();
        kinds_without_dupes.sort();
        kinds_without_dupes.dedup();
        let num_dupes = kinds.len() - kinds_without_dupes.len();
        if num_dupes != 0 {
            anyhow::bail!(
                "Found {} duplicate binary artifact{}",
                num_dupes,
                (num_dupes > 1).then(|| "s").unwrap_or("")
            );
        }
        Ok(kinds)
    }
}

View on GitHub (pinned to 98a09e7e7d)