{"id":"b54ab499fa4e4f73","repo":"rust-lang/cargo","slug":"crate-types-to-rustc-can-only-be-passed-to-one-tar","errorCode":null,"errorMessage":"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","messagePattern":"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","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/mod.rs","lineNumber":1151,"sourceCode":"            visit(&dep.unit, graph, visited);\n        }\n    }\n    for unit in root_units {\n        visit(unit, unit_graph, &mut visited);\n    }\n    unit_graph.retain(|unit, _| visited.contains(unit));\n}\n\n/// Override crate types for given units.\n///\n/// This is primarily used by `cargo rustc --crate-type`.\nfn override_rustc_crate_types(\n    units: &mut [Unit],\n    args: &[String],\n    interner: &UnitInterner,\n) -> CargoResult<()> {\n    if units.len() != 1 {\n        anyhow::bail!(\n            \"crate types to rustc can only be passed to one \\\n            target, consider filtering\\nthe package by passing, \\\n            e.g., `--lib` or `--example` to specify a single target\"\n        );\n    }\n\n    let unit = &units[0];\n    let override_unit = |f: fn(Vec<CrateType>) -> TargetKind| {\n        let crate_types = args.iter().map(|s| s.into()).collect();\n        let mut target = unit.target.clone();\n        target.set_kind(f(crate_types));\n        interner.intern(\n            &unit.pkg,\n            &target,\n            unit.profile.clone(),\n            unit.kind,\n            unit.mode,\n            unit.features.clone(),","sourceCodeStart":1133,"sourceCodeEnd":1169,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_compile/mod.rs#L1133-L1169","documentation":"Thrown by override_rustc_crate_types (src/ops/cargo_compile/mod.rs:1145-1156) when `cargo rustc --crate-type <TYPES>` resolves to a number of units other than 1. Crate-type overrides mutate a single target, so Cargo requires exactly one target selected.","triggerScenarios":"Running `cargo rustc --crate-type cdylib` (or --crate-type with multiple values) when the current selection produces more than one unit — e.g. default workspace with multiple crates, or a package with several libs/examples. The `units.len() != 1` check fires.","commonSituations":"Trying to produce a cdylib from a workspace root without -p. Mixing --crate-type with --all or default multi-target selection. Scripting artifact builds without narrowing the target.","solutions":["Add a single-target filter such as `--lib`, `--example NAME`, or `-p <pkg> --lib`.","Ensure only one library/example-library unit matches (the only kinds --crate-type can apply to).","Remove --crate-type if you did not intend to override crate types."],"exampleFix":"# before\ncargo rustc --crate-type cdylib         # multiple units\n\n# after\ncargo rustc -p mycrate --lib --crate-type cdylib","handlingStrategy":"validation","validationCode":"// Before override_rustc_crate_types, ensure one unit is selected.\nfn ensure_single_unit(units: &[Unit]) -> Result<&Unit, String> {\n    match units {\n        [u] => Ok(u),\n        _ => Err(\"pass --lib or --example to select exactly one target\".into()),\n    }\n}","typeGuard":"fn is_single_lib_or_example(units: &[Unit]) -> bool {\n    units.len() == 1 && matches!(\n        units[0].target.kind(),\n        cargo::core::TargetKind::Lib(_) | cargo::core::TargetKind::ExampleLib(_)\n    )\n}","tryCatchPattern":"if let Err(e) = ops::compile(ws, &opts) {\n    if e.to_string().contains(\"crate types to rustc can only be passed\") {\n        eprintln!(\"narrow to a single lib/example target with --lib or --example\");\n    }\n    return Err(e);\n}","preventionTips":["Always pair `--crate-type` with `--lib` or `-p pkg --lib`.","In scripts, assert the selection is a single lib/example before forwarding crate types."],"tags":["cargo","rustc-args","crate-type","target-selection","compilation"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}