Currently, the way features are enabled for the dependencies of a crate is to have them explicitly specified through crate_features. This is bad for multiple reasons:
- Crates will be built with unnecessary features, especially if a
rules_rs-equipped Bazel module is used as a dependency of another.
- First-party crates must unconditionally enable all features that other crates depend on.
- A Bazel-declared crate not usable when it is consumed as a Bazel module dependency unless it enables all its features ahead-of-time, which makes compilation times worse for everything that doesn't use those features. (In our case we have a crate that can optionally enable egui-related features gated behind an
"egui" feature, but this increases compilation times significantly for when that crate is used for a CLI tool, for example, slowing down iteration speed).
I think a better approach is:
- During the lockfile resolution/repo generation phase, collect all features of a crate's dependencies into
data.bzl.
- Add something like
dependency_features to rust_library which can be populated with a function like all_crate_features().
The only downside I can see usability-wise is that this necessitates a workspace-hack crate similar to regular Cargo if one wants to avoid multiple copies of the same crate with different features. Another problem is how to encode the final artifact since multiple artifacts can be generated from the same rust_library target with different feature combinations.
Since rust_library etc. machinery seems to be taken directly from rules_rust this might not be a trivial change (and it breaks parity with upstream, even if we're using a fork), so I understand if it's not feasible to do this at this stage.
Currently, the way features are enabled for the dependencies of a crate is to have them explicitly specified through
crate_features. This is bad for multiple reasons:rules_rs-equipped Bazel module is used as a dependency of another."egui"feature, but this increases compilation times significantly for when that crate is used for a CLI tool, for example, slowing down iteration speed).I think a better approach is:
data.bzl.dependency_featurestorust_librarywhich can be populated with a function likeall_crate_features().The only downside I can see usability-wise is that this necessitates a
workspace-hackcrate similar to regular Cargo if one wants to avoid multiple copies of the same crate with different features. Another problem is how to encode the final artifact since multiple artifacts can be generated from the samerust_librarytarget with different feature combinations.Since
rust_libraryetc. machinery seems to be taken directly fromrules_rustthis might not be a trivial change (and it breaks parity with upstream, even if we're using a fork), so I understand if it's not feasible to do this at this stage.