{"id":"85f48d03586947e9","repo":"rust-lang/cargo","slug":"std-has-a-lib","errorCode":null,"errorMessage":"std has a lib","messagePattern":"std has a lib","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compiler/standard_lib.rs","lineNumber":182,"sourceCode":"    std_features: &ResolvedFeatures,\n    kinds: &[&CompileKind],\n    package_set: &PackageSet<'_>,\n    interner: &UnitInterner,\n    profiles: &Profiles,\n    target_data: &RustcTargetData<'_>,\n) -> CargoResult<()> {\n    let std_ids = std_crates(crates, default, units)\n        .iter()\n        .map(|crate_name| std_resolve.query(crate_name))\n        .collect::<CargoResult<Vec<PackageId>>>()?;\n    let std_pkgs = package_set.get_many(std_ids)?;\n\n    for pkg in std_pkgs {\n        let lib = pkg\n            .targets()\n            .iter()\n            .find(|t| t.is_lib())\n            .expect(\"std has a lib\");\n        // I don't think we need to bother with Check here, the difference\n        // in time is minimal, and the difference in caching is\n        // significant.\n        let mode = CompileMode::Build;\n        let features = std_features.activated_features(pkg.package_id(), FeaturesFor::NormalOrDev);\n        for kind in kinds {\n            let kind = **kind;\n            let list = ret.entry(kind).or_insert_with(Vec::new);\n            let unit_for = UnitFor::new_normal(kind);\n            let profile = profiles.get_profile(\n                pkg.package_id(),\n                /*is_member*/ false,\n                /*is_local*/ false,\n                unit_for,\n                kind,\n            );\n            list.push(interner.intern(\n                pkg,","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/compiler/standard_lib.rs#L164-L200","documentation":"While generating std root units, `generate_std_roots` finds each package's library target via `.find(|t| t.is_lib()).expect(\"std has a lib\")`. Every std-workspace crate that Cargo asks to build is expected to expose a `lib` target. The panic means a requested std crate has no library target — it was asked for by name but contributes only a bin/example/etc.","triggerScenarios":"Passing `-Zbuild-std=<name>` where `<name>` is a std-workspace crate without a lib target (e.g. a tools/binary crate), or a rust-src layout where the `Cargo.toml` lacks a `[lib]`/auto-detected lib; a Cargo/std-source version mismatch where a crate's target set changed.","commonSituations":"Hand-tuning `-Zbuild-std` to include non-library crates; pointing `__CARGO_TESTS_ONLY_SRC_ROOT` at a modified std source tree; mismatched rust-src and Cargo expectations on nightly.","solutions":["Restrict `-Zbuild-std` to library crates of the std workspace (typically `std`, `core`, `alloc`, `proc_macro`, `panic_abort`, `panic_unwind`, `compiler_builtins`).","`rustup component add rust-src` and use the matching nightly Cargo.","If you maintain a patched rust-src, ensure each requested crate has a `[lib]` target."],"exampleFix":"// before\nlet lib = pkg\n    .targets()\n    .iter()\n    .find(|t| t.is_lib())\n    .expect(\"std has a lib\");\n// after\nlet lib = pkg\n    .targets()\n    .iter()\n    .find(|t| t.is_lib())\n    .ok_or_else(|| anyhow::anyhow!(\"std crate `{}` has no lib target\", pkg.package_id()))?;","handlingStrategy":"validation","validationCode":"// Confirm each requested -Zbuild-std crate has a lib target in the std source tree\nfn crate_has_lib(std_src: &std::path::Path, name: &str) -> bool {\n    std::fs::read_to_string(std_src.join(name).join(\"Cargo.toml\"))\n        .map(|t| t.contains(\"[lib]\") || !t.contains(\"[[bin]]\"))\n        .unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict `-Zbuild-std` to library crates of the std workspace.","Use matching nightly Cargo + `rustup component add rust-src`.","If patching rust-src, ensure every requested crate defines a `[lib]`."],"tags":["cargo","build-std","std","targets","internal-invariant"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}