Is there a way to see all the crates in my project that optionally depend on another crate?

Viewed 117

I'm adding parking_lot to my project and I want my dependencies that can optionally use it to use it as well.

For example, I know that Tokio has a feature flag to enable parking_lot, but I want to find all my dependencies that have similar feature flags.

1 Answers

Yandros on the Rust Discord server whipped up this combination of cargo metadata and jq to list the crates that have an optional dependency on a given package (parking_lot in this example):

cargo metadata --format-version 1 | jq -c '.packages[] | select(
    .dependencies | any(
        (.name == "parking_lot")
        and
        (.optional == true)
    )
) | .name'
Related