@@ -7,9 +7,10 @@ use crate::core::{profiles::ProfileRoot, PackageId, Target};
7
7
use crate :: util:: errors:: CargoResult ;
8
8
use crate :: util:: machine_message:: { self , Message } ;
9
9
use crate :: util:: { internal, profile} ;
10
- use anyhow:: { bail, Context as _} ;
10
+ use crate :: VerboseError ;
11
+ use anyhow:: { bail, Context as _, Error } ;
11
12
use cargo_platform:: Cfg ;
12
- use cargo_util:: paths;
13
+ use cargo_util:: { paths, ProcessError } ;
13
14
use std:: collections:: hash_map:: { Entry , HashMap } ;
14
15
use std:: collections:: { BTreeSet , HashSet } ;
15
16
use std:: path:: { Path , PathBuf } ;
@@ -386,26 +387,24 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
386
387
let timestamp = paths:: set_invocation_time ( & script_run_dir) ?;
387
388
let prefix = format ! ( "[{} {}] " , id. name( ) , id. version( ) ) ;
388
389
let mut warnings_in_case_of_panic = Vec :: new ( ) ;
389
- let output = cmd
390
- . exec_with_streaming (
391
- & mut |stdout| {
392
- if let Some ( warning) = stdout. strip_prefix ( CARGO_WARNING ) {
393
- warnings_in_case_of_panic. push ( warning. to_owned ( ) ) ;
394
- }
395
- if extra_verbose {
396
- state. stdout ( format ! ( "{}{}" , prefix, stdout) ) ?;
397
- }
398
- Ok ( ( ) )
399
- } ,
400
- & mut |stderr| {
401
- if extra_verbose {
402
- state. stderr ( format ! ( "{}{}" , prefix, stderr) ) ?;
403
- }
404
- Ok ( ( ) )
405
- } ,
406
- true ,
407
- )
408
- . with_context ( || format ! ( "failed to run custom build command for `{}`" , pkg_descr) ) ;
390
+ let output = cmd. exec_with_streaming (
391
+ & mut |stdout| {
392
+ if let Some ( warning) = stdout. strip_prefix ( CARGO_WARNING ) {
393
+ warnings_in_case_of_panic. push ( warning. to_owned ( ) ) ;
394
+ }
395
+ if extra_verbose {
396
+ state. stdout ( format ! ( "{}{}" , prefix, stdout) ) ?;
397
+ }
398
+ Ok ( ( ) )
399
+ } ,
400
+ & mut |stderr| {
401
+ if extra_verbose {
402
+ state. stderr ( format ! ( "{}{}" , prefix, stderr) ) ?;
403
+ }
404
+ Ok ( ( ) )
405
+ } ,
406
+ true ,
407
+ ) ;
409
408
410
409
if let Err ( error) = output {
411
410
insert_warnings_in_build_outputs (
@@ -414,7 +413,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
414
413
metadata_hash,
415
414
warnings_in_case_of_panic,
416
415
) ;
417
- return Err ( error) ;
416
+ return Err ( build_error_with_context ( error, & pkg_descr ) ) ;
418
417
}
419
418
420
419
let output = output. unwrap ( ) ;
@@ -496,6 +495,42 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
496
495
Ok ( job)
497
496
}
498
497
498
+ fn build_error_with_context ( error : Error , pkg_descr : & str ) -> Error {
499
+ let Some ( perr) = error. downcast_ref :: < ProcessError > ( ) else {
500
+ return error. context ( format ! ( "failed to run custom build command for `{}`" , pkg_descr) ) ;
501
+ } ;
502
+
503
+ let mut msg = format ! ( "custom build command for `{}` has failed" , pkg_descr) ;
504
+
505
+ let mut parsing_stdout = false ;
506
+ let output = perr
507
+ . stderr
508
+ . as_ref ( )
509
+ . and_then ( |stderr| std:: str:: from_utf8 ( stderr) . ok ( ) )
510
+ . filter ( |stderr| !stderr. trim_start ( ) . is_empty ( ) )
511
+ . or_else ( || {
512
+ parsing_stdout = true ;
513
+ perr. stdout
514
+ . as_ref ( )
515
+ . and_then ( |stdout| std:: str:: from_utf8 ( stdout) . ok ( ) )
516
+ } ) ;
517
+
518
+ if let Some ( out) = output {
519
+ let mut skipping_backtrace = false ;
520
+ for line in out. lines ( ) {
521
+ // Cargo directives aren't part of the error message text (except cargo:warning, which is reported separately).
522
+ // Some scripts print a lot of cargo:rerun-if-env-changed that obscure the root cause of the failure.
523
+ if parsing_stdout && line. starts_with ( "cargo:" ) {
524
+ continue ;
525
+ }
526
+
527
+ msg. push_str ( "\n " ) ;
528
+ msg. push_str ( line) ;
529
+ }
530
+ }
531
+ Error :: from ( VerboseError :: new ( error) ) . context ( msg)
532
+ }
533
+
499
534
fn insert_warnings_in_build_outputs (
500
535
build_script_outputs : Arc < Mutex < BuildScriptOutputs > > ,
501
536
id : PackageId ,
0 commit comments