rust-lang/cargo · error · anyhow::Error
crate types to rustc can only be passed to one target…
Error message
crate types to rustc can only be passed to one target, consider filtering the package by passing, e.g., `--lib` or `--example` to specify a single target
What it means
Thrown by override_rustc_crate_types (`cargo rustc --crate-type ...`) when the unit slice length is not exactly 1. Crate-type overrides apply to a single target, so ambiguity (zero or many units) is rejected up front before any target kind is rewritten.
Solutions
- Add a target selector to choose exactly one unit: `--lib`, `--example NAME`, `--bin NAME`.
- Narrow the package: `-p <pkg> --lib --crate-type cdylib`.
- Confirm the target you expect is actually defined in the manifest.
Example fix
# before cargo rustc --crate-type cdylib --release # after cargo rustc --lib --crate-type cdylib --release
Defensive patterns
Strategy: validation
Validate before calling
// Ensure exactly one target selector before `--crate-type`.
fn exactly_one_target(args: &[String]) -> bool {
args.iter().filter(|a| a.starts_with("--lib") || a.starts_with("--bin") || a.starts_with("--example")).count() == 1
} Prevention
- Always pass a single-target selector with `--crate-type`.
- Document which target is intended when scripting cargo rustc overrides.
- Avoid `--crate-type` on workspaces; scope with -p and --lib/--example.
When it happens
Trigger: Running `cargo rustc --crate-type cdylib` without a target filter when the package has multiple buildable units (lib+bin, multiple bins), or when no unit is selected at all.
Common situations: Forgetting `--lib`/`--example NAME` when experimenting with crate types; running --crate-type on a workspace default that resolves to several roots; CI invoking cargo rustc with extra args on a multi-target package.
Related errors
- crate types can only be specified for libraries and example…
- extra arguments to ` ` can only be passed to one target…
- malformed output when learning about crate-type
- no library targets found in package
- no library targets found in packages
AI-assisted analysis of rust-lang/cargo@495c385d08 (2026-08-11).
Data as JSON: /api/errors/b54ab499fa4e4f73.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_compile/mod.rs:1151
visit(&dep.unit, graph, visited);
}
}
for unit in root_units {
visit(unit, unit_graph, &mut visited);
}
unit_graph.retain(|unit, _| visited.contains(unit));
}
/// Override crate types for given units.
///
/// This is primarily used by `cargo rustc --crate-type`.
fn override_rustc_crate_types(
units: &mut [Unit],
args: &[String],
interner: &UnitInterner,
) -> CargoResult<()> {
if units.len() != 1 {
anyhow::bail!(
"crate types to rustc can only be passed to one \
target, consider filtering\nthe package by passing, \
e.g., `--lib` or `--example` to specify a single target"
);
}
let unit = &units[0];
let override_unit = |f: fn(Vec<CrateType>) -> TargetKind| {
let crate_types = args.iter().map(|s| s.into()).collect();
let mut target = unit.target.clone();
target.set_kind(f(crate_types));
interner.intern(
&unit.pkg,
&target,
unit.profile.clone(),
unit.kind,
unit.mode,
unit.features.clone(),View on GitHub (pinned to 495c385d08)