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

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

Error message

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

What it means

Thrown by check_required_features (src/ops/cargo_compile/unit_generator.rs:652-659) when a target's `required-features` list contains a value with the `dep:` prefix. The `dep:` syntax is an explicit weak/strong dependency activation syntax valid only in [features], not in target required-features. Cargo rejects it because required-features must reference features or dependency names, not dep: activations.

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

Solutions

  1. Replace `dep:name` in required-features with the bare dependency name `name` (or `name/feature`) — required-features already activates the optional dep when the name is listed.
  2. Remove the entry if it is not actually needed.
  3. Run `cargo check` to confirm the manifest parses.

Example fix

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

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

Strategy: validation

Validate before calling

// Reject `dep:` values inside any target's required-features.
fn required_features_clean(pkg: &cargo::core::Package) -> Result<(), String> {
    for t in pkg.targets() {
        if let Some(rf) = t.required_features() {
            for f in rf {
                if f.starts_with("dep:") {
                    return Err(format!("target {} uses forbidden `dep:` in required-features", t.name()));
                }
            }
        }
    }
    Ok(())
}

Type guard

fn has_no_dep_prefix(values: &[String]) -> bool {
    values.iter().all(|v| !v.starts_with("dep:"))
}

Try / catch

// check_required_features is called inside unit generation; catch at compile.
if let Err(e) = ops::compile(ws, &opts) {
    if e.to_string().contains("dep:` prefixed feature values") {
        eprintln!("replace `dep:x` with `x` in required-features");
    }
    return Err(e);
}

Prevention

When it happens

Trigger: Writing `[[bin]] required-features = ["dep:serde"]` in Cargo.toml. The FeatureValue parses as FeatureValue::Dep{..} and the bail fires.

Common situations: Copying `dep:` syntax from a [features] table into a target's required-features. Migrating to the new weak-deps syntax without knowing required-features has its own rules. Cargo edition/feature-guide confusion.

Related errors


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