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

invalid package name `{}`: it conflicts with cargo's build d

Error message

invalid package name `{}`: it conflicts with cargo's build directory names{}

What it means

check_name reports this when restricted_names::is_conflicting_artifact_name(name) is true AND the package is a binary (has_bin). Names like `debug`, `release`, `build`, `deps`, `incremental` collide with the directory names Cargo writes under target/, so a binary crate with such a name would shadow its own build artifacts. For non-binary packages the same condition only emits a warning.

Source

Thrown at src/ops/cargo_new.rs:218

            ));
        }
        help
    };
    PackageName::new(name).map_err(|err| {
        let help = bin_help();
        anyhow::anyhow!("{err}{help}")
    })?;

    if restricted_names::is_keyword(name) {
        anyhow::bail!(
            "invalid package name `{}`: it is a Rust keyword{}",
            name,
            bin_help()
        );
    }
    if restricted_names::is_conflicting_artifact_name(name) {
        if has_bin {
            anyhow::bail!(
                "invalid package name `{}`: \
                it conflicts with cargo's build directory names{}",
                name,
                name_help
            );
        } else {
            shell.warn(format!(
                "package `{}` will not support binary \
                executables with that name, \
                it conflicts with cargo's build directory names",
                name
            ))?;
        }
    }
    if name == "test" {
        anyhow::bail!(
            "invalid package name `test`: \
            it conflicts with Rust's built-in test library{}",

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Rename to something that does not collide with target/ subdirs (debug, release, build, deps, incremental, examples, ...)
  2. Pass `--name <non-conflicting>` while keeping the directory name
  3. If you only need a library (not a binary), drop --bin; the conflict then degrades to a warning

Example fix

# before
cargo new --bin debug
# after
cargo new --bin my-debugger   # or: cargo new debug --name my-debugger
Defensive patterns

Strategy: validation

Validate before calling

const CONFLICTING: &[&str] = &[
    "build","checks","deps","examples","incremental","debug","release",
];
fn is_conflicting_artifact(name: &str) -> bool {
    CONFLICTING.contains(&name)
}

if has_bin && is_conflicting_artifact(name) { /* reject */ }

Type guard

const CONFLICTING = new Set(['build','checks','deps','examples','incremental','debug','release']);
function isSafeBinName(name: string): boolean { return !CONFLICTING.has(name); }

Prevention

When it happens

Trigger: `cargo new --bin debug`, `cargo new --bin build`, or a directory named `deps`/`incremental`/`examples` used for `cargo new --bin`. The bail at cargo_new.rs:217-223 fires because has_bin is true.

Common situations: Building a debugger or build-tool and naming the binary crate `debug` or `build`; cloning a tutorial folder whose name is one of the target/ subdirectories; CI scripts that create throwaway crates in a dir called `build`.

Related errors


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