Skip to content

Commit fb7094f

Browse files
committed
Trim panic boilerplate from terse version of build script output
1 parent 6d8662f commit fb7094f

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/cargo/core/compiler/custom_build.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ fn build_error_with_context(error: Error, pkg_descr: &str) -> Error {
501501
};
502502

503503
let mut msg = format!("custom build command for `{}` has failed", pkg_descr);
504+
let may_be_panic = perr.code == Some(101);
504505

505506
let mut parsing_stdout = false;
506507
let output = perr
@@ -517,13 +518,41 @@ fn build_error_with_context(error: Error, pkg_descr: &str) -> Error {
517518

518519
if let Some(out) = output {
519520
let mut skipping_backtrace = false;
520-
for line in out.lines() {
521+
for mut line in out.lines() {
521522
// Cargo directives aren't part of the error message text (except cargo:warning, which is reported separately).
522523
// Some scripts print a lot of cargo:rerun-if-env-changed that obscure the root cause of the failure.
523524
if parsing_stdout && line.starts_with("cargo:") {
524525
continue;
525526
}
526527

528+
if !parsing_stdout && may_be_panic {
529+
// Panics print a backtrace, but the location within the build script is not interesting to downstream users
530+
if line == "stack backtrace:" {
531+
skipping_backtrace = true;
532+
continue;
533+
}
534+
if line == "note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace." || line == "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace" {
535+
skipping_backtrace = false;
536+
continue;
537+
}
538+
if skipping_backtrace {
539+
if line.starts_with(" ") {
540+
continue;
541+
}
542+
skipping_backtrace = false;
543+
}
544+
// Build scripts tend to unwrap() errors. This skips the most common panic message boilerplate.
545+
if let Some(rest) = line.strip_prefix("thread 'main' panicked at ") {
546+
line = rest;
547+
if let Some(rest) =
548+
line.strip_prefix("'called `Result::unwrap()` on an `Err` value: ")
549+
{
550+
msg.push('\''); // re-add stripped quote start
551+
line = rest;
552+
}
553+
}
554+
}
555+
527556
msg.push_str("\n ");
528557
msg.push_str(line);
529558
}

tests/testsuite/build_script.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,7 @@ fn custom_build_script_failed_terse() {
7272
"\
7373
[COMPILING] foo v0.5.0 ([CWD])
7474
[ERROR] custom build command for `foo v0.5.0 ([CWD])` has failed
75-
thread 'main' panicked at 'oops', build.rs:2:8
76-
stack backtrace:
77-
0: [..]
78-
at [..]
79-
1: build_script_build::main
80-
at [..]
81-
2: [..]
82-
at [..]
83-
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
75+
'oops', build.rs:2:8
8476
[NOTE] for more details, run again with '--verbose'",
8577
)
8678
.run();

0 commit comments

Comments
 (0)