← Debugging Guides

阅读中文版本

Cargo — feature not found or dependency feature does not exist

Fix Cargo feature errors by matching declared features and dependency feature names in Cargo.toml.

beginnerdebuggingrustcargo

Symptoms

  • error: failed to select a version for the requirement
  • feature `foo` does not exist for package bar
  • The build fails only with a particular feature set
  • A dependency was upgraded and a feature was renamed

Possible causes

  • A feature name is misspelled or was renamed in a dependency
  • The dependency is not declared with default-features settings
  • A feature is used by a crate that does not expose it
  • Optional dependencies are referenced as features without declaring them

Diagnosis steps

Read the exact feature error

cargo build --features foo 2>&1 | head -20

Interpretation

The message names the package and the missing feature, showing whether the problem is local or in a dependency.

List available features

cargo metadata --format-version 1 | grep features

Interpretation

The metadata lists declared features per package, which you can compare with the one you requested.

Inspect the dependency manifest

cargo tree -e features

Interpretation

The feature tree shows who enables which features, revealing a rename or an unexpected enablement.

Root cause

The requested feature is not declared by the target package, because it was misspelled, renamed in a dependency upgrade, or never exposed by the local crate.

Fix

Correct the feature name, declare the feature in Cargo.toml, or enable the renamed feature on the dependency after an upgrade, and avoid referencing optional dependencies as features without declaring them.

Verify the fix

Build with the intended feature set and confirm it succeeds, and confirm the feature actually enables the expected code path or dependency.

Symptom walkthrough

Cargo fails with feature does not exist for a package, or a version selection error tied to features. A feature name may be misspelled, renamed in a dependency upgrade, or referenced by a crate that never declared it. The failure often appears only under a specific feature set, which points at the feature graph rather than the code.

Fix in practice

Match the feature name to what the package declares, declare features in your Cargo.toml, and update renamed features after a dependency upgrade. Optional dependencies used as features must be declared as such before they can be enabled.

Verification

Build with the intended features and confirm it succeeds, then confirm the feature actually enables the expected dependency or code path. A successful build that also activates the feature proves the fix. Testing the feature matrix in CI keeps the optional combinations building correctly over time.

Diagnostic commands

Check the behavior with commands like these:

cargo build --features foo 2>&1 | head -20
cargo metadata --format-version 1 | grep features
cargo tree -e features