{"id":"93534b855b834bc8","repo":"rust-lang/cargo","slug":"no-library-targets-found-in-package","errorCode":null,"errorMessage":"no library targets found in package `{}`","messagePattern":"no library targets found in package `(.+?)`","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/unit_generator.rs","lineNumber":452,"sourceCode":"                            let types = target.rustc_crate_types();\n                            let types_str: Vec<&str> = types.iter().map(|t| t.as_str()).collect();\n                            self.ws.gctx().shell().warn(format!(\n                                \"doc tests are not supported for crate type(s) `{}` in package `{}`\",\n                                types_str.join(\", \"),\n                                pkg.name()\n                            ))?;\n                        } else {\n                            libs.push(proposal)\n                        }\n                    }\n                    if !all_targets && libs.is_empty() && *lib == LibRule::True {\n                        let names = self\n                            .packages\n                            .iter()\n                            .map(|pkg| pkg.name())\n                            .collect::<Vec<_>>();\n                        if names.len() == 1 {\n                            anyhow::bail!(\"no library targets found in package `{}`\", names[0]);\n                        } else {\n                            anyhow::bail!(\n                                \"no library targets found in packages: {}\",\n                                names.join(\", \")\n                            );\n                        }\n                    }\n                    proposals.extend(libs);\n                }\n\n                let default_mode = to_compile_mode(self.intent);\n\n                // If `--tests` was specified, add all targets that would be\n                // generated by `cargo test`.\n                let test_filter = match tests {\n                    FilterRule::All => Target::tested,\n                    FilterRule::Just(_) => Target::is_test,\n                };","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_compile/unit_generator.rs#L434-L470","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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."],"exampleFix":"# before: binary-only crate, `cargo build --lib` fails\nsrc/main.rs\n\n# after: add a library target\ntouch src/lib.rs\n# Cargo.toml auto-detects src/lib.rs as the lib target","handlingStrategy":"validation","validationCode":"// Ensure the single package has a library target before --lib builds.\nfn has_lib(pkg: &cargo::core::Package) -> bool {\n    pkg.targets().iter().any(|t| t.is_lib())\n}","typeGuard":"fn is_library_pkg(pkg: &cargo::core::Package) -> bool {\n    pkg.targets().iter().any(|t| t.is_lib())\n}","tryCatchPattern":"if let Err(e) = ops::compile(ws, &opts) {\n    if e.to_string().contains(\"no library targets found\") {\n        eprintln!(\"add a [lib] target or drop --lib\");\n    }\n    return Err(e);\n}","preventionTips":["Confirm src/lib.rs exists before running `cargo build --lib`.","In CI, skip --lib for binary-only crates."],"tags":["cargo","library","manifest","target-selection","compilation"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}