Skip to content

Conversation

@moabo3li
Copy link
Contributor

@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
Contributor 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
@moabo3li
Copy link
Contributor Author

moabo3li commented Nov 1, 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)

Is there anything still needed to merge this?

@weihanglo
Copy link
Member

As this is adding a user-facing change that we cannot revert after stabilization, we usually go through the FCP for this kind of one-way-door change. See #16160 (comment) for more about FCP process.

In the meanwhile you can address nits Ed had suggested, but in general nothing major is needed unless some concern is raised

@ehuss ehuss moved this to FCP merge in Cargo status tracker Nov 4, 2025
@rust-rfcbot rust-rfcbot added final-comment-period FCP — a period for last comments before action is taken and removed proposed-final-comment-period An FCP proposal has started, but not yet signed off. labels Nov 11, 2025
@rust-rfcbot
Copy link
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@rust-rfcbot rust-rfcbot added finished-final-comment-period FCP complete to-announce and removed final-comment-period FCP — a period for last comments before action is taken labels Nov 21, 2025
@rust-rfcbot
Copy link
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@epage epage added this pull request to the merge queue Nov 21, 2025
@epage
Copy link
Contributor

epage commented Nov 21, 2025

Thanks!

Merged via the queue into rust-lang:master with commit 9fa462f Nov 21, 2025
27 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 21, 2025
bors added a commit to rust-lang/rust that referenced this pull request Nov 22, 2025
Update cargo submodule

7 commits in 5c0343317ce45d2ec17ecf41eaa473a02d73e29c..9fa462fe3a81e07e0bfdcc75c29d312c55113ebb
2025-11-18 19:05:44 +0000 to 2025-11-21 20:49:51 +0000
- Enable CARGO_CFG_DEBUG_ASSERTIONS in build scripts based on profile (rust-lang/cargo#16160)
- fix(config-include): disallow glob and template syntax (rust-lang/cargo#16285)
- test(config-include): include always relative to including config (rust-lang/cargo#16286)
- docs(guide): When suggesting alt dev profile, link to related issue (rust-lang/cargo#16275)
- refactor(timings): separate data collection and presentation (rust-lang/cargo#16282)
- test(build-std): Add test for LTO (rust-lang/cargo#16277)
- fix(bindeps): do not propagate artifact dependency to proc macro or build deps (rust-lang/cargo#15788)

r? ghost
@rustbot rustbot added this to the 1.93.0 milestone Nov 22, 2025
@moabo3li moabo3li deleted the debug_assertion_handling branch November 22, 2025 20:35
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 finished-final-comment-period FCP complete T-cargo Team: Cargo to-announce

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Allow build scripts to detect debug_assertions setting

6 participants