{"record":{"id":"783eefb04198a246","repo":"rust-lang/cargo","slug":"multiple-possible-binary-sources-found","errorCode":null,"errorMessage":"\nmultiple possible binary sources found:\n  {}\n  {}\ncannot automatically generate Cargo.toml as the main target would be ambiguous","messagePattern":"\nmultiple possible binary sources found:\n  (.+?)\n  (.+?)\ncannot automatically generate Cargo\\.toml as the main target would be ambiguous","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":342,"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":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_new.rs#L324-L360","documentation":"During `cargo init`, `detect_source_paths_and_types` scans for known source files; if two files both look like the project's main binary source for the same name (e.g. both `src/main.rs` and `main.rs`, or duplicate `src/<name>.rs` detections), cargo cannot decide which to wire into `Cargo.toml`'s main target. It bails rather than guessing, because the resulting `[[bin]] path = ...` would be ambiguous.","triggerScenarios":"`cargo init` in a directory that already contains, e.g., both `src/main.rs` and `main.rs`, or `src/<name>.rs` plus `main.rs`, producing two bin detections with the same resolved name in `duplicates_checker`.","commonSituations":"Converting an existing hand-written Rust project to cargo where multiple candidate entry points exist. Copying files around before `cargo init`. Partial migrations that leave both a root-level and `src/`-level main file.","solutions":["Delete or move one of the duplicate binary sources so only one candidate main remains (e.g. remove the top-level `main.rs`, keep `src/main.rs`).","Consolidate into the conventional layout: `src/main.rs` for a binary, `src/lib.rs` for a library.","Re-run `cargo init` after resolving the ambiguity."],"exampleFix":"# before — directory has both:\n#   ./main.rs\n#   ./src/main.rs\n$ cargo init   # error: multiple possible binary sources\n\n# after — remove one\n$ rm main.rs\n$ cargo init","handlingStrategy":"validation","validationCode":"// Before `cargo init`, ensure at most one binary source root exists.\nfn count_bin_sources(files: &[&str]) -> usize {\n    files.iter().filter(|f| matches!(**f, \"src/main.rs\" | \"main.rs\")).count()\n}\n// if count_bin_sources(&files) > 1 { ask user to remove duplicates }","typeGuard":"fn has_unique_bin_root(files: &[&str]) -> bool { count_bin_sources(files) <= 1 }","tryCatchPattern":null,"preventionTips":["Adopt the conventional single `src/main.rs` layout before running `cargo init`.","Remove stray top-level entry-point files during migration.","In scaffolding helpers, pre-scan for duplicate bin candidates and warn."],"tags":["cargo-init","source-detection","binary-targets","migration"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}