{"id":"8c442ee4d5c13f78","repo":"rust-lang/cargo","slug":"target-in-package-requires-the-features","errorCode":null,"errorMessage":"target `{}` in package `{}` requires the features: {}\nConsider enabling them by passing, e.g., `--features=\"{}\"`","messagePattern":"target `(.+?)` in package `(.+?)` requires the features: (.+?)\nConsider enabling them by passing, e\\.g\\., `--features=\"(.+?)\"`","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_compile/unit_generator.rs","lineNumber":753,"sourceCode":"                            self.has_dev_units,\n                            self.requested_kinds,\n                            self.target_data,\n                            ForceAllTargets::No,\n                        )\n                    });\n                    rf.iter().filter(|f| !features.contains(*f)).collect()\n                }\n                None => Vec::new(),\n            };\n            if target.is_lib() || unavailable_features.is_empty() {\n                units.extend(self.new_units(pkg, target, mode));\n            } else if requires_features {\n                let required_features = target.required_features().unwrap();\n                let quoted_required_features: Vec<String> = required_features\n                    .iter()\n                    .map(|s| format!(\"`{}`\", s))\n                    .collect();\n                anyhow::bail!(\n                    \"target `{}` in package `{}` requires the features: {}\\n\\\n               Consider enabling them by passing, e.g., `--features=\\\"{}\\\"`\",\n                    target.name(),\n                    pkg.name(),\n                    quoted_required_features.join(\", \"),\n                    required_features.join(\" \")\n                );\n            }\n            // else, silently skip target.\n        }\n        let mut units: Vec<_> = units.into_iter().collect();\n        self.unmatched_target_filters(&units)?;\n\n        // Keep the roots in a consistent order, which helps with checking test output.\n        units.sort_unstable();\n        Ok(units)\n    }\n","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_compile/unit_generator.rs#L735-L771","documentation":"Thrown in proposals_to_units (src/ops/cargo_compile/unit_generator.rs:745-761) when a target was explicitly selected (requires_features is true), it has required-features, and some of those features are not enabled in the current feature set. The message lists the missing features and suggests the exact --features string to enable them.","triggerScenarios":"`cargo build --bin mybin` where mybin declares `required-features = [\"feat\"]` but `feat` is not in the enabled feature set (no --features, no default-features match). unavailable_features is non-empty and the target is not a lib.","commonSituations":"A feature-gated binary/example that needs an explicit feature flag. Forgetting to pass --features in CI. A target that depends on a feature that was renamed or removed.","solutions":["Pass the suggested flag: `cargo build --bin mybin --features \"feat\"`.","Enable the feature by default in [features] default = [...] if it should always be on for that target.","Remove required-features from the target if it should build unconditionally.","Verify the feature name matches one declared in [features]."],"exampleFix":"# before\ncargo build --bin gui        # mybin requires feature \"gui\"\n\n# after\ncargo build --bin gui --features=\"gui\"","handlingStrategy":"validation","validationCode":"// Compute required-features for a target and ensure they are all enabled.\nuse std::collections::HashSet;\n\nfn missing_required(\n    target: &cargo::core::Target,\n    enabled: &HashSet<String>,\n) -> Vec<String> {\n    target.required_features()\n        .unwrap_or(&[])\n        .iter()\n        .filter(|f| !enabled.contains(*f))\n        .cloned()\n        .collect()\n}\n// if let missing = missing_required(target, &enabled); !missing.is_empty() { pass --features }","typeGuard":"fn features_satisfy_target(t: &cargo::core::Target, on: &HashSet<String>) -> bool {\n    t.required_features().unwrap_or(&[]).iter().all(|f| on.contains(*f))\n}","tryCatchPattern":"if let Err(e) = ops::compile(ws, &opts) {\n    if e.to_string().contains(\"requires the features\") {\n        // parse suggested --features from the message and retry\n    }\n    return Err(e);\n}","preventionTips":["Pass --features for feature-gated targets in CI matrices.","Consider adding required features to [features].default if always needed."],"tags":["cargo","features","required-features","target-selection","compilation"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}