-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Enable CARGO_CFG_DEBUG_ASSERTIONS in build scripts based on profile #16160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5ca05d3
8c298d0
90d4e7a
be3ebae
cf9c97e
ee5e6e3
2eb1ecc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
moabo3li marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For future reference, in Conventional commits, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, for some reason I had missed the use of We don't really need tests for each of these two profiles. Once we test things in one profile, its sufficient for all. |
||
Uh oh!
There was an error while loading. Please reload this page.