Skip to content

Add per-platform crate_universe feature and dependency exclusions#4164

Open
mgeisler wants to merge 1 commit into
bazelbuild:mainfrom
mgeisler:crate-universe-per-platform-feature-exclusions
Open

Add per-platform crate_universe feature and dependency exclusions#4164
mgeisler wants to merge 1 commit into
bazelbuild:mainfrom
mgeisler:crate-universe-per-platform-feature-exclusions

Conversation

@mgeisler

Copy link
Copy Markdown
Contributor

Written by Claude, I can confirm that this works my company monorepo where we compile a Rust core for both Wasm, Android, and iOS.

I am happy to discuss other approaches: this was one suggested by the LLM. I hope the PR here serves to demonstrate the problem well and hopefully we can find a good and flexible solution. One thing I noticed while applying the fix to our codebase is that it required a lot of trial and error before Claude found the right crates and features to disable. A solution that simplifies this would be great.


crate_universe resolves crate features once over the whole Cargo workspace, so Cargo's workspace-wide feature unification activates a feature requested by any member on the single shared target generated for that crate, for every supported platform triple.

This forces features onto platforms that cannot build them: an unconditional tokio = { features = ["net"] } anywhere in the workspace pulls in the net feature for Tokio (and thus the transitive mio and socket2 dependencies). This happens even on wasm32-unknown-unknown, where they do not compile.

This MR adds new disabled_features and excluded_deps attributes to crate.annotation and crate.annotation_select. They allow a user to trim the resolved feature and dependency sets of a crate. The removal is scoped to a triple and it demotes a formerly unconditional value onto the other supported triples. This way, only the named platform loses it.

Because the trimming operates on the shared target's own resolved attributes, it fixes the crate for every consumer at once, regardless of which workspace member (or dev-dependency) unioned the feature in.

New unit tests have been added, as well as an integration test that demonstrates the issue for Tokio compiled both with and without the net feature.

Fixes #4163.

@mgeisler
mgeisler force-pushed the crate-universe-per-platform-feature-exclusions branch from 5afc99f to 8d97d08 Compare July 22, 2026 15:59
@mgeisler mgeisler changed the title Add per-platform crate_universe feature and dependency exclusions Add per-platform crate_universe feature and dependency exclusions Jul 22, 2026
@mgeisler

Copy link
Copy Markdown
Contributor Author

It seems the number of integration tests is fixed at max 128? This PR adds a new one – I'm curious to hear how I should approach this. I'll let it sit for a while until someone can tell me more about the conventions here.

@mgeisler
mgeisler force-pushed the crate-universe-per-platform-feature-exclusions branch from 8d97d08 to f98211f Compare July 23, 2026 15:17
`crate_universe` resolves crate features once over the whole Cargo
workspace, so Cargo's workspace-wide feature unification activates a
feature requested by any member on the single shared target generated
for that crate, for every supported platform triple.

This forces features onto platforms that cannot build them: an
unconditional `tokio = { features = ["net"] }` anywhere in the
workspace pulls in the `net` feature for Tokio (and thus the
transitive `mio` and `socket2` dependencies). This happens even on
`wasm32-unknown-unknown`, where they do not compile.

This MR adds new `disabled_features` and `excluded_deps` attributes to
`crate.annotation` and `crate.annotation_select`. They allow a user to
trim the resolved feature and dependency sets of a crate. The removal
is scoped to a triple and it demotes a formerly unconditional value
onto the other supported triples. This way, only the named platform
loses it.

Because the trimming operates on the shared target's own resolved
attributes, it fixes the crate for every consumer at once, regardless
of which workspace member (or dev-dependency) unioned the feature in.

New unit tests have been added, as well as an integration test that
demonstrates the issue for Tokio compiled both with and without the
`net` feature.

Fixes bazelbuild#4163.
@mgeisler
mgeisler force-pushed the crate-universe-per-platform-feature-exclusions branch from f98211f to f7183b1 Compare July 23, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cargo features are enabled for all target triples

1 participant