@@ -607,7 +607,7 @@ impl Build {
607607 let output = self
608608 . run (
609609 helpers:: git ( Some ( & self . src ) )
610- . quiet ( )
610+ . capture ( )
611611 . args ( [ "config" , "--file" ] )
612612 . arg ( self . config . src . join ( ".gitmodules" ) )
613613 . args ( [ "--get-regexp" , "path" ] ) ,
@@ -971,60 +971,62 @@ impl Build {
971971
972972 self . verbose ( || println ! ( "running: {command:?}" ) ) ;
973973
974- let output_mode = command. output_mode . unwrap_or_else ( || match self . is_verbose ( ) {
975- true => OutputMode :: All ,
976- false => OutputMode :: OnlyOutput ,
977- } ) ;
978- let ( output, print_error) : ( io:: Result < CommandOutput > , bool ) = match output_mode {
979- mode @ ( OutputMode :: All | OutputMode :: OnlyOutput ) => (
980- command. command . status ( ) . map ( |status| status. into ( ) ) ,
981- matches ! ( mode, OutputMode :: All ) ,
982- ) ,
983- mode @ ( OutputMode :: OnlyOnFailure | OutputMode :: Quiet ) => (
984- command. command . output ( ) . map ( |o| o. into ( ) ) ,
985- matches ! ( mode, OutputMode :: OnlyOnFailure ) ,
986- ) ,
974+ let output: io:: Result < CommandOutput > = match command. output_mode {
975+ OutputMode :: Print => command. command . status ( ) . map ( |status| status. into ( ) ) ,
976+ OutputMode :: CaptureAll => command. command . output ( ) . map ( |o| o. into ( ) ) ,
977+ OutputMode :: CaptureStdout => {
978+ command. command . stderr ( Stdio :: inherit ( ) ) ;
979+ command. command . output ( ) . map ( |o| o. into ( ) )
980+ }
987981 } ;
988982
989983 let output = match output {
990984 Ok ( output) => output,
991- Err ( e) => fail ( & format ! ( "failed to execute command: {:?}\n error: {}" , command , e ) ) ,
985+ Err ( e) => fail ( & format ! ( "failed to execute command: {command :?}\n error: {e}" ) ) ,
992986 } ;
993987 if !output. is_success ( ) {
994- if print_error {
995- println ! (
996- "\n \n Command did not execute successfully.\
997- \n Expected success, got: {}",
998- output. status( ) ,
999- ) ;
988+ use std:: fmt:: Write ;
989+
990+ // Here we build an error message, and below we decide if it should be printed or not.
991+ let mut message = String :: new ( ) ;
992+ writeln ! (
993+ message,
994+ "\n \n Command {command:?} did not execute successfully.\
995+ \n Expected success, got: {}",
996+ output. status( ) ,
997+ )
998+ . unwrap ( ) ;
1000999
1001- if !self . is_verbose ( ) {
1002- println ! ( "Add `-v` to see more details.\n " ) ;
1003- }
1000+ // If the output mode is OutputMode::Print, the output has already been printed to
1001+ // stdout/stderr, and we thus don't have anything captured to print anyway.
1002+ if matches ! ( command. output_mode, OutputMode :: CaptureAll | OutputMode :: CaptureStdout ) {
1003+ writeln ! ( message, "\n STDOUT ----\n {}" , output. stdout( ) . trim( ) ) . unwrap ( ) ;
10041004
1005- self . verbose ( || {
1006- println ! (
1007- "\n STDOUT ----\n {}\n \
1008- STDERR ----\n {}\n ",
1009- output. stdout( ) ,
1010- output. stderr( ) ,
1011- )
1012- } ) ;
1005+ // Stderr is added to the message only if it was captured
1006+ if matches ! ( command. output_mode, OutputMode :: CaptureAll ) {
1007+ writeln ! ( message, "\n STDERR ----\n {}" , output. stderr( ) . trim( ) ) . unwrap ( ) ;
1008+ }
10131009 }
10141010
10151011 match command. failure_behavior {
10161012 BehaviorOnFailure :: DelayFail => {
10171013 if self . fail_fast {
1014+ println ! ( "{message}" ) ;
10181015 exit ! ( 1 ) ;
10191016 }
10201017
10211018 let mut failures = self . delayed_failures . borrow_mut ( ) ;
1022- failures. push ( format ! ( "{command:?}" ) ) ;
1019+ failures. push ( message ) ;
10231020 }
10241021 BehaviorOnFailure :: Exit => {
1022+ println ! ( "{message}" ) ;
10251023 exit ! ( 1 ) ;
10261024 }
1027- BehaviorOnFailure :: Ignore => { }
1025+ BehaviorOnFailure :: Ignore => {
1026+ // If failures are allowed, either the error has been printed already
1027+ // (OutputMode::Print) or the user used a capture output mode and wants to
1028+ // handle the error output on their own.
1029+ }
10281030 }
10291031 }
10301032 output
@@ -1515,7 +1517,7 @@ impl Build {
15151517 // (Note that we use a `..` range, not the `...` symmetric difference.)
15161518 self . run (
15171519 helpers:: git ( Some ( & self . src ) )
1518- . quiet ( )
1520+ . capture ( )
15191521 . arg ( "rev-list" )
15201522 . arg ( "--count" )
15211523 . arg ( "--merges" )
0 commit comments