My rust project is constructed from a workspace with many (50+) sub-crates in the workspace.
I want to cascade a feature flag (sharing the same name) through all the dependency tree (all the sub-crates which are depending on each other).
example
# I have many shared crates. some sub-crates depend on other subcrates
package/Cargo.toml
[dependencies]
shared_package1 = {path = "../shared_package1"}
shared_package2 = {path = "../shared_package2"}
shared_package3 = {path = "../shared_package3"}
...
shared_package100 = {path = "../shared_package100"}
[features]
shared_feature = []
shared_package2/Cargo.toml
[dependencies]
shared_package3 = {path = "../shared_package3"}
...
shared_package100 = {path = "../shared_package100"}
[features]
shared_feature = []
....
....
shared_package100/Cargo.toml
[dependencies]
[features]
shared_feature = []
now that I've added shared_feature
I resort to manually adding the feature configuration to my 100 crates and dependencies (need to figure out which feature flags need to be modified) and this is error prone (I'm tripping myself up writing the question :) ).
package/Cargo.toml
[features]
# turn on shared_feature flag for all the dependencies now and cascade it down
shared_feature = ["shared_package1/shared_feature", "shared_package2/shared_feature", .... "shared_package100/shared_feature"]
shared_package1/Cargo.toml
...
[features]
# turn it on....
shared_feature = ["shared_package2/shared_feature", "shared_package3/shared_feature", .... "shared_package100/shared_feature"]
# doing this for all the tree
...
...
shared_package99/Cargo.toml
...
[features]
shared_feature = [....]
I've tried to grep through Cargo.toml files but seems very cumbersome.
Edit:
This toy script helps to locate the feature flags
# make sure you have these installed
# https://crates.io/crates/cargo-get
# cargo install cargo-get
# cargo install feature
declare -A cache
# feature name you are searching for
feature=$1
echo "Cargo.toml files with $feature feature flags"
for toml in $(find . -name Cargo.toml)
do
crate=$(cargo get -n --root $toml 2>/dev/null)
if [ -z "$crate" ]
then
continue
fi
hasfeatue=$(cargo feature $crate 2>/dev/null | grep $feature)
if [ -z "$hasfeatue" ]
then
cache[$crate]=0
continue
fi
cache[$crate]=1
echo $toml $crate
echo $hasfeatue
for sub in $(cargo tree -p $crate --depth 1 --prefix=none | cut -d' ' -f1)
do
if [ $sub = $crate ]
then
continue
fi
if [ ! -z ${cache[$sub]} ]
then
if [ ${cache[$sub]} -eq 1 ]
then
echo "\tsub: ${sub}"
fi
continue
fi
res=$(cargo feature $sub 2>/dev/null | grep $feature 2>/dev/null)
if [ -z "$res" ]
then
cache[$sub]=0
continue
fi
cache[$sub]=1
echo "\tsub: ${sub}"
done
done