rust-lang/cargo · error · anyhow::Error
no library targets found in package
Error message
no library targets found in package `{}` What it means
Thrown by unit_generator when LibRule::True requires a library target but none of the selected packages expose one, and exactly one package is in scope (singular message form). This happens when the user asks cargo to build a library (e.g. `cargo build --lib`) for a package that has no [lib] target.
Solutions
- Drop the --lib flag if you actually want the binary: `cargo build`.
- Add a [lib] section to Cargo.toml if the crate should expose a library.
- Point the build at the correct package that does have a lib: `-p <lib-pkg>`.
Example fix
# before cargo build --lib # after (binary-only crate) cargo build --bin myapp
Defensive patterns
Strategy: validation
Validate before calling
// Check the package has a [lib] target before --lib builds. // cargo metadata -> packages[].targets[].kind contains "lib" / "rlib" etc.
Prevention
- Don't pass --lib to binary-only crates.
- Add a [lib] section when a library target is genuinely needed.
- Select the correct package with -p when running library intents.
When it happens
Trigger: Running `cargo build --lib` (or `--tests`/doc which need a lib) on a package whose Cargo.toml defines only [[bin]] targets; a binary-only crate being asked for its library.
Common situations: Binary-only application crates mistakenly built with --lib; crates whose [lib] was removed; tooling/IDE that defaults to --lib.
Related errors
- no library targets found in packages
- no target
- can't find library ` `, rename file to `src/lib.rs` or…
- could not compile due to
- crate types to rustc can only be passed to one target…
AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11).
Data as JSON: /api/errors/93534b855b834bc8.
Report an issue: GitHub.
Appendix: 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 eb98b54bc9)