{"record":{"id":"e6275e85b54a57f6","repo":"rust-lang/cargo","slug":"found-duplicate-target-kind-name-name-but-all","errorCode":null,"errorMessage":"found duplicate {target_kind} name {name}, but all {target_kind} targets must have a unique name","messagePattern":"found duplicate (.+?) name (.+?), but all (.+?) targets must have a unique name","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/workspace/parser/targets.rs","lineNumber":894,"sourceCode":"}\n\nfn inferred_to_toml_targets(inferred: &[(String, PathBuf)]) -> Vec<TomlTarget> {\n    inferred\n        .iter()\n        .map(|(name, path)| TomlTarget {\n            name: Some(name.clone()),\n            path: Some(PathValue(path.clone())),\n            ..TomlTarget::new()\n        })\n        .collect()\n}\n\n/// Will check a list of toml targets, and make sure the target names are unique within a vector.\nfn validate_unique_names(targets: &[TomlTarget], target_kind: &str) -> CargoResult<()> {\n    let mut seen = HashSet::default();\n    for name in targets.iter().map(|e| name_or_panic(e)) {\n        if !seen.insert(name) {\n            anyhow::bail!(\n                \"found duplicate {target_kind} name {name}, \\\n                 but all {target_kind} targets must have a unique name\",\n                target_kind = target_kind,\n                name = name\n            );\n        }\n    }\n    Ok(())\n}\n\n/// Will check a list of build scripts, and make sure script file stems are unique within a vector.\nfn validate_unique_build_scripts(scripts: &[String]) -> CargoResult<()> {\n    let mut seen = HashMap::default();\n    for script in scripts {\n        let stem = Path::new(script)\n            .file_stem()\n            .and_then(|s| s.to_str())\n            .expect(\"previously normalized\");","sourceCodeStart":876,"sourceCodeEnd":912,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/workspace/parser/targets.rs#L876-L912","documentation":"Within a single target kind (all `[[bin]]`, all `[[bench]]`, all `[[test]]`, or all `[[example]]` tables), each declared target must have a unique name. `validate_unique_names` walks the list, inserting names into a `HashSet`, and bails on the first collision. Duplicate names would produce ambiguous build artifacts.","triggerScenarios":"Two (or more) tables of the same kind declare an identical `name`. E.g. two `[[bin]]` entries both with `name = \"server\"`. The `!seen.insert(name)` check at targets.rs:899 fails.","commonSituations":"Copy-pasting a `[[bin]]` block and forgetting to rename. Merging manifests during a refactor. Automated generation that appends targets without dedup.","solutions":["Rename one of the colliding targets so each `name` is unique within its kind.","If the duplicate is unintended, delete the extra table.","Run `cargo metadata` to confirm target names are now distinct."],"exampleFix":"# before\n[[bin]]\nname = \"server\"\npath = \"src/bin/server_a.rs\"\n\n[[bin]]\nname = \"server\"\npath = \"src/bin/server_b.rs\"\n\n# after\n[[bin]]\nname = \"server_a\"\npath = \"src/bin/server_a.rs\"\n\n[[bin]]\nname = \"server_b\"\npath = \"src/bin/server_b.rs\"","handlingStrategy":"validation","validationCode":"use std::collections::HashSet;\nfn unique_target_names(targets: &[(String, String)]) -> bool {\n    // targets: Vec<(kind, name)>\n    let mut seen = HashSet::new();\n    targets.iter().all(|(kind, name)| seen.insert((kind.clone(), name.clone())))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Deduplicate target tables after merging manifests.","Use distinct, descriptive names for each target.","Lint for duplicate names within each kind in CI."],"tags":["cargo","config","targets","validation","manifest"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}