Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ let out_dir = env::var("OUT_DIR").unwrap();
> Note that different [target triples][Target Triple] have different sets of `cfg` values,
> hence variables present in one target triple might not be available in the other.
>
> Some cfg values like `debug_assertions` and `test` are not available.
> Some cfg values like `test` are not available.
* `OUT_DIR` --- the folder in which all output and intermediate artifacts should
be placed. This folder is inside the build directory for the package being built,
and it is unique for the package in question.
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
139 changes: 139 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,142 @@ 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();
}

#[cargo_test]
fn build_script_debug_assertions_release() {
// Test that CARGO_CFG_DEBUG_ASSERTIONS is NOT set in release 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 NOT be set in release profile");
}
"#;

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

// Release profile has debug-assertions disabled by default
p.cargo("check --release").run();
}

#[cargo_test]
fn build_script_debug_assertions_override_dev() {
// Test that CARGO_CFG_DEBUG_ASSERTIONS respects profile overrides
// Dev profile with debug-assertions explicitly DISABLED
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 NOT be set when dev profile disables it");
}
"#;

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[profile.dev]
debug-assertions = false
"#,
)
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();

// Dev profile with debug-assertions explicitly disabled
p.cargo("check").run();
}

#[cargo_test]
fn build_script_debug_assertions_override_release() {
// Test that CARGO_CFG_DEBUG_ASSERTIONS respects profile overrides
// Release profile with debug-assertions explicitly ENABLED
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 when release profile enables it");
}
"#;

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[profile.release]
debug-assertions = true
"#,
)
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();

// Release profile with debug-assertions explicitly enabled
p.cargo("check --release").run();
}

#[cargo_test]
fn build_script_debug_assertions_build_override() {
let build_rs = r#"
fn main() {
let profile = std::env::var("PROFILE").unwrap();
if profile == "debug" {
assert!(!cfg!(debug_assertions));
} else if profile == "release" {
assert!(cfg!(debug_assertions));
}
}
"#;

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[profile.dev.build-override]
debug-assertions = false

[profile.release.build-override]
debug-assertions = true
"#,
)
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();

p.cargo("check").run();
p.cargo("check --release").run();
}
Comment on lines +558 to +593
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this checking dev and release? The test is only relevant for the profile build-override is being used on

Copy link
Author

Choose a reason for hiding this comment

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

The test is checking both to verify that build-override correctly affects build script compilation in both profiles. It's showing the override works bidirectionally:
You can disable debug assertions for build scripts in dev (normally they're on)
You can enable debug assertions for build scripts in release (normally they're off)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right, for some reason I had missed the use of build-override in both.

We don't really need tests for each of these two profiles. Once we test things in one profile, its sufficient for all.