Skip to content

Commit

Permalink
Use rustfmt given by RUSTFMT env var (#4419)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay authored Sep 16, 2020
1 parent 3c19c4d commit 39fe0e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
9 changes: 8 additions & 1 deletion config_proc_macro/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use std::env;
use std::ffi::OsStr;

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

let mut child = Command::new("rustfmt")
let rustfmt_var = env::var_os("RUSTFMT");
let rustfmt = match &rustfmt_var {
Some(rustfmt) => rustfmt,
None => OsStr::new("rustfmt"),
};
let mut child = Command::new(rustfmt)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
Expand Down
14 changes: 12 additions & 2 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,7 @@
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::hash::{Hash, Hasher};
use std::io::{self, Write};
Expand Down Expand Up @@ -149,6 150,15 @@ fn is_status_options(s: &str) -> bool {
|| s.starts_with("--print-config=")
}

fn rustfmt_command() -> Command {
let rustfmt_var = env::var_os("RUSTFMT");
let rustfmt = match &rustfmt_var {
Some(rustfmt) => rustfmt,
None => OsStr::new("rustfmt"),
};
Command::new(rustfmt)
}

fn build_rustfmt_args(opts: &Opts, rustfmt_args: &mut Vec<String>) -> Result<(), String> {
let mut contains_check = false;
let mut contains_emit_mode = false;
Expand Down Expand Up @@ -240,7 250,7 @@ fn handle_command_status(status: Result<i32, io::Error>) -> i32 {
}

fn get_rustfmt_info(args: &[String]) -> Result<i32, io::Error> {
let mut command = Command::new("rustfmt")
let mut command = rustfmt_command()
.stdout(std::process::Stdio::inherit())
.args(args)
.spawn()
Expand Down Expand Up @@ -625,7 635,7 @@ fn run_rustfmt(
println!();
}

let mut command = Command::new("rustfmt")
let mut command = rustfmt_command()
.stdout(stdout)
.args(files)
.args(&["--edition", edition])
Expand Down
9 changes: 8 additions & 1 deletion src/format-diff/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,8 @@ use serde_json as json;
use thiserror::Error;

use std::collections::HashSet;
use std::env;
use std::ffi::OsStr;
use std::io::{self, BufRead};
use std::process;

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

let exit_status = process::Command::new("rustfmt")
let rustfmt_var = env::var_os("RUSTFMT");
let rustfmt = match &rustfmt_var {
Some(rustfmt) => rustfmt,
None => OsStr::new("rustfmt"),
};
let exit_status = process::Command::new(rustfmt)
.args(files)
.arg("--file-lines")
.arg(ranges_as_json)
Expand Down

0 comments on commit 39fe0e1

Please sign in to comment.