{"id":"baa0a75e2b6ed37b","repo":"rust-lang/cargo","slug":"multiple-possible-binary-sources-found","errorCode":null,"errorMessage":"multiple possible binary sources found:\n  {}\n  {}\ncannot automatically generate Cargo.toml as the main target would be ambiguous","messagePattern":"multiple possible binary sources found:\n  (.+?)\n  (.+?)\ncannot automatically generate Cargo\\.toml as the main target would be ambiguous","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":393,"sourceCode":"                let isbin = content.contains(\"fn main\");\n                SourceFileInformation {\n                    relative_path: pp,\n                    bin: isbin,\n                }\n            }\n        };\n        detected_files.push(sfi);\n    }\n\n    // Check for duplicate lib attempt\n\n    let mut previous_lib_relpath: Option<&str> = None;\n    let mut duplicates_checker: BTreeMap<&str, &SourceFileInformation> = BTreeMap::new();\n\n    for i in detected_files {\n        if i.bin {\n            if let Some(x) = BTreeMap::get::<str>(&duplicates_checker, &name) {\n                anyhow::bail!(\n                    \"\\\nmultiple possible binary sources found:\n  {}\n  {}\ncannot automatically generate Cargo.toml as the main target would be ambiguous\",\n                    &x.relative_path,\n                    &i.relative_path\n                );\n            }\n            duplicates_checker.insert(name, i);\n        } else {\n            if let Some(plp) = previous_lib_relpath {\n                anyhow::bail!(\n                    \"cannot have a package with \\\n                     multiple libraries, \\\n                     found both `{}` and `{}`\",\n                    plp,\n                    i.relative_path","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_new.rs#L375-L411","documentation":"During `cargo init`, detectSourcePaths scans candidate source files (src/main.rs, src/bin/<name>.rs, etc.) and, when more than one of them is classified as a binary entry point, Cargo cannot decide which one is the canonical [[bin]] target. Because auto-generating Cargo.toml would create an ambiguous main target, the operation aborts and asks the user to disambiguate.","triggerScenarios":"Run `cargo init` in a directory that already contains BOTH `src/main.rs` and `src/bin/<pkgname>.rs` (or two src/bin/*.rs files matching the package name). The duplicate-binary branch at cargo_new.rs:391-403 fires.","commonSituations":"Converting an existing Rust source tree (cloned without Cargo.toml) that already has both src/main.rs and src/bin/foo.rs; partial migration from another build system; leftover scaffolding files from a previous attempt.","solutions":["Delete or rename one of the conflicting binary files so only a single main entry point remains","Manually write a Cargo.toml that lists each binary explicitly under [[bin]] with distinct names/paths, then run `cargo init` is unnecessary (or skip init)","Run `cargo init --name <x>` only after collapsing to one binary source"],"exampleFix":"# before: src/main.rs AND src/bin/foo.rs both present\ncargo init\n# after: keep one, remove the other\nrm src/bin/foo.rs   # then\ncargo init","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn count_binary_sources(dir: &Path, pkg_name: &str) -> usize {\n    let mut n = 0;\n    if dir.join(\"src/main.rs\").is_file() { n += 1; }\n    if dir.join(format!(\"src/bin/{pkg_name}.rs\")).is_file() { n += 1; }\n    // also count src/bin/*.rs that equal pkg_name\n    n\n}\n\nif count_binary_sources(dir, name) > 1 { /* tell user to disambiguate before cargo init */ }","typeGuard":"import { existsSync } from 'fs';\nfunction hasSingleBinaryRoot(dir: string, name: string): boolean {\n  const main = existsSync(`${dir}/src/main.rs`);\n  const bin = existsSync(`${dir}/src/bin/${name}.rs`);\n  return !(main && bin);\n}","tryCatchPattern":null,"preventionTips":["Before `cargo init`, ensure only one of src/main.rs vs src/bin/<name>.rs exists","Adopt a consistent layout convention (single binary lives at src/main.rs)","In generators, never emit both files"],"tags":["cargo-init","binary-target","ambiguity","source-detection"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}