rust-lang/cargo · error

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

Error message

invalid feature `{}` in required-features of target `{}`: `dep:` prefixed feature values are not allowed in required-features

What it means

Thrown while validating a target's required-features list when a FeatureValue parses as FeatureValue::Dep (i.e. a `dep:<name>` form). required-features must reference real features or plain optional dependencies, never the explicit `dep:` syntax, so cargo hard-fails per target with the offending feature and target name.

Solutions

  1. Replace `dep:name` in required-features with the feature that enables the dependency (e.g. `"my-feature"` defined in [features] as `dep:name`).
  2. Use the bare optional-dependency name directly if you intend 'this target is built when the optional dep is enabled'.
  3. Validate manifests with `cargo check` after editing required-features.

Example fix

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

Strategy: validation

Validate before calling

// Lint required-features entries for the `dep:` prefix.
fn valid_required_feature(f: &str) -> bool { !f.starts_with("dep:") }

Type guard

// Only plain feature names or plain optional-dep names are allowed.
fn is_valid_required_feature(f: &str) -> bool {
    !f.starts_with("dep:") && !f.ends_with('?')
}

Prevention

When it happens

Trigger: Writing `required-features = ["dep:some-optional-crate"]` in a [[bin]]/[[example]]/[[test]]/[[bench]] target. The `dep:` prefix is only valid inside [features] tables, not in required-features.

Common situations: Copy-pasting feature syntax into required-features; misunderstanding that `dep:` and required-features compose differently; migrating a manifest to the new weak-deps syntax incorrectly.

Related errors


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

Appendix: source

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

            None => return Ok(()),
            Some(resolve) => resolve,
        };

        let mut shell = self.ws.gctx().shell();
        for feature in required_features {
            let fv = FeatureValue::new(feature.into());
            match &fv {
                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,

View on GitHub (pinned to eb98b54bc9)