rust-lang/cargo · error
target .name is required
Error message
{} target {}.name is required What it means
For explicitly declared targets (`[[bin]]`, `[[test]]`, `[[bench]]`, `[[example]]`), the `name` field is mandatory — there is no inference for listed tables. `validate_target_name` hits the `None` arm at targets.rs:1190-1194 and bails. (Auto-inferred targets like the default `src/main.rs` bin bypass this path.)
Solutions
- Add an explicit `name = "..."` to the target table.
- Alternatively, rely on file-based discovery (place the binary at `src/bin/foo.rs`) instead of a `[[bin]]` table.
Example fix
# before [[bin]] path = "src/server.rs" # after [[bin]] name = "server" path = "src/server.rs"
Defensive patterns
Strategy: validation
Validate before calling
fn explicit_targets_have_name(tables: &[Option<&str>]) -> bool {
tables.iter().all(|t| t.is_some())
}
// every [[bin]]/[[test]]/[[bench]]/[[example]] table must carry a `name` Prevention
- Always set `name` on explicitly listed target tables.
- Prefer file-based discovery (`src/bin/foo.rs`) when name inference is wanted.
- Lint manifests for nameless target tables.
When it happens
Trigger: A `[[bin]]` (or `[[test]]`/`[[bench]]`/`[[example]]`) table with no `name` key. The `match target.name` falls through to `None`.
Common situations: Adding a `[[bin]]` table to set only `path` and forgetting `name`. Truncated copy-paste of a target block. Tutorials that omit the name field.
Related errors
- found duplicate name , but all targets must have a unique…
- target names cannot be empty
- found build scripts with duplicate file stems, but all…
- invalid `package.build` file name
- can't find library ` `, rename file to `src/lib.rs` or…
AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11).
Data as JSON: /api/errors/f09ae8e64de87448.
Report an issue: GitHub.
Appendix: source
Thrown at src/workspace/parser/targets.rs:1190
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(())
}
fn validate_bin_proc_macro(
target: &TomlTarget,
edition: Edition,
warnings: &mut Vec<String>,
errors: &mut Vec<String>,
) -> CargoResult<()> {
if target.proc_macro() == Some(true) {
let name = name_or_panic(target);
errors.push(format!(View on GitHub (pinned to eb98b54bc9)