rust-lang/cargo · error · anyhow::Error
no library targets found in packages: {}
Error message
no library targets found in packages: {} What it means
Same condition as error 89 but the multi-package branch (src/ops/cargo_compile/unit_generator.rs:454-457): multiple selected packages collectively lack any library target while LibRule::True is required. The names of all offending packages are joined.
Source
Thrown at src/ops/cargo_compile/unit_generator.rs:454
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,
};
let test_mode = match self.intent {
UserIntent::Build => CompileMode::Test,View on GitHub (pinned to 0e07a15537)
Solutions
- Inspect the listed package names; whichever should be a library needs a [lib] (src/lib.rs).
- Remove --lib and target binaries/tests explicitly.
- Narrow -p to the single package that actually has a lib.
Example fix
# before cargo build --lib -p app-a -p app-b # neither has a [lib] # after cargo build --lib -p shared-core # the crate that actually has a lib
Defensive patterns
Strategy: validation
Validate before calling
// For a multi-package --lib selection, ensure at least one member has a lib.
fn any_lib_among(ws: &Workspace, names: &[String]) -> bool {
ws.members()
.filter(|m| names.iter().any(|n| n == m.name().as_str()))
.any(|m| m.targets().iter().any(|t| t.is_lib()))
} Type guard
fn selection_has_lib(pkgs: &[&cargo::core::Package]) -> bool {
pkgs.iter().flat_map(|p| p.targets()).any(|t| t.is_lib())
} Try / catch
if let Err(e) = ops::compile(ws, &opts) {
if e.to_string().contains("no library targets found in packages") {
eprintln!("none of the selected packages has a [lib]; drop --lib or pick a lib crate");
}
return Err(e);
} Prevention
- For --workspace --lib, ensure at least one crate exposes a library.
- Narrow -p to the crate that owns the lib.
When it happens
Trigger: `cargo build --lib --workspace` (or -p across several crates) where none of the selected packages expose a [lib]. libs stays empty across all packages and LibRule::True holds.
Common situations: A workspace of binary-only crates where `--lib` was passed by mistake. Selecting several app crates expecting a shared lib that was moved or deleted.
Related errors
- no library targets found in package `{}`
- package `{}` cannot be tested because it requires dev-depend
- crate types can only be specified for libraries and example
- {}package(s) `{}` not found in workspace `{}`
- no {target_desc} target {named} `{target_name}`{unmatched_pa
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/14ae698d639d0108.json.
Report an issue: GitHub.