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

Found {} duplicate binary artifact{}

Error message

Found {} duplicate binary artifact{}

What it means

Thrown by `ArtifactKind::validate` in src/workspace/dependency.rs:675 when parsing the `--artifact` CLI flags. Cargo collects every artifact specifier (e.g. `bin`, `bin:foo`, `cdylib`, `staticlib`), sorts and dedups them, and bails if any specifier appears more than once, because duplicates are redundant and would produce ambiguous build output.

Source

Thrown at src/workspace/dependency.rs:675

        })
    }

    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)
    }
}

/// Patch is a dependency override that knows where it has been defined.
/// See [`PatchLocation`] for possible locations.
#[derive(Clone, Debug)]
pub struct Patch {
    pub dep: Dependency,
    pub loc: PatchLocation,
}

/// Place where a [`Patch`] has been defined.

View on GitHub (pinned to 0e07a15537)

Solutions

  1. De-duplicate the `--artifact` values before passing them to cargo: `printf '%s\n' "${ARTIFACTS[@]}" | sort -u | xargs -I{} cargo build --artifact {}`.
  2. Inspect your build script/generator and remove the repeated `--artifact <same>` entry.
  3. Run `cargo build --artifact bin --artifact cdylib` with each kind only once; note `bin` already covers all binaries so never combine `bin` with `bin:<name>` (that is a separate error).

Example fix

// before
cargo build --artifact bin:foo --artifact bin:foo
// after
cargo build --artifact bin:foo
Defensive patterns

Strategy: validation

Validate before calling

use std::collections::BTreeSet;
let mut seen = BTreeSet::new();
let mut deduped = Vec::new();
for a in artifacts {
    if seen.insert(a.as_ref().to_string()) {
        deduped.push(a);
    }
}
// pass `deduped` to cargo / Artifact::parse

Prevention

When it happens

Trigger: Calling `cargo build --artifact bin:foo --artifact bin:foo`, `--artifact cdylib --artifact cdylib`, or `cargo build --artifact bin:foo --artifact bin:foo --artifact bin:foo` (produces count 2, pluralized "artifact{}"). The check runs in `Artifact::parse` which maps each `--artifact` value through `ArtifactKind::parse` then `ArtifactKind::validate`.

Common situations: Shell loops or scripts that append duplicate `--artifact` flags; copy-paste of build commands; build-system wrappers that fan out artifact lists without de-duplicating; CI matrices that concat artifact specs.

Related errors


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