rust-lang/cargo · error · anyhow::Error
crate types can only be specified for libraries and example…
Error message
crate types can only be specified for libraries and example libraries. Binaries, tests, and benchmarks are always the `bin` crate type
What it means
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.
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.
Example fix
# before cargo rustc --bin myapp --crate-type cdylib # after cargo rustc --lib --crate-type cdylib
Defensive patterns
Strategy: type-guard
Type guard
// Only lib/example-lib targets may receive crate-type overrides.
fn can_override_crate_type(kind: &TargetKind) -> bool {
matches!(kind, TargetKind::Lib(_) | TargetKind::ExampleLib(_))
} Prevention
- 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.
When it happens
Trigger: Running `cargo rustc --crate-type cdylib --bin myapp` (or --test, --bench), i.e. requesting a crate-type override on a non-library target.
Common situations: 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.
Related errors
- crate types to rustc can only be passed to one target…
- malformed output when learning about crate-type
- cannot compile ` ` as the target ` ` does not support any…
- cannot mix `proc-macro` crate type with others
- cannot produce for ` ` as the target ` ` does not support…
AI-assisted analysis of rust-lang/cargo@495c385d08 (2026-08-11).
Data as JSON: /api/errors/a419934aa0a19518.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_compile/mod.rs:1184
unit.profile.clone(),
unit.kind,
unit.mode,
unit.features.clone(),
unit.rustflags.clone(),
unit.rustdocflags.clone(),
unit.links_overrides.clone(),
unit.is_std,
unit.dep_hash,
unit.artifact,
unit.artifact_target_for_features,
unit.skip_non_compile_time_dep,
)
};
units[0] = match unit.target.kind() {
TargetKind::Lib(_) => override_unit(TargetKind::Lib),
TargetKind::ExampleLib(_) => override_unit(TargetKind::ExampleLib),
_ => {
anyhow::bail!(
"crate types can only be specified for libraries and example libraries.\n\
Binaries, tests, and benchmarks are always the `bin` crate type"
);
}
};
Ok(())
}
/// Gets all of the features enabled for a package, plus its dependencies'
/// features.
///
/// Dependencies are added as `dep_name/feat_name` because `required-features`
/// wants to support that syntax.
pub fn resolve_all_features(
resolve_with_overrides: &Resolve,
resolved_features: &features::ResolvedFeatures,
package_set: &PackageSet<'_>,View on GitHub (pinned to 495c385d08)