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

invalid feature `{}` in required-features of target `{}`: op

Error message

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

What it means

Thrown by check_required_features (src/ops/cargo_compile/unit_generator.rs:660-667) when a target's `required-features` contains a weak dependency reference using the `dep_name?` (optional dependency with `?`) syntax. Weak/conditional activation is a [features]-table concept and is not permitted inside target required-features.

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 0e07a15537)

Solutions

  1. Use the bare dependency name `serde` (required-features treats listing a dep name as requiring it enabled).
  2. Use `dep_name/feature` if you need a specific dependency feature.
  3. Drop the `?`.

Example fix

# before
[[bin]]
name = "mybin"
required-features = ["serde?"]

# after
[[bin]]
name = "mybin"
required-features = ["serde"]
Defensive patterns

Strategy: validation

Validate before calling

// Reject `name?` weak-dep references in required-features.
fn no_weak_dep_in_required_features(pkg: &cargo::core::Package) -> Result<(), String> {
    for t in pkg.targets() {
        if let Some(rf) = t.required_features() {
            for f in rf {
                if f.ends_with('?') || f.contains("?/") {
                    return Err(format!("target {} uses forbidden weak-dep `?` in required-features", t.name()));
                }
            }
        }
    }
    Ok(())
}

Type guard

fn has_no_weak_dep(values: &[String]) -> bool {
    values.iter().all(|v| !v.contains('?'))
}

Try / catch

if let Err(e) = ops::compile(ws, &opts) {
    if e.to_string().contains("optional dependency with `?`") {
        eprintln!("drop the `?`; list the bare dependency name in required-features");
    }
    return Err(e);
}

Prevention

When it happens

Trigger: Writing `[[bin]] required-features = ["serde?"]` (the `?` weak-dep syntax). FeatureValue::DepFeature{weak:true,...} triggers the bail.

Common situations: Using new weak-deps syntax in the wrong place. Copying `crate?/feature` patterns into required-features. Feature-guide confusion between [features] and required-features.

Related errors


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