{"record":{"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":"exception","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/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/ops/cargo_compile/packages.rs#L21-L57","documentation":"Thrown by Packages::from_flags when an --exclude list is supplied but the `all` (i.e. --workspace) flag is false. Exclusion is only meaningful across the whole workspace, so cargo rejects it in any other selection mode.","triggerScenarios":"Running `cargo build --exclude foo` without also passing --workspace (or its aliases -all). Also via programmatic Packages::from_flags(false, non_empty, n).","commonSituations":"Cargo CLI invocations / scripts that add --exclude expecting it to filter default members; wrappers that pass exclude unconditionally.","solutions":["Add --workspace (or --all) alongside --exclude.","If you meant to exclude by default, restructure workspace `default-members` instead.","Use -p with explicit members rather than --exclude if you do not want --workspace."],"exampleFix":"# before\ncargo build --exclude foo\n# after\ncargo build --workspace --exclude foo","handlingStrategy":"validation","validationCode":"// Validate flag combination before calling Packages::from_flags.\nfn valid(exclude: &[String], all: bool) -> bool { exclude.is_empty() || all }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass --workspace when using --exclude.","In wrappers, reject --exclude unless --workspace is also set.","Use `default-members` for persistent filtering instead of --exclude on every command."],"tags":["cargo-compile","packages","workspace","cli-flags","user-input"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}