Don't panic in build.rs when using RA with rustc-wrapper (#3904)

This commit is contained in:
Björn Quentin 2025-08-06 21:21:04 +02:00 committed by GitHub
parent 4dac7bafe2
commit 46fd87347a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View File

@ -22,6 +22,11 @@ macro_rules! assert_unique_features {
}
fn main() -> Result<(), Box<dyn Error>> {
// if using '"rust-analyzer.cargo.buildScripts.useRustcWrapper": true' we can detect this
let suppress_panics = std::env::var("RUSTC_WRAPPER")
.unwrap_or_default()
.contains("rust-analyzer");
println!("cargo:rustc-check-cfg=cfg(is_debug_build)");
if let Ok(level) = std::env::var("OPT_LEVEL")
&& (level == "0" || level == "1")
@ -30,7 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
// If some library required unstable make sure unstable is actually enabled.
if cfg!(feature = "requires-unstable") && !cfg!(feature = "unstable") {
if !suppress_panics && cfg!(feature = "requires-unstable") && !cfg!(feature = "unstable") {
panic!(
"\n\nThe `unstable` feature is required by a dependent crate but is not enabled.\n\n"
);
@ -44,7 +49,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Ensure that exactly one chip has been specified:
let chip = esp_metadata_generated::Chip::from_cargo_feature()?;
if chip.target() != std::env::var("TARGET").unwrap_or_default().as_str() {
if !suppress_panics && chip.target() != std::env::var("TARGET").unwrap_or_default().as_str() {
panic!("
Seems you are building for an unsupported or wrong target (e.g. the host environment).
Maybe you are missing the `target` in `.cargo/config.toml` or you have configs overriding it?

View File

@ -21,6 +21,11 @@ macro_rules! assert_unique_features {
}
fn main() -> Result<(), Box<dyn Error>> {
// if using '"rust-analyzer.cargo.buildScripts.useRustcWrapper": true' we can detect this
let suppress_panics = std::env::var("RUSTC_WRAPPER")
.unwrap_or_default()
.contains("rust-analyzer");
// Load the configuration file for the configured device:
let chip = Chip::from_cargo_feature()?;
@ -28,7 +33,7 @@ fn main() -> Result<(), Box<dyn Error>> {
chip.define_cfgs();
// If some library required unstable make sure unstable is actually enabled.
if cfg!(feature = "requires-unstable") && !cfg!(feature = "unstable") {
if !suppress_panics && cfg!(feature = "requires-unstable") && !cfg!(feature = "unstable") {
panic!(
"\n\nThe `unstable` feature is required by a dependent crate but is not enabled.\n\n"
);
@ -42,6 +47,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|| cfg!(feature = "smoltcp")
|| cfg!(feature = "sniffer"))
&& !cfg!(feature = "unstable")
&& !suppress_panics
{
panic!(
"\n\nThe `unstable` feature was not provided, but is required for the following features: `ble`, `coex`, `csi`, `esp-now`, `ieee802154`, `smoltcp`, `sniffer`.\n\n"