Skip to content

Commit 14d53f7

Browse files
dtolnaycalebcartwright
authored andcommitted
Use rustfmt given by RUSTFMT env var (#4419)
1 parent 2a8ff20 commit 14d53f7

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
@@ -25,7 +27,12 @@ pub fn debug_with_rustfmt(input: &TokenStream) {
2527
use std::io::Write;
2628
use std::process::{Command, Stdio};
2729

28-
let mut child = Command::new("rustfmt")
30+
let rustfmt_var = env::var_os("RUSTFMT");
31+
let rustfmt = match &rustfmt_var {
32+
Some(rustfmt) => rustfmt,
33+
None => OsStr::new("rustfmt"),
34+
};
35+
let mut child = Command::new(rustfmt)
2936
.stdin(Stdio::piped())
3037
.stdout(Stdio::piped())
3138
.spawn()

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cargo_metadata;
77
use std::cmp::Ordering;
88
use std::collections::{BTreeMap, BTreeSet};
99
use std::env;
10+
use std::ffi::OsStr;
1011
use std::fs;
1112
use std::hash::{Hash, Hasher};
1213
use std::io::{self, Write};
@@ -129,6 +130,15 @@ fn execute() -> i32 {
129130
}
130131
}
131132

133+
fn rustfmt_command() -> Command {
134+
let rustfmt_var = env::var_os("RUSTFMT");
135+
let rustfmt = match &rustfmt_var {
136+
Some(rustfmt) => rustfmt,
137+
None => OsStr::new("rustfmt"),
138+
};
139+
Command::new(rustfmt)
140+
}
141+
132142
fn convert_message_format_to_rustfmt_args(
133143
message_format: &str,
134144
rustfmt_args: &mut Vec<String>,
@@ -205,7 +215,7 @@ fn handle_command_status(status: Result<i32, io::Error>) -> i32 {
205215
}
206216

207217
fn get_rustfmt_info(args: &[String]) -> Result<i32, io::Error> {
208-
let mut command = Command::new("rustfmt")
218+
let mut command = rustfmt_command()
209219
.stdout(std::process::Stdio::inherit())
210220
.args(args)
211221
.spawn()
@@ -484,7 +494,7 @@ fn run_rustfmt(
484494
println!();
485495
}
486496

487-
let mut command = Command::new("rustfmt")
497+
let mut command = rustfmt_command()
488498
.stdout(stdout)
489499
.args(files)
490500
.args(&["--edition", edition])

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use serde_json as json;
1313
use thiserror::Error;
1414

1515
use std::collections::HashSet;
16+
use std::env;
17+
use std::ffi::OsStr;
1618
use std::io::{self, BufRead};
1719
use std::process;
1820

@@ -94,7 +96,12 @@ fn run_rustfmt(files: &HashSet<String>, ranges: &[Range]) -> Result<(), FormatDi
9496
debug!("Files: {:?}", files);
9597
debug!("Ranges: {:?}", ranges);
9698

97-
let exit_status = process::Command::new("rustfmt")
99+
let rustfmt_var = env::var_os("RUSTFMT");
100+
let rustfmt = match &rustfmt_var {
101+
Some(rustfmt) => rustfmt,
102+
None => OsStr::new("rustfmt"),
103+
};
104+
let exit_status = process::Command::new(rustfmt)
98105
.args(files)
99106
.arg("--file-lines")
100107
.arg(ranges_as_json)

0 commit comments

Comments
 (0)