rust-lang/cargo · error · anyhow::Error

target `{}` in package `{}` requires the features: {} Consid

Error message

target `{}` in package `{}` requires the features: {}
Consider enabling them by passing, e.g., `--features="{}"`

What it means

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.

Source

Thrown at src/ops/cargo_compile/unit_generator.rs:753

                            self.has_dev_units,
                            self.requested_kinds,
                            self.target_data,
                            ForceAllTargets::No,
                        )
                    });
                    rf.iter().filter(|f| !features.contains(*f)).collect()
                }
                None => Vec::new(),
            };
            if target.is_lib() || unavailable_features.is_empty() {
                units.extend(self.new_units(pkg, target, mode));
            } else if requires_features {
                let required_features = target.required_features().unwrap();
                let quoted_required_features: Vec<String> = required_features
                    .iter()
                    .map(|s| format!("`{}`", s))
                    .collect();
                anyhow::bail!(
                    "target `{}` in package `{}` requires the features: {}\n\
               Consider enabling them by passing, e.g., `--features=\"{}\"`",
                    target.name(),
                    pkg.name(),
                    quoted_required_features.join(", "),
                    required_features.join(" ")
                );
            }
            // else, silently skip target.
        }
        let mut units: Vec<_> = units.into_iter().collect();
        self.unmatched_target_filters(&units)?;

        // Keep the roots in a consistent order, which helps with checking test output.
        units.sort_unstable();
        Ok(units)
    }

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Pass the suggested flag: `cargo build --bin mybin --features "feat"`.
  2. Enable the feature by default in [features] default = [...] if it should always be on for that target.
  3. Remove required-features from the target if it should build unconditionally.
  4. Verify the feature name matches one declared in [features].

Example fix

# before
cargo build --bin gui        # mybin requires feature "gui"

# after
cargo build --bin gui --features="gui"
Defensive patterns

Strategy: validation

Validate before calling

// Compute required-features for a target and ensure they are all enabled.
use std::collections::HashSet;

fn missing_required(
    target: &cargo::core::Target,
    enabled: &HashSet<String>,
) -> Vec<String> {
    target.required_features()
        .unwrap_or(&[])
        .iter()
        .filter(|f| !enabled.contains(*f))
        .cloned()
        .collect()
}
// if let missing = missing_required(target, &enabled); !missing.is_empty() { pass --features }

Type guard

fn features_satisfy_target(t: &cargo::core::Target, on: &HashSet<String>) -> bool {
    t.required_features().unwrap_or(&[]).iter().all(|f| on.contains(*f))
}

Try / catch

if let Err(e) = ops::compile(ws, &opts) {
    if e.to_string().contains("requires the features") {
        // parse suggested --features from the message and retry
    }
    return Err(e);
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/8c442ee4d5c13f78.json. Report an issue: GitHub.