rust-lang/cargo · error

invalid feature ` ` in required-features of target ` `…

Error message

invalid feature `{}` in required-features of target `{}`: optional dependency with `?` is not allowed in required-features

What it means

Sibling of error 78, same validator: fires when a FeatureValue in required-features parses as FeatureValue::DepFeature with weak == true, i.e. an optional dependency referenced with the trailing `?` (weak feature) form like `"opt-dep?"`. Weak optional deps cannot drive target inclusion because their activation depends on another feature, so cargo rejects them in required-features.

Solutions

  1. Remove the `?` so the dependency is a normal optional dependency: `required-features = ["some-optional-dep"]`.
  2. Or gate the target on the feature that activates the weak dep: `required-features = ["the-feature"]`.
  3. Re-run `cargo check` to confirm the manifest validates after the edit.

Example fix

# before
[[bin]]
name = "tool"
required-features = ["opt-dep?"]
# after
[[bin]]
name = "tool"
required-features = ["opt-dep"]
Defensive patterns

Strategy: validation

Validate before calling

// Lint required-features entries for weak-dep `?` suffix.
fn valid_required_feature(f: &str) -> bool { !f.ends_with('?') }

Type guard

// Reject weak optional-dep syntax in required-features.
fn is_valid_required_feature(f: &str) -> bool {
    !f.starts_with("dep:") && !f.ends_with('?')
}

Prevention

When it happens

Trigger: Writing `required-features = ["some-optional-dep?"]` in a target. The `?` suffix marks the dependency as weakly optional, which is not permitted for gating target builds.

Common situations: Adopting the weak-deps (`dep:`/`?`) RFC and incorrectly reusing the syntax in required-features; refactoring [features] without updating target required-features consistently.

Related errors


AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11). Data as JSON: /api/errors/c62c1ace021c3397. Report an issue: GitHub.

Appendix: source

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

                FeatureValue::Feature(f) => {
                    if !summary.features().contains_key(f) {
                        shell.warn(format!(
                            "invalid feature `{}` in required-features of target `{}`: \
                      `{}` is not present in [features] section",
                            fv, target_name, fv
                        ))?;
                    }
                }
                FeatureValue::Dep { .. } => {
                    anyhow::bail!(
                        "invalid feature `{}` in required-features of target `{}`: \
                  `dep:` prefixed feature values are not allowed in required-features",
                        fv,
                        target_name
                    );
                }
                FeatureValue::DepFeature { weak: true, .. } => {
                    anyhow::bail!(
                        "invalid feature `{}` in required-features of target `{}`: \
                  optional dependency with `?` is not allowed in required-features",
                        fv,
                        target_name
                    );
                }
                // Handling of dependent_crate/dependent_crate_feature syntax
                FeatureValue::DepFeature {
                    dep_name,
                    dep_feature,
                    weak: false,
                } => {
                    match resolve.deps(summary.package_id()).find(|(_dep_id, deps)| {
                        deps.iter().any(|dep| dep.name_in_toml() == *dep_name)
                    }) {
                        Some((dep_id, _deps)) => {
                            let dep_summary = resolve.summary(dep_id);
                            if !dep_summary.features().contains_key(dep_feature)

View on GitHub (pinned to eb98b54bc9)