rust-lang/cargo · error · anyhow::Error

multiple possible binary sources found: {} {} cannot aut

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`, 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.

Source

Thrown at src/ops/cargo_new.rs:393

                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 0e07a15537)

Solutions

  1. Delete or rename one of the conflicting binary files so only a single main entry point remains
  2. 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)
  3. Run `cargo init --name <x>` only after collapsing to one binary source

Example fix

# before: src/main.rs AND src/bin/foo.rs both present
cargo init
# after: keep one, remove the other
rm src/bin/foo.rs   # then
cargo init
Defensive patterns

Strategy: validation

Validate before calling

use std::path::Path;
fn count_binary_sources(dir: &Path, pkg_name: &str) -> usize {
    let mut n = 0;
    if dir.join("src/main.rs").is_file() { n += 1; }
    if dir.join(format!("src/bin/{pkg_name}.rs")).is_file() { n += 1; }
    // also count src/bin/*.rs that equal pkg_name
    n
}

if count_binary_sources(dir, name) > 1 { /* tell user to disambiguate before cargo init */ }

Type guard

import { existsSync } from 'fs';
function hasSingleBinaryRoot(dir: string, name: string): boolean {
  const main = existsSync(`${dir}/src/main.rs`);
  const bin = existsSync(`${dir}/src/bin/${name}.rs`);
  return !(main && bin);
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/baa0a75e2b6ed37b.json. Report an issue: GitHub.