{"record":{"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":"exception","errorClass":null,"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/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/ops/cargo_compile/unit_generator.rs#L735-L771","documentation":"Thrown when a build target (binary, example, bench, test) declares `required-features` in Cargo.toml, but the active feature set does not satisfy all of them, AND the target is not skippable. Cargo computes the unavailable features by subtracting the enabled feature set from the target's `required-features`; if any remain and `requires_features` is true (the default-filter path where targets are not silently filterable), it aborts rather than silently dropping the target. The error names the offending target, package, and the exact features to enable.","triggerScenarios":"Running `cargo build`/`cargo run`/`cargo test` (default target selection, i.e. `required_features_filterable == false`) against a package containing a `[[bin]]`, `[[example]]`, `[[bench]]`, or `[[test]]` target with `required-features = [\"...\"]`, without passing `--features` to enable them. Concretely: a target like `[[bin]]\\nname = \"tool\"\\nrequired-features = [\"gui\"]` built with `cargo build` instead of `cargo build --features gui`.","commonSituations":"Adding a feature-gated binary/example to a workspace crate and forgetting to enable the feature in CI. Copying a target with `required-features` from another project. Upgrading a dependency whose feature name changed. Running `cargo run --bin gated-bin` directly. CI invoking plain `cargo build --workspace` where one member has required-feature targets.","solutions":["Enable the missing feature(s): `cargo build --features=\"<feature>\"` (or space/comma-separated list) exactly as the message prints in the `Consider enabling` hint.","If the target should always build, remove or broaden the `required-features` list for that target in Cargo.toml so the default feature set satisfies it.","Make the feature default-on: add it to `[features] default = [\"gui\"]` so normal builds include it.","If you intended to skip the target, build a specific other target explicitly with `--bin <name>` / `--example <name>` (still needs the feature) or exclude the package.","In CI, mirror the feature set the target expects — e.g. `cargo build --all-features` or `--features` matching the crate's documented matrix."],"exampleFix":"// before (Cargo.toml)\n[[bin]]\nname = \"tool\"\nrequired-features = [\"gui\"]\n// run: cargo build   -> error\n\n// after — command\n//   cargo build --features gui\n\n// after — or make it default\n[features]\ndefault = [\"gui\"]\ngui = [\"dep:some_gui_lib\"]","handlingStrategy":"validation","validationCode":"// Before building, compute whether the enabled feature set satisfies each target's required-features.\nfn missing_required_features(\n    enabled: &std::collections::HashSet<String>,\n    required: &[String],\n) -> Vec<String> {\n    required.iter().filter(|f| !enabled.contains(*f)).cloned().collect()\n}\n\n// usage: if !missing.is_empty() { eprintln!(\"pass --features='{}'\", missing.join(\" \")); }","typeGuard":"// Narrow a target description before selecting it for a default build.\nstruct TargetSpec<'a> { name: &'a str, required_features: &'a [String] }\nfn is_buildable_with(target: &TargetSpec, features: &std::collections::HashSet<String>) -> bool {\n    target.required_features.iter().all(|f| features.contains(f))\n}","tryCatchPattern":null,"preventionTips":["Audit Cargo.toml [[bin]]/[[example]]/[[bench]]/[[test]] blocks for `required-features` and document the needed `--features` in your README/CI.","In CI, build with the documented feature set explicitly rather than a bare `cargo build`.","Consider adding required features to `[features] default = [...]` if a target should build by default.","When adding a feature-gated target, update CI matrices and any wrapper scripts in the same commit."],"tags":["features","build-targets","cargo-toml","cli-usage"],"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"}