You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add per-platform crate_universe feature and dependency exclusions
`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.
doc="A list of labels to add to a crate's `rust_library::deps` attribute.",
1419
1419
),
1420
+
"disabled_features": attr.string_list(
1421
+
doc="A list of features to remove from a crate's resolved `rust_library::crate_features` attribute. Applied per-triple when set via `annotation_select`, preserving the feature on the other supported triples.",
1422
+
),
1423
+
"excluded_deps": attr.string_list(
1424
+
doc="A list of dependency crate names to remove from a crate's resolved `rust_library::deps` attribute. Typically paired with `disabled_features` to also drop the optional dependencies a disabled feature would have activated.",
1425
+
),
1420
1426
"link_deps": _relative_label_list(
1421
1427
doc="A list of labels to add to a crate's `rust_library::link_deps` attribute.",
1422
1428
),
@@ -1624,6 +1630,34 @@ crate = module_extension(
1624
1630
doc="""\
1625
1631
Crate universe module extensions.
1626
1632
1633
+
## Removing features and dependencies on specific platforms
1634
+
1635
+
Cargo enables features additively. When one crate in the build turns on a
1636
+
feature of a shared dependency, that feature is enabled for every target that
1637
+
uses the dependency. This is usually fine, but sometimes it enables a feature on
1638
+
a platform that cannot build it.
1639
+
1640
+
Take the `net` feature of `tokio`. It requires the `mio` and `socket2` crates,
1641
+
which do not compile for `wasm32-unknown-unknown`. If any crate in the build
1642
+
enables `net`, then `net` is enabled for `tokio` on every platform, and the Wasm
1643
+
build fails.
1644
+
1645
+
The `disabled_features` and `excluded_deps` fields on an annotation remove
1646
+
features and dependencies from a crate. Pair them with `crate.annotation_select`
1647
+
to limit the removal to specific target triples. The feature or dependency stays
1648
+
enabled on the other triples, so only the platform you name loses it:
1649
+
1650
+
```python
1651
+
crate.annotation_select(
1652
+
crate = "tokio",
1653
+
triples = ["wasm32-unknown-unknown"],
1654
+
# For wasm32 only: drop `net` and the implicit `mio` and `socket2` features,
1655
+
# and exclude the `mio` and `socket2` crates. `net` stays enabled elsewhere.
0 commit comments