rust-lang/cargo · error

no library targets found in packages

Error message

no library targets found in packages: {}

What it means

Plural-form sibling of errors 75/76: same condition (LibRule::True, libs empty, not all_targets) but multiple packages are in scope, so cargo lists all of them. It indicates that NONE of the selected packages expose a library target.

Solutions

  1. Drop the --lib flag, or scope to the package that has a library: `-p <lib-pkg>`.
  2. Add [lib] sections to the crates that should expose libraries.
  3. Adjust workspace default-members so library-needing intents include a lib crate.

Example fix

# before
cargo build --lib  # workspace of bin crates
# after
cargo build -p shared-lib --lib
Defensive patterns

Strategy: validation

Validate before calling

// For workspace-wide lib intents, ensure at least one member has a lib.
fn any_lib(members: &[Package]) -> bool {
    members.iter().flat_map(|p| &p.targets).any(|t| matches!(t.kind, TargetKind::Lib(_)))
}

Prevention

When it happens

Trigger: Running `cargo build --lib` (or a workspace-wide doc/test intent needing libs) across a workspace where no member has a [lib] target; selecting several binary-only packages.

Common situations: Workspace of binary-only crates; misconfigured CI invoking --lib across the workspace; default-members set to bin-only packages.

Related errors


AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11). Data as JSON: /api/errors/14ae698d639d0108. Report an issue: GitHub.

Appendix: 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 eb98b54bc9)