{"record":{"id":"9eff207533697de0","repo":"rust-lang/cargo","slug":"extra-arguments-to-can-only-be-passed-to-one","errorCode":null,"errorMessage":"extra arguments to `{}` can only be passed to one target, consider filtering\nthe package by passing, e.g., `--lib` or `--bin NAME` to specify a single target","messagePattern":"extra arguments to `(.+?)` can only be passed to one target, consider filtering\nthe package by passing, e\\.g\\., `--lib` or `--bin NAME` to specify a single target","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/mod.rs","lineNumber":590,"sourceCode":"                platform: target_data.short_name(&unit.kind).to_owned(),\n                index,\n                features: unit\n                    .features\n                    .iter()\n                    .map(|s| s.as_str().to_owned())\n                    .collect(),\n                requested: root_unit_indexes.contains(&index),\n                dependencies,\n            });\n        }\n        let elapsed = ws.gctx().invocation_instant().elapsed().as_secs_f64();\n        logger.log(LogMessage::UnitGraphFinished { elapsed });\n    }\n\n    let mut extra_compiler_args = HashMap::default();\n    if let Some(args) = extra_args {\n        if root_units.len() != 1 {\n            anyhow::bail!(\n                \"extra arguments to `{}` can only be passed to one \\\n                 target, consider filtering\\nthe package by passing, \\\n                 e.g., `--lib` or `--bin NAME` to specify a single target\",\n                extra_args_name\n            );\n        }\n        extra_compiler_args.insert(root_units[0].clone(), args);\n    }\n\n    for unit in root_units\n        .iter()\n        .filter(|unit| unit.mode.is_doc() || unit.mode.is_doc_test())\n        .filter(|unit| rustdoc_document_private_items || unit.target.is_bin())\n    {\n        // Add `--document-private-items` rustdoc flag if requested or if\n        // the target is a binary. Binary crates get their private items\n        // documented by default.\n        let mut args = vec![\"--document-private-items\".into()];","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/rust-lang/cargo/blob/495c385d0875c4ba51eb72ea0448a2d4c018b8d4/src/ops/cargo_compile/mod.rs#L572-L608","documentation":"Thrown when extra rustc or rustdoc args (from -- target args) are supplied but the build resolved to a number of root units other than one. Extra args can only attach to a single target, so cargo refuses to guess which unit receives them.","triggerScenarios":"Running `cargo build -- <rustc-args>` (or `cargo rustc -- <args>`) without narrowing to exactly one target, in a workspace/package where multiple root units (lib + bin, multiple bins, etc.) are selected.","commonSituations":"Forgetting `--lib` / `--bin NAME` when passing rustc flags; default members resolve to more than one target; passing `--` args to `cargo test`/`bench` that build several roots.","solutions":["Add a target filter to select exactly one unit, as the message suggests: `--lib`, `--bin NAME`, `--example NAME`, etc.","Narrow the package too if needed: `-p <pkg> --lib`.","Avoid trailing `-- <args>` unless you have already constrained to a single target."],"exampleFix":"# before\ncargo build -- --cfg foo\n# after\ncargo build --bin myapp -- --cfg foo","handlingStrategy":"validation","validationCode":"// Count selected root units before forwarding `--` args.\n// Heuristic: require exactly one of --lib/--bin NAME/--example NAME/--test NAME\n// when the command line contains a `--` separator.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair trailing `-- <rustc-args>` with a single-target selector.","In wrappers, validate that exactly one target filter is present before passing args through.","Prefer `cargo rustc --lib -- <args>` over `cargo build -- <args>`."],"tags":["cargo-compile","rustc-args","target-selection","user-input"],"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"}