rust-lang/cargo · error

crate name is empty

Error message

crate name is empty

What it means

From parse_crate (src/bin/cargo/commands/install.rs:271-273). After handling the `@` split, if the resulting crate name is empty (e.g. the whole spec was empty or reduced to nothing), it bails. Guards against an empty package name reaching PackageName validation.

Source

Thrown at src/bin/cargo/commands/install.rs:272

type CrateVersion = (String, Option<VersionReq>);

fn parse_crate(krate: &str) -> crate::CargoResult<CrateVersion> {
    let (krate, version) = if let Some((k, v)) = krate.split_once('@') {
        if k.is_empty() {
            // by convention, arguments starting with `@` are response files
            anyhow::bail!("missing crate name before '@'");
        }
        let krate = k.to_owned();
        let version = Some(parse_semver_flag(v)?);
        (krate, version)
    } else {
        let krate = krate.to_owned();
        let version = None;
        (krate, version)
    };

    if krate.is_empty() {
        anyhow::bail!("crate name is empty");
    }

    Ok((krate, version))
}

/// Parses x.y.z as if it were =x.y.z, and gives CLI-specific error messages in the case of invalid
/// values.
fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
    // If the version begins with character <, >, =, ^, ~ parse it as a
    // version range, otherwise parse it as a specific version
    let first = v
        .chars()
        .next()
        .ok_or_else(|| format_err!("no version provided for the `--version` flag"))?;

    if let Some(stripped) = v.strip_prefix("v") {
        bail!(
            "the version provided, `{v}` is not a valid SemVer requirement\n\n\

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Provide a non-empty crate name: `cargo install <crate>`.
  2. Check the shell variable/expansion feeding the command isn't empty.
  3. Remove stray quotes around an empty value.

Example fix

# before
CRATE=""
cargo install "$CRATE"

# after
CRATE=ripgrep
cargo install "$CRATE"
Defensive patterns

Strategy: validation

Validate before calling

// Reject empty crate names before invoking cargo install
fn nonempty_crate(s: &str) -> Result<&str, String> {
    let t = s.trim();
    if t.is_empty() { Err("crate name is empty".into()) } else { Ok(t) }
}

Type guard

fn is_nonempty_crate_name(s: &str) -> bool {
    !s.trim().is_empty()
}

Prevention

When it happens

Trigger: `cargo install ""` or a spec that reduces to empty after stripping (e.g. stray quotes/whitespace producing an empty token).

Common situations: Shell quoting bug producing an empty argument; a script variable that expanded to empty; copy-paste leaving an empty crate slot.

Related errors


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