rust-lang/cargo · error · anyhow::Error
Cannot specify both 'bin' and 'bin:<name>' binary artifacts,
Error message
Cannot specify both 'bin' and 'bin:<name>' binary artifacts, as 'bin' selects all available binaries.
What it means
ArtifactKind::validate rejects a dependency artifact list that contains both `bin` (AllBinaries — selects every binary the crate provides) and any `bin:<name>` (SelectedBinary — a specific binary). Combining them is contradictory: `bin` already covers the named one, so specifying both is ambiguous and almost always a user mistake.
Source
Thrown at src/workspace/dependency.rs:666
"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 0e07a15537)
Solutions
- Choose one strategy: use `bin` for all binaries, or list each with `bin:<name>`.
- Remove the blanket `bin` entry when you list specific `bin:<name>` artifacts.
- Re-read RFC 3452: `bin` and `bin:<name>` are mutually exclusive within one dependency.
Example fix
# Cargo.toml before [dependencies.mycrate] artifact = ["bin", "bin:mybin"] # after (specific binaries only) [dependencies.mycrate] artifact = ["bin:mybin"] # or all binaries [dependencies.mycrate] artifact = ["bin"]
Defensive patterns
Strategy: validation
Validate before calling
fn validate_artifact_list(kinds: &[String]) -> Result<(), String> {
let has_all = kinds.iter().any(|k| k == "bin");
let has_named = kinds.iter().any(|k| k.starts_with("bin:") && k.len() > 4);
if has_all && has_named {
return Err("cannot mix 'bin' with 'bin:<name>'; choose one".into());
}
Ok(())
} Type guard
fn artifact_list_is_consistent(kinds: &[String]) -> bool {
let all = kinds.iter().any(|k| k == "bin");
let named = kinds.iter().any(|k| k.starts_with("bin:"));
!(all && named)
} Try / catch
match ArtifactKind::validate(parsed_kinds) {
Err(e) if e.to_string().contains("Cannot specify both 'bin' and 'bin:") => {
eprintln!("pick either 'bin' (all) or specific 'bin:<name>' entries");
return Err(e);
}
r => r,
} Prevention
- Treat `bin` as exclusive: once used, do not add `bin:<name>` in the same dependency.
- Lint the `artifact` array in CI toml checks.
- Remove blanket `bin` when narrowing to specific binaries.
When it happens
Trigger: Writing `artifact = ["bin", "bin:mybin"]` (or multiple entries mixing `bin` with one or more `bin:<name>`) in a single Cargo.toml dependency.
Common situations: Misunderstanding that `bin` means all binaries; copy-pasting artifact entries together; incrementally adding a named binary while leaving a blanket `bin`.
Related errors
- '{}' is not a valid artifact specifier
- dependency `{}` in package `{}` requires a `{}` artifact to
- Deprecated dependency sections are unsupported: {}
- could not compile due to {error_count} previous target resol
- {manifest_key_name} `{}` does not appear to exist{}.
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/482a2ea857c1875d.json.
Report an issue: GitHub.