rust-lang/cargo · error

target names cannot be empty

Error message

{} target names cannot be empty

What it means

When a target's `name` field is present, it must not be empty or whitespace-only. `validate_target_name` trims the name and bails at targets.rs:1184 if `name.trim().is_empty()`. An empty name cannot map to a file or artifact.

Solutions

  1. Provide a non-empty, non-whitespace `name`.
  2. Remove the `name` field only if the target is auto-inferred (not applicable to explicitly listed `[[bin]]` etc.).
  3. Add a pre-commit manifest validator that rejects empty names.

Example fix

# before
[[bin]]
name = ""
path = "src/main.rs"

# after
[[bin]]
name = "myapp"
path = "src/main.rs"
Defensive patterns

Strategy: validation

Validate before calling

fn name_non_empty(name: &str) -> bool {
    !name.trim().is_empty()
}

Prevention

When it happens

Trigger: `[[bin]] name = ""` or `name = " "`. The `Some(ref name)` arm with the trimmed-empty check fires.

Common situations: Placeholder names from code generation. YAML/TOML merge tools that collapse values to empty. Accidentally clearing a name during a refactor.

Related errors


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

Appendix: source

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

        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)
            }
            if cfg!(windows) && restricted_names::is_windows_reserved(name) {
                warnings.push(format!(
                    "{} target `{}` is a reserved Windows filename, \
                        this target will not work on Windows platforms",
                    target_kind_human, name
                ));
            }
        }
        None => anyhow::bail!(
            "{} target {}.name is required",
            target_kind_human,
            target_kind
        ),
    }

    Ok(())
}

View on GitHub (pinned to eb98b54bc9)