@@ -38,13 +38,7 @@ use serde::{Deserialize, Serialize};
3838use std:: io:: { BufReader , Write } ;
3939use std:: process:: { Command , ExitStatus , Output , Stdio , exit} ;
4040use std:: {
41- ffi:: OsString ,
42- fmt:: Display ,
43- fs:: read_to_string,
44- io:: BufRead ,
45- path:: PathBuf ,
46- str:: FromStr ,
47- sync:: { Arc , Mutex } ,
41+ ffi:: OsString , fmt:: Display , fs:: read_to_string, io:: BufRead , path:: PathBuf , str:: FromStr ,
4842} ;
4943
5044use crate :: error:: Context ;
@@ -365,53 +359,50 @@ impl CommandExt for Command {
365359 . with_context ( || format ! ( "failed to run command `{cmdline}`" ) ) ?;
366360
367361 let mut stdout = child. stdout . take ( ) . map ( BufReader :: new) . unwrap ( ) ;
368- let stdout_lines = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
369- let stdout_lines_ = stdout_lines. clone ( ) ;
370- std:: thread:: spawn ( move || {
362+ let stdout_thread = std:: thread:: spawn ( move || {
371363 let mut line = String :: new ( ) ;
372- if let Ok ( mut lines) = stdout_lines_. lock ( ) {
373- loop {
374- line. clear ( ) ;
375- match stdout. read_line ( & mut line) {
376- Ok ( 0 ) => break ,
377- Ok ( _) => {
378- log:: debug!( action = "stdout" ; "{}" , line. trim_end( ) ) ;
379- lines. extend ( line. as_bytes ( ) ) ;
380- }
381- Err ( _) => ( ) ,
364+ let mut lines = Vec :: new ( ) ;
365+ loop {
366+ line. clear ( ) ;
367+ match stdout. read_line ( & mut line) {
368+ Ok ( 0 ) => break ,
369+ Ok ( _) => {
370+ log:: debug!( action = "stdout" ; "{}" , line. trim_end( ) ) ;
371+ lines. extend ( line. as_bytes ( ) ) ;
382372 }
373+ Err ( _) => ( ) ,
383374 }
384375 }
376+ lines
385377 } ) ;
386378
387379 let mut stderr = child. stderr . take ( ) . map ( BufReader :: new) . unwrap ( ) ;
388- let stderr_lines = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
389- let stderr_lines_ = stderr_lines. clone ( ) ;
390- std:: thread:: spawn ( move || {
380+ let stderr_thread = std:: thread:: spawn ( move || {
391381 let mut line = String :: new ( ) ;
392- if let Ok ( mut lines) = stderr_lines_. lock ( ) {
393- loop {
394- line. clear ( ) ;
395- match stderr. read_line ( & mut line) {
396- Ok ( 0 ) => break ,
397- Ok ( _) => {
398- log:: debug!( action = "stderr" ; "{}" , line. trim_end( ) ) ;
399- lines. extend ( line. as_bytes ( ) ) ;
400- }
401- Err ( _) => ( ) ,
382+ let mut lines = Vec :: new ( ) ;
383+ loop {
384+ line. clear ( ) ;
385+ match stderr. read_line ( & mut line) {
386+ Ok ( 0 ) => break ,
387+ Ok ( _) => {
388+ log:: debug!( action = "stderr" ; "{}" , line. trim_end( ) ) ;
389+ lines. extend ( line. as_bytes ( ) ) ;
402390 }
391+ Err ( _) => ( ) ,
403392 }
404393 }
394+ lines
405395 } ) ;
406396
407397 let status = child
408398 . wait ( )
409399 . with_context ( || format ! ( "failed to run command `{cmdline}`" ) ) ?;
410400
401+ // wait for the readers to reach EOF so the output is complete
411402 let output = Output {
412403 status,
413- stdout : std :: mem :: take ( & mut * stdout_lines . lock ( ) . unwrap ( ) ) ,
414- stderr : std :: mem :: take ( & mut * stderr_lines . lock ( ) . unwrap ( ) ) ,
404+ stdout : stdout_thread . join ( ) . unwrap_or_default ( ) ,
405+ stderr : stderr_thread . join ( ) . unwrap_or_default ( ) ,
415406 } ;
416407
417408 if output. status . success ( ) {
@@ -441,4 +432,19 @@ mod tests {
441432 let help = Cli :: command ( ) . render_help ( ) . to_string ( ) ;
442433 assert ! ( help. contains( "Build" ) ) ;
443434 }
435+
436+ #[ cfg( unix) ]
437+ #[ test]
438+ fn output_ok_captures_all_output ( ) {
439+ use crate :: CommandExt ;
440+
441+ for _ in 0 ..20 {
442+ let output = std:: process:: Command :: new ( "sh" )
443+ . args ( [ "-c" , "echo out1; echo err1 >&2; echo out2; echo err2 >&2" ] )
444+ . output_ok ( )
445+ . unwrap ( ) ;
446+ assert_eq ! ( output. stdout, b"out1\n out2\n " ) ;
447+ assert_eq ! ( output. stderr, b"err1\n err2\n " ) ;
448+ }
449+ }
444450}
0 commit comments