4219: Avoid `rustup` invocation for non-rustup rust installation r=matklad a=oxalica

Fix #4218 and #3243.



Co-authored-by: oxalica <oxalicc@pm.me>
This commit is contained in:
bors[bot] 2020-04-30 11:59:29 +00:00 committed by GitHub
commit 36775ef0d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,7 +10,6 @@ pub mod pre_commit;
pub mod codegen; pub mod codegen;
mod ast_src; mod ast_src;
use anyhow::Context;
use std::{ use std::{
env, env,
io::Write, io::Write,
@ -24,9 +23,9 @@ use crate::{
not_bash::{date_iso, fs2, pushd, rm_rf, run}, not_bash::{date_iso, fs2, pushd, rm_rf, run},
}; };
pub use anyhow::Result; pub use anyhow::{bail, Context as _, Result};
const TOOLCHAIN: &str = "stable"; const RUSTFMT_TOOLCHAIN: &str = "stable";
pub fn project_root() -> PathBuf { pub fn project_root() -> PathBuf {
Path::new( Path::new(
@ -57,15 +56,25 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
let _dir = pushd(project_root()); let _dir = pushd(project_root());
ensure_rustfmt()?; ensure_rustfmt()?;
let check = if mode == Mode::Verify { "--check" } else { "" }; if Command::new("cargo")
run!("rustup run {} -- cargo fmt -- {}", TOOLCHAIN, check)?; .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
Ok(()) .args(&["fmt", "--"])
.args(if mode == Mode::Verify { &["--check"][..] } else { &[] })
.stderr(Stdio::inherit())
.status()?
.success()
{
Ok(())
} else {
bail!("Rustfmt failed");
}
} }
fn reformat(text: impl std::fmt::Display) -> Result<String> { fn reformat(text: impl std::fmt::Display) -> Result<String> {
ensure_rustfmt()?; ensure_rustfmt()?;
let mut rustfmt = Command::new("rustup") let mut rustfmt = Command::new("rustfmt")
.args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"]) .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
.args(&["--config-path"])
.arg(project_root().join("rustfmt.toml")) .arg(project_root().join("rustfmt.toml"))
.args(&["--config", "fn_single_line=true"]) .args(&["--config", "fn_single_line=true"])
.stdin(Stdio::piped()) .stdin(Stdio::piped())
@ -79,29 +88,42 @@ fn reformat(text: impl std::fmt::Display) -> Result<String> {
} }
fn ensure_rustfmt() -> Result<()> { fn ensure_rustfmt() -> Result<()> {
match Command::new("rustup") match Command::new("rustfmt")
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) .args(&["--version"])
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
.stdout(Stdio::piped())
.stderr(Stdio::null()) .stderr(Stdio::null())
.stdout(Stdio::null()) .spawn()
.status() .and_then(|child| child.wait_with_output())
{ {
Ok(status) if status.success() => return Ok(()), Ok(output)
_ => (), if output.status.success()
}; && std::str::from_utf8(&output.stdout)?.contains(RUSTFMT_TOOLCHAIN) =>
run!("rustup toolchain install {}", TOOLCHAIN)?; {
run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?; Ok(())
Ok(()) }
_ => {
bail!(
"Failed to run rustfmt from toolchain '{0}'. \
Please run `rustup component add rustfmt --toolchain {0}` to install it.",
RUSTFMT_TOOLCHAIN,
);
}
}
} }
pub fn run_clippy() -> Result<()> { pub fn run_clippy() -> Result<()> {
match Command::new("rustup") match Command::new("cargo")
.args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) .args(&["clippy", "--version"])
.stderr(Stdio::null()) .stderr(Stdio::null())
.stdout(Stdio::null()) .stdout(Stdio::null())
.status() .status()
{ {
Ok(status) if status.success() => (), Ok(status) if status.success() => (),
_ => install_clippy().context("install clippy")?, _ => bail!(
"Failed run cargo clippy. \
Please run `rustup component add clippy` to install it.",
),
}; };
let allowed_lints = [ let allowed_lints = [
@ -110,17 +132,7 @@ pub fn run_clippy() -> Result<()> {
"clippy::nonminimal_bool", "clippy::nonminimal_bool",
"clippy::redundant_pattern_matching", "clippy::redundant_pattern_matching",
]; ];
run!( run!("cargo clippy --all-features --all-targets -- -A {}", allowed_lints.join(" -A "))?;
"rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
TOOLCHAIN,
allowed_lints.join(" -A ")
)?;
Ok(())
}
fn install_clippy() -> Result<()> {
run!("rustup toolchain install {}", TOOLCHAIN)?;
run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?;
Ok(()) Ok(())
} }
@ -130,7 +142,29 @@ pub fn run_fuzzer() -> Result<()> {
run!("cargo install cargo-fuzz")?; run!("cargo install cargo-fuzz")?;
}; };
run!("rustup run nightly -- cargo fuzz run parser")?; // Expecting nightly rustc
match Command::new("rustc")
.args(&["--version"])
.env("RUSTUP_TOOLCHAIN", "nightly")
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.and_then(|child| child.wait_with_output())
{
Ok(output)
if output.status.success()
&& std::str::from_utf8(&output.stdout)?.contains("nightly") => {}
_ => bail!("fuzz tests require nightly rustc"),
}
let status = Command::new("cargo")
.env("RUSTUP_TOOLCHAIN", "nightly")
.args(&["fuzz", "run", "parser"])
.stderr(Stdio::inherit())
.status()?;
if !status.success() {
bail!("{}", status);
}
Ok(()) Ok(())
} }