{"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":"validation","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/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_compile/mod.rs#L1166-L1202","documentation":"Thrown by override_rustc_crate_types (src/ops/cargo_compile/mod.rs:1180-1189) when `cargo rustc --crate-type` targets a unit whose TargetKind is neither Lib nor ExampleLib. Binaries, tests, and benchmarks are always the `bin` crate type and cannot be overridden.","triggerScenarios":"Calling `cargo rustc --crate-type cdylib --bin foo` (or against a test/bench target). The match on unit.target.kind() falls into the `_` arm and bails.","commonSituations":"Assuming --crate-type works on any target. Trying to build a cdylib from a binary target instead of the library target. Migrating a bin-only crate and pointing --crate-type at the wrong target.","solutions":["Apply --crate-type to the library target instead: `cargo rustc --lib --crate-type cdylib`.","If you need a cdylib, add a [lib] section with `crate-type = [\"cdylib\"]` in Cargo.toml rather than overriding a bin.","Remove --crate-type from a bin/test/bench invocation."],"exampleFix":"# before\ncargo rustc --bin app --crate-type cdylib   # error: bins are always `bin`\n\n# after\ncargo rustc --lib --crate-type cdylib\n# or in Cargo.toml\n[lib]\ncrate-type = [\"cdylib\"]","handlingStrategy":"validation","validationCode":"// Confirm the selected unit is a Lib or ExampleLib before applying --crate-type.\nfn crate_type_applicable(unit: &Unit) -> bool {\n    matches!(unit.target.kind(),\n        cargo::core::TargetKind::Lib(_) | cargo::core::TargetKind::ExampleLib(_))\n}","typeGuard":"fn is_overridable_kind(t: &cargo::core::Target) -> bool {\n    t.is_lib() || t.is_example()\n}","tryCatchPattern":"if let Err(e) = ops::compile(ws, &opts) {\n    if e.to_string().contains(\"crate types can only be specified\") {\n        eprintln!(\"apply --crate-type to a [lib] or example-library target instead\");\n    }\n    return Err(e);\n}","preventionTips":["Prefer declaring `crate-type` in [lib] over using --crate-type on bins.","Remember bins/tests/benches are always the `bin` crate type."],"tags":["cargo","crate-type","manifest","compilation","target-selection"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}