Skip to content

Conversation

@moabo3li
Copy link

@moabo3li moabo3li commented Oct 27, 2025

Build scripts need to know whether debug assertions are enabled to properly configure dependencies with matching assertion behavior. Previously, CARGO_CFG_DEBUG_ASSERTIONS was filtered out because the cfg query to rustc doesn't include profile settings.

This PR manually sets CARGO_CFG_DEBUG_ASSERTIONS based on the profile's debug-assertions setting, allowing build scripts to configure dependencies appropriately.

FCP: #16160 (comment)

Closes #15760

@rustbot rustbot added A-build-scripts Area: build.rs scripts A-profiles Area: profiles S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 27, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 27, 2025

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@moabo3li moabo3li force-pushed the debug_assertion_handling branch from 76b11fa to 72144ac Compare October 27, 2025 07:40
@moabo3li moabo3li force-pushed the debug_assertion_handling branch from e0ee77d to 7d83b95 Compare October 28, 2025 13:20
@rustbot rustbot added the A-infrastructure Area: infrastructure around the cargo repo, ci, releases, etc. label Oct 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 28, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@moabo3li moabo3li force-pushed the debug_assertion_handling branch from 7d83b95 to ee5e6e3 Compare October 28, 2025 13:35
Comment on lines +558 to +593
#[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();
}
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.

@rustbot rustbot added the A-documenting-cargo-itself Area: Cargo's documentation label Oct 28, 2025
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.

Copy link
Contributor

@epage epage left a comment

Choose a reason for hiding this comment

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

While some nits, this is good enough to go in. Thanks for your work on this!

View changes since this review

@epage epage added the T-cargo Team: Cargo label Oct 28, 2025
@epage
Copy link
Contributor

epage commented Oct 28, 2025

@rfcbot fcp merge

Like with #14902, this adds an artificial CARGO_CFG to build scripts for --cfg debug_assertions. For more background, see #15760 (comment)

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Oct 28, 2025

Team member @epage has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-scripts Area: build.rs scripts A-documenting-cargo-itself Area: Cargo's documentation A-infrastructure Area: infrastructure around the cargo repo, ci, releases, etc. A-profiles Area: profiles disposition-merge FCP with intent to merge proposed-final-comment-period An FCP proposal has started, but not yet signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Team: Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow build scripts to detect debug_assertions setting

5 participants