From 7ccda69f700afa70ca0dffa658ac0307379e9299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Tue, 31 Jan 2023 22:09:54 +0000 Subject: [PATCH] do not display message to enable debuginfo when it's already enabled --- src/cargo/core/compiler/custom_build.rs | 12 ++++++-- tests/testsuite/build_script.rs | 37 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 97764ab25d3..11c3f1a24e4 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -333,6 +333,13 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { let targets_fresh = targets.clone(); let env_profile_name = unit.profile.name.to_uppercase(); + let built_with_debuginfo = cx + .bcx + .unit_graph + .get(unit) + .and_then(|deps| deps.iter().find(|dep| dep.unit.target == unit.target)) + .map(|dep| dep.unit.profile.debuginfo.is_turned_on()) + .unwrap_or(false); // Prepare the unit of "dirty work" which will actually run the custom build // command. @@ -412,9 +419,10 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { format!("failed to run custom build command for `{}`", pkg_descr); // If we're opting into backtraces, mention that build dependencies' backtraces can - // be improved by setting a higher debuginfo level. + // be improved by requesting debuginfo to be built, if we're not building with + // debuginfo already. if let Ok(show_backtraces) = std::env::var("RUST_BACKTRACE") { - if show_backtraces != "0" { + if !built_with_debuginfo && show_backtraces != "0" { build_error_context.push_str(&format!( "\n\ note: To improve backtraces for build dependencies, set the \ diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 783a86bdde6..d5ee1f99c9a 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -102,6 +102,43 @@ Caused by: .run(); } +#[cargo_test] +fn custom_build_script_failed_backtraces_message_with_debuginfo() { + // This is the same test as `custom_build_script_failed_backtraces_message` above, this time + // ensuring that the message dedicated to improving backtraces by requesting debuginfo is not + // shown when debuginfo is already turned on. + let p = project() + .file( + "Cargo.toml", + r#" + [package] + + name = "foo" + version = "0.5.0" + authors = ["wycats@example.com"] + build = "build.rs" + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("build.rs", "fn main() { std::process::exit(101); }") + .build(); + p.cargo("build -v") + .env("RUST_BACKTRACE", "1") + .env("CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG", "true") + .with_status(101) + .with_stderr( + "\ +[COMPILING] foo v0.5.0 ([CWD]) +[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]` +[RUNNING] `[..]/build-script-build` +[ERROR] failed to run custom build command for `foo v0.5.0 ([CWD])` + +Caused by: + process didn't exit successfully: `[..]/build-script-build` (exit [..]: 101)", + ) + .run(); +} + #[cargo_test] fn custom_build_env_vars() { let p = project()