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

{err}{help}

Error message

{err}{help}

What it means

Generic wrapper around the structured validation performed by PackageName::new (from cargo_util_schemas). It fires when the chosen package name violates core crate-name rules: empty name, leading digit, invalid characters (anything beyond ASCII alphanumerics, '-', '_'), exceeding the 64-character length cap, or a reserved form. The appended {help} text provides context-aware suggestions (e.g. how to set a [[bin]] name with a different package name).

Source

Thrown at src/ops/cargo_new.rs:206

            help.push_str(&format!(
                "\n\
                help: to name the binary \"{name}\", use a valid package \
                name, and set the binary name to be different from the package. \
                This can be done by setting the binary filename to `src/bin/{name}.rs` \
                or change the name in Cargo.toml with:\n\
                \n    \
                [[bin]]\n    \
                name = \"{name}\"\n    \
                path = \"src/main.rs\"\n\
            ",
                name = name
            ));
        }
        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 {

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Rename to use only ASCII letters, digits, hyphens, underscores, starting with a letter, <=64 chars
  2. Pass `--name <valid>` to override the auto-detected directory name
  3. If the invalid directory name is the cause, `mv` the directory first
  4. Read the appended help line; it often suggests the exact [[bin]] block to add so the binary name can differ from the package name

Example fix

# before
cargo new "My App"
# after
cargo new my-app
# or keep dir, override name:
cargo new "My App" --name my-app
Defensive patterns

Strategy: validation

Validate before calling

// Mirror cargo_util_schemas::manifest::PackageName rules
fn valid_pkg_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 64
        && name.chars().next().map_or(false, |c| c.is_ascii_alphabetic())
        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

if !valid_pkg_name(name) { /* fail early with a friendly message */ }

Type guard

const PKG_NAME_RE = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
function isValidPackageName(name: string): boolean {
  return PKG_NAME_RE.test(name);
}

Prevention

When it happens

Trigger: `cargo new 1app`, `cargo new my.app`, `cargo new ""`, `cargo new` in a directory whose name is too long (>64 chars), or `--name` with disallowed symbols. The error originates from PackageName::new(name) at cargo_new.rs:204.

Common situations: Naming a crate after a directory like `My App` (space), `2fa-helper` (leading digit), or a very long path component. Using CamelCase or dots because the project directory was created by another tool. Confusing package name rules with module name rules.

Related errors


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