Skip to content

Commit c832a7a

Browse files
committed
Avoid calling build_command in cargo fix
1 parent 734569c commit c832a7a

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/cargo/ops/fix.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::collections::{BTreeSet, HashMap, HashSet};
4242
use std::env;
4343
use std::ffi::OsString;
4444
use std::path::{Path, PathBuf};
45-
use std::process::{self, Command, ExitStatus};
45+
use std::process::{self, ExitStatus};
4646
use std::str;
4747

4848
use anyhow::{bail, Context, Error};
@@ -353,9 +353,15 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
353353
.ok();
354354
let mut rustc = ProcessBuilder::new(&args.rustc).wrapped(workspace_rustc.as_ref());
355355
rustc.env_remove(FIX_ENV);
356+
args.apply(&mut rustc);
356357

357358
trace!("start rustfixing {:?}", args.file);
358-
let fixes = rustfix_crate(&lock_addr, &rustc, &args.file, &args, config)?;
359+
let json_error_rustc = {
360+
let mut cmd = rustc.clone();
361+
cmd.arg("--error-format=json");
362+
cmd
363+
};
364+
let fixes = rustfix_crate(&lock_addr, &json_error_rustc, &args.file, &args, config)?;
359365

360366
// Ok now we have our final goal of testing out the changes that we applied.
361367
// If these changes went awry and actually started to cause the crate to
@@ -366,11 +372,8 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
366372
// new rustc, and otherwise we capture the output to hide it in the scenario
367373
// that we have to back it all out.
368374
if !fixes.files.is_empty() {
369-
let mut cmd = rustc.build_command();
370-
args.apply(&mut cmd);
371-
cmd.arg("--error-format=json");
372-
debug!("calling rustc for final verification: {:?}", cmd);
373-
let output = cmd.output().context("failed to spawn rustc")?;
375+
debug!("calling rustc for final verification: {json_error_rustc}");
376+
let output = json_error_rustc.output()?;
374377

375378
if output.status.success() {
376379
for (path, file) in fixes.files.iter() {
@@ -407,15 +410,13 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
407410
// - If the fix failed, show the original warnings and suggestions.
408411
// - If `--broken-code`, show the error messages.
409412
// - If the fix succeeded, show any remaining warnings.
410-
let mut cmd = rustc.build_command();
411-
args.apply(&mut cmd);
412413
for arg in args.format_args {
413414
// Add any json/error format arguments that Cargo wants. This allows
414415
// things like colored output to work correctly.
415-
cmd.arg(arg);
416+
rustc.arg(arg);
416417
}
417-
debug!("calling rustc to display remaining diagnostics: {:?}", cmd);
418-
exit_with(cmd.status().context("failed to spawn rustc")?);
418+
debug!("calling rustc to display remaining diagnostics: {rustc}");
419+
exit_with(rustc.status()?);
419420
}
420421

421422
#[derive(Default)]
@@ -502,7 +503,7 @@ fn rustfix_crate(
502503
// We'll generate new errors below.
503504
file.errors_applying_fixes.clear();
504505
}
505-
rustfix_and_fix(&mut fixes, rustc, filename, args, config)?;
506+
rustfix_and_fix(&mut fixes, rustc, filename, config)?;
506507
let mut progress_yet_to_be_made = false;
507508
for (path, file) in fixes.files.iter_mut() {
508509
if file.errors_applying_fixes.is_empty() {
@@ -543,26 +544,14 @@ fn rustfix_and_fix(
543544
fixes: &mut FixedCrate,
544545
rustc: &ProcessBuilder,
545546
filename: &Path,
546-
args: &FixArgs,
547547
config: &Config,
548548
) -> Result<(), Error> {
549549
// If not empty, filter by these lints.
550550
// TODO: implement a way to specify this.
551551
let only = HashSet::new();
552552

553-
let mut cmd = rustc.build_command();
554-
cmd.arg("--error-format=json");
555-
args.apply(&mut cmd);
556-
debug!(
557-
"calling rustc to collect suggestions and validate previous fixes: {:?}",
558-
cmd
559-
);
560-
let output = cmd.output().with_context(|| {
561-
format!(
562-
"failed to execute `{}`",
563-
rustc.get_program().to_string_lossy()
564-
)
565-
})?;
553+
debug!("calling rustc to collect suggestions and validate previous fixes: {rustc}");
554+
let output = rustc.output()?;
566555

567556
// If rustc didn't succeed for whatever reasons then we're very likely to be
568557
// looking at otherwise broken code. Let's not make things accidentally
@@ -834,7 +823,7 @@ impl FixArgs {
834823
})
835824
}
836825

837-
fn apply(&self, cmd: &mut Command) {
826+
fn apply(&self, cmd: &mut ProcessBuilder) {
838827
cmd.arg(&self.file);
839828
cmd.args(&self.other);
840829
if self.prepare_for_edition.is_some() {

0 commit comments

Comments
 (0)