rust-lang/cargo · error

invalid package name

Error message

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

What it means

`restricted_names::is_conflicting_artifact_name` flags exactly `deps`, `examples`, `build`, and `incremental` — these are subdirectory names cargo creates under `target/`. When the package has a binary target (`has_bin`), naming the package one of these would collide with cargo's own output layout, so it is a hard error. For lib-only packages it downgrades to a warning.

Solutions

  1. Rename the package to something non-conflicting, e.g. `build-tool`, `deps-cli`, `example-runner`.
  2. If you only need a library (no binary), the name is allowed with just a warning — but a binary by that exact name is still impossible.
  3. Keep a valid package name and rename the binary via `[[bin]] name = "build"` only if you accept the target-path consequences (still discouraged).

Example fix

# before
$ cargo new --bin build   # error: conflicts with cargo's build directory names

# after
$ cargo new --bin build-tool
Defensive patterns

Strategy: validation

Validate before calling

const CONFLICTING: &[&str] = &["deps", "examples", "build", "incremental"];
fn is_conflicting_artifact_name(name: &str) -> bool { CONFLICTING.contains(&name) }
// reject for bin projects before `cargo new --bin <name>`.

Type guard

fn safe_bin_package_name(name: &str) -> bool { !is_conflicting_artifact_name(name) }

Prevention

When it happens

Trigger: `cargo new --bin build`, `cargo new --bin deps`, `cargo new --bin examples`, or `cargo new --bin incremental`. The `(has_bin == true)` branch is what bails.

Common situations: Naming a CLI tool after its function (`build`, `examples` runner). Copying a folder named `examples` into a new bin project. Auto-naming from a directory that happens to be one of these four.

Related errors


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

Appendix: source

Thrown at src/ops/cargo_new.rs:167

            ));
        }
        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 98a09e7e7d)