Crates.io | solana-crate-features |
lib.rs | solana-crate-features |
version | 1.8.16 |
source | src |
created_at | 2019-10-03 06:06:42.562864 |
updated_at | 2022-02-18 05:14:36.67509 |
description | Solana Crate Features |
homepage | https://solana.com/ |
repository | https://github.com/solana-labs/solana |
max_upload_size | |
id | 169522 |
size | 8,839 |
Cargo (as of 1.37) does not nicely handle the scenario where features of a dependent crate of multiple other crates in the tree vary.
To illustrate the problem consider crates A, B and C arranged as follows:
When crate A and B are built together, cargo
builds C with both feature F1 and
F2 enabled (the union of all enabled features). However when A or B are built
individually, cargo
builds C with only feature F1 or F2 enabled.
Unfortunately in all these cases, cargo
will build crate C in the same target
location and the outputs for C will be recreated every time the features for crate C
change.
From a clean workspace, building A individually first will cause C to be built as expected. Then build B individually and C will be re-built because F2 was enabled instead of F1. Now rebuild A and observe that C will be re-built again because F1 was re-enabled.
In practice this problem is much less obvious as both A and B likely to not have a direct dependency on C, indirectly causing rebuilds of numerous other crates as well.
The solana-crate-features
offers a workaround to this "feature thrashing"
problem by explicitly declaring all "C-like crates" with the union of all features
that any other crate in the tree (either explicitly or implicitly) enable. All
crates in the Solana source tree should depend on solana-crate-features
.
When unnecessary cargo
rebuilds are observed, the first step is to figure what
dependent crate is suffering from feature thrashing.
This information is not readily available from the stock cargo
program, so use
the following steps to produce a custom cargo
program that will output the
necessary feature information to stderr during a build:
$ git clone git@github.com:rust-lang/cargo.git -b rust-1.38.0
$ cd cargo
$ git apply 0001-Print-package-features.patch
$ cargo build
$ mv ~/.cargo/bin/cargo ~/.cargo/bin/cargo.org
$ cp ./target/debug/cargo ~/.cargo/bin/cargo
Rebuild with the custom cargo
and search for indications of crates getting
built with different features (repeated runs of ./scripts/cargo-install-all.sh
work great for this). When the problematic crate is identified, add it as a
dependency of solana-crate-features
with the union of all observed enabled
features for that crate.
This command will enable some additional cargo log output that can help debug dependency problems as well:
export CARGO_LOG=cargo::core::compiler::fingerprint=info