Skip to content

Commit 39fe0e1

Browse files
authored
Use rustfmt given by RUSTFMT env var (#4419)
1 parent 3c19c4d commit 39fe0e1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Diff for: config_proc_macro/src/utils.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use proc_macro2::TokenStream;
22
use quote::{quote, ToTokens};
3+
use std::env;
4+
use std::ffi::OsStr;
35

46
pub fn fold_quote<F, I, T>(input: impl Iterator<Item = I>, f: F) -> TokenStream
57
where
@@ -21,7 +23,12 @@ pub(crate) fn debug_with_rustfmt(input: &TokenStream) {
2123
use std::io::Write;
2224
use std::process::{Command, Stdio};
2325

24-
let mut child = Command::new("rustfmt")
26+
let rustfmt_var = env::var_os("RUSTFMT");
27+
let rustfmt = match &rustfmt_var {
28+
Some(rustfmt) => rustfmt,
29+
None => OsStr::new("rustfmt"),
30+
};
31+
let mut child = Command::new(rustfmt)
2532
.stdin(Stdio::piped())
2633
.stdout(Stdio::piped())
2734
.spawn()

Diff for: src/cargo-fmt/main.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::cmp::Ordering;
66
use std::collections::{BTreeMap, BTreeSet};
77
use std::env;
8+
use std::ffi::OsStr;
89
use std::fs;
910
use std::hash::{Hash, Hasher};
1011
use std::io::{self, Write};
@@ -149,6 +150,15 @@ fn is_status_options(s: &str) -> bool {
149150
|| s.starts_with("--print-config=")
150151
}
151152

153+
fn rustfmt_command() -> Command {
154+
let rustfmt_var = env::var_os("RUSTFMT");
155+
let rustfmt = match &rustfmt_var {
156+
Some(rustfmt) => rustfmt,
157+
None => OsStr::new("rustfmt"),
158+
};
159+
Command::new(rustfmt)
160+
}
161+
152162
fn build_rustfmt_args(opts: &Opts, rustfmt_args: &mut Vec<String>) -> Result<(), String> {
153163
let mut contains_check = false;
154164
let mut contains_emit_mode = false;
@@ -240,7 +250,7 @@ fn handle_command_status(status: Result<i32, io::Error>) -> i32 {
240250
}
241251

242252
fn get_rustfmt_info(args: &[String]) -> Result<i32, io::Error> {
243-
let mut command = Command::new("rustfmt")
253+
let mut command = rustfmt_command()
244254
.stdout(std::process::Stdio::inherit())
245255
.args(args)
246256
.spawn()
@@ -625,7 +635,7 @@ fn run_rustfmt(
625635
println!();
626636
}
627637

628-
let mut command = Command::new("rustfmt")
638+
let mut command = rustfmt_command()
629639
.stdout(stdout)
630640
.args(files)
631641
.args(&["--edition", edition])

Diff for: src/format-diff/main.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use serde_json as json;
1111
use thiserror::Error;
1212

1313
use std::collections::HashSet;
14+
use std::env;
15+
use std::ffi::OsStr;
1416
use std::io::{self, BufRead};
1517
use std::process;
1618

@@ -90,7 +92,12 @@ fn run_rustfmt(files: &HashSet<String>, ranges: &[Range]) -> Result<(), FormatDi
9092
debug!("Files: {:?}", files);
9193
debug!("Ranges: {:?}", ranges);
9294

93-
let exit_status = process::Command::new("rustfmt")
95+
let rustfmt_var = env::var_os("RUSTFMT");
96+
let rustfmt = match &rustfmt_var {
97+
Some(rustfmt) => rustfmt,
98+
None => OsStr::new("rustfmt"),
99+
};
100+
let exit_status = process::Command::new(rustfmt)
94101
.args(files)
95102
.arg("--file-lines")
96103
.arg(ranges_as_json)

0 commit comments

Comments
 (0)