Assume that I have a .cargo/config.toml
file in my ROOT_DIR
(ROOT_DIR
contains my cargo.toml
file), with the following content:
.cargo/config.toml
[build]
rustflags = ["--cfg", "MAINNET"]
Consider my src/main.rs
file:
src/main.rs
fn main() {
#[cfg(MAINNET)]
println("This is `MAINNET`");
#[cfg(TESTNET)]
println!("This is `TESTNET`");
}
I can use cargo check --config 'build.rustflags=["--cfg", "TESTNET"]'
to override the "MAINNET"
config flag within .cargo/config.toml
if I want to specifically do cargo check
with the "TESTNET"
flag set.
However, whenever I try to do anything, e.g. cargo check
, cargo test
, with different compilation build.rustflags
, e.g. ["--cfg", "TESTNET"]
or ["--cfg", "MAINNET"]
, cargo will recompile from the beginning. It seems that nothing was cached from the previous compilations.
For example, suppose I run these commands in this order
-
cargo check --config 'build.rustflags=["--cfg", "TESTNET"]'
- Compiles all the dependencies from scratch. -
cargo check --config 'build.rustflags=["--cfg", "TESTNET"]'
- Does NOT compile all the dependencies from scratch. -
cargo check --config 'build.rustflags=["--cfg", "MAINNET"]'
- Compiles all the dependencies from scratch. -
cargo check --config 'build.rustflags=["--cfg", "MAINNET"]'
- Does NOT compile all the dependencies from scratch. -
cargo check --config 'build.rustflags=["--cfg", "TESTNET"]'
- COMPILES all the dependencies from scratch.
Note that there are zero code changes between each of these commands.
Is there a way to force cargo to cache the dependencies when building with different [build.rustflags]
?