Skip to content
Open
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/build-rs-test-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn smoke_test_inputs() {
dbg!(cargo());
dbg!(cargo_cfg_feature());
dbg!(cargo_cfg("careful"));
dbg!(cargo_cfg_debug_assertions());
#[cfg(feature = "unstable")]
dbg!(cargo_cfg_fmt_debug());
#[cfg(feature = "unstable")]
Expand Down
2 changes: 1 addition & 1 deletion crates/build-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "build-rs"
version = "0.3.2"
version = "0.3.3"
rust-version.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
5 changes: 0 additions & 5 deletions crates/build-rs/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ mod cfg {
}

/// If we are compiling with debug assertions enabled.
///
/// Build scripts are not passed this cfg because
/// this cfg is always true and misleading.
/// That is because Cargo queries rustc without any profile settings.
#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_debug_assertions() -> bool {
ENV.is_present("CARGO_CFG_DEBUG_ASSERTIONS")
Expand Down
15 changes: 10 additions & 5 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,19 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
"feature",
unit.features.iter().map(|s| s.as_str()).collect::<Vec<_>>(),
);
// Manually inject debug_assertions based on the profile setting.
// The cfg query from rustc doesn't include profile settings and would always be true,
// so we override it with the actual profile setting.
if unit.profile.debug_assertions {
cfg_map.insert("debug_assertions", Vec::new());
}
for cfg in bcx.target_data.cfg(unit.kind) {
match *cfg {
Cfg::Name(ref n) => {
// Skip debug_assertions from rustc query; we use the profile setting instead
if n.as_str() == "debug_assertions" {
continue;
}
cfg_map.insert(n.as_str(), Vec::new());
}
Cfg::KeyPair(ref k, ref v) => {
Expand All @@ -419,11 +429,6 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
}
}
for (k, v) in cfg_map {
if k == "debug_assertions" {
// This cfg is always true and misleading, so avoid setting it.
// That is because Cargo queries rustc without any profile settings.
continue;
}
// FIXME: We should handle raw-idents somehow instead of predenting they
// don't exist here
let k = format!("CARGO_CFG_{}", super::envify(k));
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl Profiles {
result.root = for_unit_profile.root;
result.debuginfo = for_unit_profile.debuginfo;
result.opt_level = for_unit_profile.opt_level;
result.debug_assertions = for_unit_profile.debug_assertions;
result.trim_paths = for_unit_profile.trim_paths.clone();
result
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5887,7 +5887,7 @@ fn user_specific_cfgs_are_filtered_out() {
r#"
fn main() {
assert!(std::env::var_os("CARGO_CFG_PROC_MACRO").is_none());
assert!(std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_none());
assert!(std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some());
}
"#,
)
Expand Down
19 changes: 19 additions & 0 deletions tests/testsuite/build_script_env.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test(add): add test for CARGO_CFG_DEBUG_ASSERTIONS in dev profile

For future reference, in Conventional commits, add is the scope, or what this applies to. In this case you are sayign you are doing tests for cargo add.

Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,22 @@ fn rerun_if_env_newly_added_in_config() {
"#]])
.run();
}

#[cargo_test]
fn build_script_debug_assertions_dev() {
// Test that CARGO_CFG_DEBUG_ASSERTIONS is set in dev profile (default)
let build_rs = r#"
fn main() {
let has_debug_assertions = std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some();
assert!(has_debug_assertions, "CARGO_CFG_DEBUG_ASSERTIONS should be set in dev profile");
}
"#;

let p = project()
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();

// Default dev profile has debug-assertions enabled
p.cargo("check").run();
}