rust-lang/cargo · error

multiple possible binary sources found

Error message

multiple possible binary sources found:
  {}
  {}
cannot automatically generate Cargo.toml as the main target would be ambiguous

What it means

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.

Solutions

  1. 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`).
  2. Consolidate into the conventional layout: `src/main.rs` for a binary, `src/lib.rs` for a library.
  3. Re-run `cargo init` after resolving the ambiguity.

Example fix

# before — directory has both:
#   ./main.rs
#   ./src/main.rs
$ cargo init   # error: multiple possible binary sources

# after — remove one
$ rm main.rs
$ cargo init
Defensive patterns

Strategy: validation

Validate before calling

// Before `cargo init`, ensure at most one binary source root exists.
fn count_bin_sources(files: &[&str]) -> usize {
    files.iter().filter(|f| matches!(**f, "src/main.rs" | "main.rs")).count()
}
// if count_bin_sources(&files) > 1 { ask user to remove duplicates }

Type guard

fn has_unique_bin_root(files: &[&str]) -> bool { count_bin_sources(files) <= 1 }

Prevention

When it happens

Trigger: `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`.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/ops/cargo_new.rs:342

                let isbin = content.contains("fn main");
                SourceFileInformation {
                    relative_path: pp,
                    bin: isbin,
                }
            }
        };
        detected_files.push(sfi);
    }

    // Check for duplicate lib attempt

    let mut previous_lib_relpath: Option<&str> = None;
    let mut duplicates_checker: BTreeMap<&str, &SourceFileInformation> = BTreeMap::new();

    for i in detected_files {
        if i.bin {
            if let Some(x) = BTreeMap::get::<str>(&duplicates_checker, &name) {
                anyhow::bail!(
                    "\
multiple possible binary sources found:
  {}
  {}
cannot automatically generate Cargo.toml as the main target would be ambiguous",
                    &x.relative_path,
                    &i.relative_path
                );
            }
            duplicates_checker.insert(name, i);
        } else {
            if let Some(plp) = previous_lib_relpath {
                anyhow::bail!(
                    "cannot have a package with \
                     multiple libraries, \
                     found both `{}` and `{}`",
                    plp,
                    i.relative_path

View on GitHub (pinned to 98a09e7e7d)