rust-lang/cargo · error

cannot have a package with multiple libraries, found both

Error message

cannot have a package with multiple libraries, found both `{}` and `{}`

What it means

Inside `detect_source_paths_and_types`, if more than one file is detected as a library source (e.g. both `src/lib.rs` and `lib.rs`, or `src/<name>.rs` lib-detections), cargo refuses: a package may have at most one library target. `previous_lib_relpath` tracks the first lib; a second triggers the bail naming both relative paths.

Solutions

  1. Keep exactly one library root — conventionally `src/lib.rs` — and remove/relocate the other.
  2. If the second file is a module, move it under `src/` (e.g. `src/extra.rs`) and `mod extra;` rather than leaving it as a stray lib root.
  3. Re-run `cargo init` once the layout has a single lib.

Example fix

# before — directory has both:
#   ./lib.rs
#   ./src/lib.rs
$ cargo init   # error: multiple libraries

# after
$ rm lib.rs
$ cargo init
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: `cargo init` in a directory containing both `src/lib.rs` and `lib.rs`, or multiple files that the detection rules classify as library roots.

Common situations: Migrating a project that keeps lib sources in two locations. Leftover files from a refactor. Copying a library crate's contents into a directory that already has a `src/lib.rs`.

Related errors


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

Appendix: source

Thrown at src/ops/cargo_new.rs:355

    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
                )
            }
            previous_lib_relpath = Some(&i.relative_path);
        }
    }

    Ok(())
}

fn plan_new_source_file(bin: bool) -> SourceFileInformation {
    if bin {
        SourceFileInformation {
            relative_path: "src/main.rs".to_string(),

View on GitHub (pinned to 98a09e7e7d)