@@ -226,8 +226,10 @@ impl ProcessBuilder {
226
226
Ok ( mut child) => return child. wait ( ) ,
227
227
}
228
228
}
229
- let ( mut cmd, _argfile) = self . build_command_with_argfile ( ) ?;
230
- cmd. spawn ( ) ?. wait ( )
229
+ let ( mut cmd, argfile) = self . build_command_with_argfile ( ) ?;
230
+ let status = cmd. spawn ( ) ?. wait ( ) ;
231
+ close_tempfile_and_log_error ( argfile) ;
232
+ status
231
233
}
232
234
233
235
/// Runs the process, waiting for completion, and mapping non-success exit codes to an error.
@@ -279,8 +281,10 @@ impl ProcessBuilder {
279
281
Ok ( child) => return child. wait_with_output ( ) ,
280
282
}
281
283
}
282
- let ( mut cmd, _argfile) = self . build_command_with_argfile ( ) ?;
283
- piped ( & mut cmd) . spawn ( ) ?. wait_with_output ( )
284
+ let ( mut cmd, argfile) = self . build_command_with_argfile ( ) ?;
285
+ let output = piped ( & mut cmd) . spawn ( ) ?. wait_with_output ( ) ;
286
+ close_tempfile_and_log_error ( argfile) ;
287
+ output
284
288
}
285
289
286
290
/// Executes the process, returning the stdio output, or an error if non-zero exit status.
@@ -334,7 +338,7 @@ impl ProcessBuilder {
334
338
335
339
let status = ( || {
336
340
let cmd = self . build_command ( ) ;
337
- let ( mut child, _argfile ) = spawn ( cmd) ?;
341
+ let ( mut child, argfile ) = spawn ( cmd) ?;
338
342
let out = child. stdout . take ( ) . unwrap ( ) ;
339
343
let err = child. stderr . take ( ) . unwrap ( ) ;
340
344
read2 ( out, err, & mut |is_out, data, eof| {
@@ -380,7 +384,11 @@ impl ProcessBuilder {
380
384
data. drain ( ..idx) ;
381
385
* pos = 0 ;
382
386
} ) ?;
383
- child. wait ( )
387
+ let status = child. wait ( ) ;
388
+ if let Some ( argfile) = argfile {
389
+ close_tempfile_and_log_error ( argfile) ;
390
+ }
391
+ status
384
392
} ) ( )
385
393
. with_context ( || ProcessError :: could_not_execute ( self ) ) ?;
386
394
let output = Output {
@@ -526,26 +534,38 @@ fn piped(cmd: &mut Command) -> &mut Command {
526
534
. stdin ( Stdio :: null ( ) )
527
535
}
528
536
537
+ fn close_tempfile_and_log_error ( file : NamedTempFile ) {
538
+ file. close ( ) . unwrap_or_else ( |e| {
539
+ log:: warn!( "failed to close temporary file: {e}" ) ;
540
+ } ) ;
541
+ }
542
+
529
543
#[ cfg( unix) ]
530
544
mod imp {
531
- use super :: { debug_force_argfile, ProcessBuilder , ProcessError } ;
545
+ use super :: { close_tempfile_and_log_error , debug_force_argfile, ProcessBuilder , ProcessError } ;
532
546
use anyhow:: Result ;
533
547
use std:: io;
534
548
use std:: os:: unix:: process:: CommandExt ;
535
549
536
550
pub fn exec_replace ( process_builder : & ProcessBuilder ) -> Result < ( ) > {
537
551
let mut error;
552
+ let mut file = None ;
538
553
if debug_force_argfile ( process_builder. retry_with_argfile ) {
539
- let ( mut command, _argfile) = process_builder. build_command_with_argfile ( ) ?;
554
+ let ( mut command, argfile) = process_builder. build_command_with_argfile ( ) ?;
555
+ file = Some ( argfile) ;
540
556
error = command. exec ( )
541
557
} else {
542
558
let mut command = process_builder. build_command ( ) ;
543
559
error = command. exec ( ) ;
544
560
if process_builder. should_retry_with_argfile ( & error) {
545
- let ( mut command, _argfile) = process_builder. build_command_with_argfile ( ) ?;
561
+ let ( mut command, argfile) = process_builder. build_command_with_argfile ( ) ?;
562
+ file = Some ( argfile) ;
546
563
error = command. exec ( )
547
564
}
548
565
}
566
+ if let Some ( file) = file {
567
+ close_tempfile_and_log_error ( file) ;
568
+ }
549
569
550
570
Err ( anyhow:: Error :: from ( error) . context ( ProcessError :: new (
551
571
& format ! ( "could not execute process {}" , process_builder) ,
0 commit comments