rust-lang/cargo · error · anyhow::Error
no library targets found in package `{}`
Error message
no library targets found in package `{}` What it means
Thrown in proposals_to_units (src/ops/cargo_compile/unit_generator.rs:445-458) when a single selected package is required to provide a library target (LibRule::True) and not in all-targets mode, but the package has no [lib] target. The single-package branch formats the package name.
Source
Thrown at src/ops/cargo_compile/unit_generator.rs:452
let types = target.rustc_crate_types();
let types_str: Vec<&str> = types.iter().map(|t| t.as_str()).collect();
self.ws.gctx().shell().warn(format!(
"doc tests are not supported for crate type(s) `{}` in package `{}`",
types_str.join(", "),
pkg.name()
))?;
} else {
libs.push(proposal)
}
}
if !all_targets && libs.is_empty() && *lib == LibRule::True {
let names = self
.packages
.iter()
.map(|pkg| pkg.name())
.collect::<Vec<_>>();
if names.len() == 1 {
anyhow::bail!("no library targets found in package `{}`", names[0]);
} else {
anyhow::bail!(
"no library targets found in packages: {}",
names.join(", ")
);
}
}
proposals.extend(libs);
}
let default_mode = to_compile_mode(self.intent);
// If `--tests` was specified, add all targets that would be
// generated by `cargo test`.
let test_filter = match tests {
FilterRule::All => Target::tested,
FilterRule::Just(_) => Target::is_test,
};View on GitHub (pinned to 0e07a15537)
Solutions
- Add a [lib] section (or ensure src/lib.rs exists) if the crate should be a library.
- Drop the --lib flag and build the intended target (e.g. --bin).
- Verify you targeted the right package with -p.
Example fix
# before: binary-only crate, `cargo build --lib` fails src/main.rs # after: add a library target touch src/lib.rs # Cargo.toml auto-detects src/lib.rs as the lib target
Defensive patterns
Strategy: validation
Validate before calling
// Ensure the single package has a library target before --lib builds.
fn has_lib(pkg: &cargo::core::Package) -> bool {
pkg.targets().iter().any(|t| t.is_lib())
} Type guard
fn is_library_pkg(pkg: &cargo::core::Package) -> bool {
pkg.targets().iter().any(|t| t.is_lib())
} Try / catch
if let Err(e) = ops::compile(ws, &opts) {
if e.to_string().contains("no library targets found") {
eprintln!("add a [lib] target or drop --lib");
}
return Err(e);
} Prevention
- Confirm src/lib.rs exists before running `cargo build --lib`.
- In CI, skip --lib for binary-only crates.
When it happens
Trigger: `cargo build --lib` (or any lib-requiring intent like doc/check on the lib) on a package whose Cargo.toml has no [lib] section and no default src/lib.rs. LibRule::True with libs.is_empty() triggers the bail.
Common situations: A binary-only crate (src/main.rs only) where someone runs `cargo doc --lib` or `cargo build --lib`. A crate renamed to drop its lib. Missing src/lib.rs file in a crate that was expected to be a library.
Related errors
- crate types can only be specified for libraries and example
- no library targets found in packages: {}
- extra arguments to `{}` can only be passed to one target, co
- could not compile due to {error_count} previous target resol
- crate types to rustc can only be passed to one target, consi
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/93534b855b834bc8.json.
Report an issue: GitHub.