Skip to content

Commit 5ca05d3

Browse files
committed
Pass CARGO_CFG_DEBUG_ASSERTIONS to build scripts based on profile setting
fix(test): update test to reflect CARGO_CFG_DEBUG_ASSERTIONS is now passed to build scripts chore(deps): update build-rs to version 0.3.3 in Cargo.lock and Cargo.toml
1 parent c96a7fc commit 5ca05d3

File tree

7 files changed

+15
-13
lines changed

7 files changed

+15
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/build-rs-test-lib/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn smoke_test_inputs() {
1111
dbg!(cargo());
1212
dbg!(cargo_cfg_feature());
1313
dbg!(cargo_cfg("careful"));
14+
dbg!(cargo_cfg_debug_assertions());
1415
#[cfg(feature = "unstable")]
1516
dbg!(cargo_cfg_fmt_debug());
1617
#[cfg(feature = "unstable")]

crates/build-rs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "build-rs"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
rust-version.workspace = true
55
edition.workspace = true
66
license.workspace = true

crates/build-rs/src/input.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ mod cfg {
172172
}
173173

174174
/// If we are compiling with debug assertions enabled.
175-
///
176-
/// Build scripts are not passed this cfg because
177-
/// this cfg is always true and misleading.
178-
/// That is because Cargo queries rustc without any profile settings.
179-
#[cfg(any())]
180175
#[track_caller]
181176
pub fn cargo_cfg_debug_assertions() -> bool {
182177
ENV.is_present("CARGO_CFG_DEBUG_ASSERTIONS")

src/cargo/core/compiler/custom_build.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,19 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
407407
"feature",
408408
unit.features.iter().map(|s| s.as_str()).collect::<Vec<_>>(),
409409
);
410+
// Manually inject debug_assertions based on the profile setting.
411+
// The cfg query from rustc doesn't include profile settings and would always be true,
412+
// so we override it with the actual profile setting.
413+
if unit.profile.debug_assertions {
414+
cfg_map.insert("debug_assertions", Vec::new());
415+
}
410416
for cfg in bcx.target_data.cfg(unit.kind) {
411417
match *cfg {
412418
Cfg::Name(ref n) => {
419+
// Skip debug_assertions from rustc query; we use the profile setting instead
420+
if n.as_str() == "debug_assertions" {
421+
continue;
422+
}
413423
cfg_map.insert(n.as_str(), Vec::new());
414424
}
415425
Cfg::KeyPair(ref k, ref v) => {
@@ -419,11 +429,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
419429
}
420430
}
421431
for (k, v) in cfg_map {
422-
if k == "debug_assertions" {
423-
// This cfg is always true and misleading, so avoid setting it.
424-
// That is because Cargo queries rustc without any profile settings.
425-
continue;
426-
}
427432
// FIXME: We should handle raw-idents somehow instead of predenting they
428433
// don't exist here
429434
let k = format!("CARGO_CFG_{}", super::envify(k));

src/cargo/core/profiles.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl Profiles {
330330
result.root = for_unit_profile.root;
331331
result.debuginfo = for_unit_profile.debuginfo;
332332
result.opt_level = for_unit_profile.opt_level;
333+
result.debug_assertions = for_unit_profile.debug_assertions;
333334
result.trim_paths = for_unit_profile.trim_paths.clone();
334335
result
335336
}

tests/testsuite/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5887,7 +5887,7 @@ fn user_specific_cfgs_are_filtered_out() {
58875887
r#"
58885888
fn main() {
58895889
assert!(std::env::var_os("CARGO_CFG_PROC_MACRO").is_none());
5890-
assert!(std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_none());
5890+
assert!(std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some());
58915891
}
58925892
"#,
58935893
)

0 commit comments

Comments
 (0)