{"record":{"id":"a419934aa0a19518","repo":"rust-lang/cargo","slug":"crate-types-can-only-be-specified-for-libraries-an","errorCode":null,"errorMessage":"crate types can only be specified for libraries and example libraries.\nBinaries, tests, and benchmarks are always the `bin` crate type","messagePattern":"crate types can only be specified for libraries and example libraries\\.\nBinaries, tests, and benchmarks are always the `bin` crate type","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/mod.rs","lineNumber":1184,"sourceCode":"            unit.profile.clone(),\n            unit.kind,\n            unit.mode,\n            unit.features.clone(),\n            unit.rustflags.clone(),\n            unit.rustdocflags.clone(),\n            unit.links_overrides.clone(),\n            unit.is_std,\n            unit.dep_hash,\n            unit.artifact,\n            unit.artifact_target_for_features,\n            unit.skip_non_compile_time_dep,\n        )\n    };\n    units[0] = match unit.target.kind() {\n        TargetKind::Lib(_) => override_unit(TargetKind::Lib),\n        TargetKind::ExampleLib(_) => override_unit(TargetKind::ExampleLib),\n        _ => {\n            anyhow::bail!(\n                \"crate types can only be specified for libraries and example libraries.\\n\\\n                Binaries, tests, and benchmarks are always the `bin` crate type\"\n            );\n        }\n    };\n\n    Ok(())\n}\n\n/// Gets all of the features enabled for a package, plus its dependencies'\n/// features.\n///\n/// Dependencies are added as `dep_name/feat_name` because `required-features`\n/// wants to support that syntax.\npub fn resolve_all_features(\n    resolve_with_overrides: &Resolve,\n    resolved_features: &features::ResolvedFeatures,\n    package_set: &PackageSet<'_>,","sourceCodeStart":1166,"sourceCodeEnd":1202,"githubUrl":"https://github.com/rust-lang/cargo/blob/495c385d0875c4ba51eb72ea0448a2d4c018b8d4/src/ops/cargo_compile/mod.rs#L1166-L1202","documentation":"Thrown by override_rustc_crate_types after the single-unit check passed, but that unit's TargetKind is neither Lib nor ExampleLib. Binaries, tests, and benchmarks are always the `bin` crate type, so overriding crate types on them is meaningless and rejected.","triggerScenarios":"Running `cargo rustc --crate-type cdylib --bin myapp` (or --test, --bench), i.e. requesting a crate-type override on a non-library target.","commonSituations":"Misunderstanding that only libraries and example libraries are crate-type overridable; trying to produce a cdylib from a binary target instead of a lib target.","solutions":["Apply --crate-type to the library target instead: `cargo rustc --lib --crate-type cdylib`.","Add a dedicated [lib] or [[example]] with crate-type = [...] in Cargo.toml rather than overriding a bin.","Drop the --crate-type flag if you actually want to build the existing bin/test/bench."],"exampleFix":"# before\ncargo rustc --bin myapp --crate-type cdylib\n# after\ncargo rustc --lib --crate-type cdylib","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Only lib/example-lib targets may receive crate-type overrides.\nfn can_override_crate_type(kind: &TargetKind) -> bool {\n    matches!(kind, TargetKind::Lib(_) | TargetKind::ExampleLib(_))\n}","tryCatchPattern":null,"preventionTips":["Reserve --crate-type for `--lib` or `--example NAME` targets only.","Define a dedicated [lib] with crate-type in the manifest for cdylib/dylib outputs.","Remember bin/test/bench are always the `bin` crate type."],"tags":["cargo-compile","crate-type","target-kind","rustc"],"backgroundTag":null,"analyzedSha":"495c385d0875c4ba51eb72ea0448a2d4c018b8d4","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}