Forcing set of features for shared dependencies across targets in workspace

#1

Is there any way to force same set of features for all the dependencies shared across targets in workspace?

Cargo will use minimal set of features for each target (https://github.com/rust-lang/cargo/issues/4463 & related). This causes problems for us due to all the recompilation that happens across different targets.

For example, we have two chains of dependencies:

  1. A => huge_crate => some_derive => syn
  2. B => huge_crate => some_derive => syn

When compiling “A”, “syn” is compiled with default features. However, when compiling “B”, “syn” is compiled with feature [“extra-traits”] enabled (because of some other dependency only used for “B”, but not for “A”).

This triggers chain of discrepancies and in the end, when both “A” and “B” are compiled independently (for example, “cargo run --package A”, then separately “cargo run --package B”), causes some of our pretty big crates (“huge_crate”) to be compiled twice.

What I would prefer is some way to unify features across the whole workspace, maybe something like a superset of features? Like a workspace setting saying “when depending on ‘syn’, turn on [“extra-traits”] feature”.

Is anything like this possible?

Currently I’m using a pretty crude workaround, a crate used as dev-dependency both for “A” and “B” and which depends on all of the crates where we have discrepancy in features, with a superset of features turned on.

0 Likes

#2

I think, what I’m asking for is the reverse of https://github.com/rust-lang/cargo/issues/5730#issuecomment-427893012 and https://github.com/rust-lang/crates-io-cargo-teams/issues/13#issuecomment-439757466: I want to unify features across different targets as much as possible (perhaps, manually, by providing some extra configuration in my workspace Cargo.toml), to reduce (re-)compilation times.

0 Likes