Skip to content

Commit 5e8827f

Browse files
committed
Debug assertion for rustc argfile invocations
1 parent 3f749f6 commit 5e8827f

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

crates/cargo-util/src/process_builder.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,13 @@ impl ProcessBuilder {
218218
}
219219

220220
fn _status(&self) -> io::Result<ExitStatus> {
221-
let mut cmd = self.build_command();
222-
match cmd.spawn() {
223-
Err(ref e) if self.should_retry_with_argfile(e) => {}
224-
Err(e) => return Err(e),
225-
Ok(mut child) => return child.wait(),
221+
if !debug_force_argfile(self.retry_with_argfile) {
222+
let mut cmd = self.build_command();
223+
match cmd.spawn() {
224+
Err(ref e) if self.should_retry_with_argfile(e) => {}
225+
Err(e) => return Err(e),
226+
Ok(mut child) => return child.wait(),
227+
}
226228
}
227229
let (mut cmd, _argfile) = self.build_command_with_argfile()?;
228230
cmd.spawn()?.wait()
@@ -269,11 +271,13 @@ impl ProcessBuilder {
269271
}
270272

271273
fn _output(&self) -> io::Result<Output> {
272-
let mut cmd = self.build_command();
273-
match piped(&mut cmd).spawn() {
274-
Err(ref e) if self.should_retry_with_argfile(e) => {}
275-
Err(e) => return Err(e),
276-
Ok(child) => return child.wait_with_output(),
274+
if !debug_force_argfile(self.retry_with_argfile) {
275+
let mut cmd = self.build_command();
276+
match piped(&mut cmd).spawn() {
277+
Err(ref e) if self.should_retry_with_argfile(e) => {}
278+
Err(e) => return Err(e),
279+
Ok(child) => return child.wait_with_output(),
280+
}
277281
}
278282
let (mut cmd, _argfile) = self.build_command_with_argfile()?;
279283
piped(&mut cmd).spawn()?.wait_with_output()
@@ -317,11 +321,13 @@ impl ProcessBuilder {
317321
let mut stderr_pos = 0;
318322

319323
let spawn = |mut cmd| {
320-
match piped(&mut cmd).spawn() {
321-
Err(ref e) if self.should_retry_with_argfile(e) => {}
322-
Err(e) => return Err(e),
323-
Ok(child) => return Ok((child, None)),
324-
};
324+
if !debug_force_argfile(self.retry_with_argfile) {
325+
match piped(&mut cmd).spawn() {
326+
Err(ref e) if self.should_retry_with_argfile(e) => {}
327+
Err(e) => return Err(e),
328+
Ok(child) => return Ok((child, None)),
329+
}
330+
}
325331
let (mut cmd, argfile) = self.build_command_with_argfile()?;
326332
Ok((piped(&mut cmd).spawn()?, Some(argfile)))
327333
};
@@ -506,6 +512,13 @@ impl ProcessBuilder {
506512
}
507513
}
508514

515+
/// Forces the command to use `@path` argfile.
516+
///
517+
/// You should set `__CARGO_TEST_FORCE_ARGFILE` to enable this.
518+
fn debug_force_argfile(retry_enabled: bool) -> bool {
519+
cfg!(debug_assertions) && env::var("__CARGO_TEST_FORCE_ARGFILE").is_ok() && retry_enabled
520+
}
521+
509522
/// Creates new pipes for stderr and stdout. Ignores stdin.
510523
fn piped(cmd: &mut Command) -> &mut Command {
511524
cmd.stdout(Stdio::piped())
@@ -515,18 +528,23 @@ fn piped(cmd: &mut Command) -> &mut Command {
515528

516529
#[cfg(unix)]
517530
mod imp {
518-
use super::{ProcessBuilder, ProcessError};
531+
use super::{debug_force_argfile, ProcessBuilder, ProcessError};
519532
use anyhow::Result;
520533
use std::io;
521534
use std::os::unix::process::CommandExt;
522535

523536
pub fn exec_replace(process_builder: &ProcessBuilder) -> Result<()> {
524-
let mut command = process_builder.build_command();
525-
526-
let mut error = command.exec();
527-
if process_builder.should_retry_with_argfile(&error) {
537+
let mut error;
538+
if debug_force_argfile(process_builder.retry_with_argfile) {
528539
let (mut command, _argfile) = process_builder.build_command_with_argfile()?;
529540
error = command.exec()
541+
} else {
542+
let mut command = process_builder.build_command();
543+
error = command.exec();
544+
if process_builder.should_retry_with_argfile(&error) {
545+
let (mut command, _argfile) = process_builder.build_command_with_argfile()?;
546+
error = command.exec()
547+
}
530548
}
531549

532550
Err(anyhow::Error::from(error).context(ProcessError::new(

0 commit comments

Comments
 (0)