Skip to content

Commit

Permalink
improve debuginfo error message when build script fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lqd committed Jan 31, 2023
1 parent 779b3c1 commit 066da36
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
// Need a separate copy for the fresh closure.
let targets_fresh = targets.clone();

let env_profile_name = unit.profile.name.to_uppercase();

// Prepare the unit of "dirty work" which will actually run the custom build
// command.
//
Expand Down Expand Up @@ -406,15 +408,19 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
true,
)
.with_context(|| {
let mut build_error_context = format!("failed to run custom build command for `{}`", pkg_descr);
let mut build_error_context =
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.
if let Ok(show_backtraces) = std::env::var("RUST_BACKTRACE") {
if show_backtraces != "0" {
build_error_context.push_str("\n\
note: To improve backtraces for build dependencies, make sure full debug info is turned on. \
More details at https://doc.rust-lang.org/cargo/reference/profiles.html#build-dependencies");
build_error_context.push_str(&format!(
"\n\
note: To improve backtraces for build dependencies, set the \
CARGO_PROFILE_{env_profile_name}_BUILD_OVERRIDE_DEBUG=true environment \
variable to enable debug information generation.",
));
}
}

Expand Down
19 changes: 18 additions & 1 deletion tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,24 @@ fn custom_build_script_failed_backtraces_message() {
[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])`
note: To improve backtraces for build dependencies[..]
note: To improve backtraces for build dependencies, set the \
CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable [..]
Caused by:
process didn't exit successfully: `[..]/build-script-build` (exit [..]: 101)",
)
.run();

p.cargo("check -v")
.env("RUST_BACKTRACE", "1")
.with_status(101)
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([CWD])
[RUNNING] `[..]/build-script-build`
[ERROR] failed to run custom build command for `foo v0.5.0 ([CWD])`
note: To improve backtraces for build dependencies, set the \
CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable [..]
Caused by:
process didn't exit successfully: `[..]/build-script-build` (exit [..]: 101)",
Expand Down

0 comments on commit 066da36

Please sign in to comment.