{"id":"68bdb80e697553ec","repo":"rust-lang/cargo","slug":"exclude-can-only-be-used-together-with-workspa","errorCode":null,"errorMessage":"--exclude can only be used together with --workspace","messagePattern":"--exclude can only be used together with --workspace","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/packages.rs","lineNumber":39,"sourceCode":"    ///\n    /// As of the time of this writing, it only works on opting in all workspace members.\n    /// Keeps the packages passed in to verify that they exist in the workspace.\n    All(Vec<String>),\n    /// Opt out of packages passed in.\n    ///\n    /// As of the time of this writing, it only works on opting out workspace members.\n    OptOut(Vec<String>),\n    /// A sequence of hand-picked packages that will be built. Normally done by `-p` flag.\n    Packages(Vec<String>),\n}\n\nimpl Packages {\n    /// Creates a `Packages` from flags which are generally equivalent to command line flags.\n    pub fn from_flags(all: bool, exclude: Vec<String>, package: Vec<String>) -> CargoResult<Self> {\n        Ok(match (all, exclude.len(), package.len()) {\n            (false, 0, 0) => Packages::Default,\n            (false, 0, _) => Packages::Packages(package),\n            (false, _, _) => anyhow::bail!(\"--exclude can only be used together with --workspace\"),\n            (true, 0, _) => Packages::All(package),\n            (true, _, _) => Packages::OptOut(exclude),\n        })\n    }\n\n    /// Converts selected packages to [`PackageIdSpec`]s.\n    pub fn to_package_id_specs(&self, ws: &Workspace<'_>) -> CargoResult<Vec<PackageIdSpec>> {\n        let specs = match self {\n            Packages::All(packages) => {\n                emit_packages_not_found_within_workspace(ws, packages)?;\n                ws.members()\n                    .map(Package::package_id)\n                    .map(|id| id.to_spec())\n                    .collect()\n            }\n            Packages::OptOut(opt_out) => {\n                let (mut patterns, mut ids) = opt_patterns_and_ids(opt_out)?;\n                let specs = ws","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_compile/packages.rs#L21-L57","documentation":"Thrown by Packages::from_flags (src/ops/cargo_compile/packages.rs:35-43) when the `--exclude` flag is passed without `--workspace` (or `-r`/`--all`). Exclusion only has meaning over the full workspace set, so Cargo rejects exclude used in isolation.","triggerScenarios":"Invoking `cargo build --exclude foo` (or test/check/etc.) without also passing --workspace/--all. The match arm `(false, _, _)` where exclude.len() > 0 bails.","commonSituations":"Copy-pasting a command that used --exclude from a workspace context into a non-workspace invocation. Muscle-memory `--exclude` forgetting the companion --workspace. Aliases or scripts that inject --exclude conditionally.","solutions":["Add --workspace: `cargo build --workspace --exclude foo`.","If you only want specific packages, drop --exclude and use `-p a -p b` to list what you want.","Remove the --exclude flag entirely."],"exampleFix":"# before\ncargo test --exclude slow-crate\n\n# after\ncargo test --workspace --exclude slow-crate","handlingStrategy":"validation","validationCode":"// Packages::from_flags requires: exclude.is_empty() || all == true.\nuse cargo::ops::Packages;\n\nfn validate_flags(all: bool, exclude: &[String], package: &[String])\n    -> Result<Packages, String>\n{\n    if !exclude.is_empty() && !all {\n        return Err(\"--exclude requires --workspace\".into());\n    }\n    Packages::from_flags(all, exclude.to_vec(), package.to_vec())\n        .map_err(|e| e.to_string())\n}","typeGuard":"fn exclude_is_valid(all: bool, exclude: &[String]) -> bool {\n    exclude.is_empty() || all\n}","tryCatchPattern":"match Packages::from_flags(all, exclude, package) {\n    Err(e) if e.to_string().contains(\"--exclude\") => {\n        eprintln!(\"add --workspace when using --exclude\");\n        return Err(e);\n    }\n    r => r,\n}","preventionTips":["Treat --exclude and --workspace as a pair in scripts.","Validate flag combinations before invoking cargo."],"tags":["cargo","cli","workspace","package-selection","flags"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}