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

  1. Drop the --lib flag if you actually want the binary: `cargo build`.
  2. Add a [lib] section to Cargo.toml if the crate should expose a library.
  3. 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

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


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)