rust-lang/cargo · error

the binary target name

Error message

the binary target name `{name}` is forbidden, it conflicts with cargo's build directory names

What it means

A binary target name cannot collide with Cargo's own output directory/layout names (`build`, `deps`, `examples`, `incremental`, `native`, `registry`, etc.), because the artifact would be written into or confused with those directories. `restricted_names::is_conflicting_artifact_name` catches these at targets.rs:1168-1172.

Solutions

  1. Rename the binary to something not in Cargo's reserved artifact set.
  2. Check `is_conflicting_artifact_name` against your chosen name before committing.
  3. If the on-disk binary must keep that name, set `[[bin]] name = "..."` to a safe name and `filename = "build"` only if truly required.

Example fix

# before
[[bin]]
name = "build"

# after
[[bin]]
name = "my-builder"
Defensive patterns

Strategy: validation

Validate before calling

fn is_safe_bin_name(name: &str) -> bool {
    !matches!(name,
        "deps" | "build" | "examples" | "incremental" | "native" | "registry")
}
// (mirrors cargo_core::restricted_names::is_conflicting_artifact_name)

Prevention

When it happens

Trigger: `[[bin]] name = "deps"` (or `"build"`, `"examples"`, etc.). The `is_conflicting_artifact_name` predicate returns true and bails.

Common situations: Naming a CLI tool generically (e.g. `build`). Templated scaffolding that produces short directory-style names. Copying a workspace member name that happens to match a Cargo dir.

Understand the failure class

Related errors


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

Appendix: source

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

        .as_deref()
        .unwrap_or_else(|| panic!("target name is required"))
}

fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec<String>) -> CargoResult<()> {
    validate_target_name(target, TARGET_KIND_HUMAN_LIB, TARGET_KIND_LIB, warnings)?;
    let name = name_or_panic(target);
    if name.contains('-') {
        anyhow::bail!("library target names cannot contain hyphens: {}", name)
    }

    Ok(())
}

fn validate_bin_name(bin: &TomlTarget, warnings: &mut Vec<String>) -> CargoResult<()> {
    validate_target_name(bin, TARGET_KIND_HUMAN_BIN, TARGET_KIND_BIN, warnings)?;
    let name = name_or_panic(bin).to_owned();
    if restricted_names::is_conflicting_artifact_name(&name) {
        anyhow::bail!(
            "the binary target name `{name}` is forbidden, \
                 it conflicts with cargo's build directory names",
        )
    }

    Ok(())
}

fn validate_target_name(
    target: &TomlTarget,
    target_kind_human: &str,
    target_kind: &str,
    warnings: &mut Vec<String>,
) -> CargoResult<()> {
    match target.name {
        Some(ref name) => {
            if name.trim().is_empty() {
                anyhow::bail!("{} target names cannot be empty", target_kind_human)

View on GitHub (pinned to eb98b54bc9)