{"record":{"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":"exception","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/495c385d0875c4ba51eb72ea0448a2d4c018b8d4/src/ops/cargo_compile/mod.rs#L1133-L1169","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ncargo rustc --crate-type cdylib --release\n# after\ncargo rustc --lib --crate-type cdylib --release","handlingStrategy":"validation","validationCode":"// Ensure exactly one target selector before `--crate-type`.\nfn exactly_one_target(args: &[String]) -> bool {\n    args.iter().filter(|a| a.starts_with(\"--lib\") || a.starts_with(\"--bin\") || a.starts_with(\"--example\")).count() == 1\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["cargo-compile","crate-type","rustc","target-selection"],"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"}