bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
661 stars 426 forks source link

Crate universe: extra crate_features from unrelated crate with resolver = "2" #1851

Open ttiurani opened 1 year ago

ttiurani commented 1 year ago

I'm unsure if this is a bug, a missing feature or just a question, but I have a problem with crate universe I can't get around. The background is that tokio has features that can not be built with WASM, but I want to use these features in unrelated non-wasm crates in the same cargo workspace, where I have a WASM crate.

I added a reproduction of this here:

https://github.com/ttiurani/rules_rust/tree/main/examples/crate_universe_failure/extra_crate_features

I'm guessing this problem is not uncommon, and I believe slightly different problem than #1727 (feature unification is not the problem here as the crates are unrelated). Also I am inheriting these tokio features through other upstream crates so don't quite know how I would create separate rust_libraries in my Bazel workspace.

I stumbled across a similar issue with plain Cargo as well, but there using edition = "2021" and/or resolver = "2" fixed the problem:

https://github.com/rust-lang/cargo/issues/11653

Is supporting this behavior of resolver = "2" in the roadmap for crate universe?

UebelAndre commented 1 year ago

I thought this was supposed to be handled by https://github.com/bazelbuild/rules_rust/pull/1710

UebelAndre commented 11 months ago

If I understand correctly, this issue is caused by how crate_universe collects all features from all platforms and applies them to the generated dependencies. In Bazel you compile with the superset of of features because it's not currently possible define a target who's build changes based on the consumer, ignoring any configuration changes in the consumer attribute definitions.

Some possible ideas:

Not sure if anyone else has other ideas. I'm happy to share what I know to help someone find a solution and would be happy to review a pull request 😄

duarten commented 7 months ago

Would a third option be to create a target per target platform, each containing the subset of features enabled for that platform?

UebelAndre commented 5 months ago

I think https://github.com/bazelbuild/rules_rust/pull/2636 might solve this problem but am not sure.