From 88f18b71616248c464f73999e8b6a8adce2b91f9 Mon Sep 17 00:00:00 2001 From: not-matthias Date: Tue, 29 Jul 2025 13:57:55 +0200 Subject: [PATCH] fix(cargo-codspeed): use target.rustflags config so it's merged with the .cargo/config.toml When using build.rustflags, it won't be merged because it has a lower precedence. See: https://doc.rust-lang.org/nightly/cargo/reference/config.html#buildrustflags --- crates/cargo-codspeed/src/build.rs | 3 ++- .../tests/cargo_config.in/.cargo/config.toml | 8 ++++++++ .../tests/cargo_config.in/benches/cargo_config_bench.rs | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/cargo-codspeed/src/build.rs b/crates/cargo-codspeed/src/build.rs index ab099b6d..6bf9c117 100644 --- a/crates/cargo-codspeed/src/build.rs +++ b/crates/cargo-codspeed/src/build.rs @@ -145,8 +145,9 @@ impl BuildOptions<'_> { // Use --config to set rustflags // Our rust integration has an msrv of 1.74, --config is available since 1.63 // https://doc.rust-lang.org/nightly/cargo/CHANGELOG.html#cargo-163-2022-08-11 + // Note: We have to use `target.cfg(all())` since `build` has a lower precedence. let config_value = format!( - "build.rustflags=[{}]", + "target.'cfg(all())'.rustflags=[{}]", flags.into_iter().map(|f| format!("\"{f}\"")).join(",") ); cargo.arg("--config").arg(config_value); diff --git a/crates/cargo-codspeed/tests/cargo_config.in/.cargo/config.toml b/crates/cargo-codspeed/tests/cargo_config.in/.cargo/config.toml index 7b4d0356..d1905f4b 100644 --- a/crates/cargo-codspeed/tests/cargo_config.in/.cargo/config.toml +++ b/crates/cargo-codspeed/tests/cargo_config.in/.cargo/config.toml @@ -1,2 +1,10 @@ +# Note: This does nothing since `target..rustflags` takes precedence over `build.rustflags` +# See: https://doc.rust-lang.org/nightly/cargo/reference/config.html#buildrustflags [build] +rustflags = ["--cfg", "this_does_nothing"] + +[target.x86_64-unknown-linux-gnu] +rustflags = ["--cfg", "target_feature_flag"] + +[target.'cfg(all())'] rustflags = ["--cfg", "custom_feature_flag"] diff --git a/crates/cargo-codspeed/tests/cargo_config.in/benches/cargo_config_bench.rs b/crates/cargo-codspeed/tests/cargo_config.in/benches/cargo_config_bench.rs index d763db64..36c2478c 100644 --- a/crates/cargo-codspeed/tests/cargo_config.in/benches/cargo_config_bench.rs +++ b/crates/cargo-codspeed/tests/cargo_config.in/benches/cargo_config_bench.rs @@ -8,6 +8,11 @@ fn bench_failing_without_custom_flag(c: &mut Criterion) { compile_error!( "custom_feature_flag is not enabled - .cargo/config.toml rustflags not applied" ); + + #[cfg(not(target_feature_flag))] + compile_error!( + "target_feature_flag is not enabled - .cargo/config.toml rustflags not applied" + ); }) }); }