Skip to content

Commit 36775ef

Browse files
bors[bot]oxalica
andauthored
Merge #4219
4219: Avoid `rustup` invocation for non-rustup rust installation r=matklad a=oxalica Fix #4218 and #3243. Co-authored-by: oxalica <oxalicc@pm.me>
2 parents fdaddb9 + a1e8451 commit 36775ef

File tree

1 file changed

+67
-33
lines changed

1 file changed

+67
-33
lines changed

xtask/src/lib.rs

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub mod pre_commit;
1010
pub mod codegen;
1111
mod ast_src;
1212

13-
use anyhow::Context;
1413
use std::{
1514
env,
1615
io::Write,
@@ -24,9 +23,9 @@ use crate::{
2423
not_bash::{date_iso, fs2, pushd, rm_rf, run},
2524
};
2625

27-
pub use anyhow::Result;
26+
pub use anyhow::{bail, Context as _, Result};
2827

29-
const TOOLCHAIN: &str = "stable";
28+
const RUSTFMT_TOOLCHAIN: &str = "stable";
3029

3130
pub fn project_root() -> PathBuf {
3231
Path::new(
@@ -57,15 +56,25 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
5756
let _dir = pushd(project_root());
5857
ensure_rustfmt()?;
5958

60-
let check = if mode == Mode::Verify { "--check" } else { "" };
61-
run!("rustup run {} -- cargo fmt -- {}", TOOLCHAIN, check)?;
62-
Ok(())
59+
if Command::new("cargo")
60+
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
61+
.args(&["fmt", "--"])
62+
.args(if mode == Mode::Verify { &["--check"][..] } else { &[] })
63+
.stderr(Stdio::inherit())
64+
.status()?
65+
.success()
66+
{
67+
Ok(())
68+
} else {
69+
bail!("Rustfmt failed");
70+
}
6371
}
6472

6573
fn reformat(text: impl std::fmt::Display) -> Result<String> {
6674
ensure_rustfmt()?;
67-
let mut rustfmt = Command::new("rustup")
68-
.args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"])
75+
let mut rustfmt = Command::new("rustfmt")
76+
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
77+
.args(&["--config-path"])
6978
.arg(project_root().join("rustfmt.toml"))
7079
.args(&["--config", "fn_single_line=true"])
7180
.stdin(Stdio::piped())
@@ -79,29 +88,42 @@ fn reformat(text: impl std::fmt::Display) -> Result<String> {
7988
}
8089

8190
fn ensure_rustfmt() -> Result<()> {
82-
match Command::new("rustup")
83-
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
91+
match Command::new("rustfmt")
92+
.args(&["--version"])
93+
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
94+
.stdout(Stdio::piped())
8495
.stderr(Stdio::null())
85-
.stdout(Stdio::null())
86-
.status()
96+
.spawn()
97+
.and_then(|child| child.wait_with_output())
8798
{
88-
Ok(status) if status.success() => return Ok(()),
89-
_ => (),
90-
};
91-
run!("rustup toolchain install {}", TOOLCHAIN)?;
92-
run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?;
93-
Ok(())
99+
Ok(output)
100+
if output.status.success()
101+
&& std::str::from_utf8(&output.stdout)?.contains(RUSTFMT_TOOLCHAIN) =>
102+
{
103+
Ok(())
104+
}
105+
_ => {
106+
bail!(
107+
"Failed to run rustfmt from toolchain '{0}'. \
108+
Please run `rustup component add rustfmt --toolchain {0}` to install it.",
109+
RUSTFMT_TOOLCHAIN,
110+
);
111+
}
112+
}
94113
}
95114

96115
pub fn run_clippy() -> Result<()> {
97-
match Command::new("rustup")
98-
.args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"])
116+
match Command::new("cargo")
117+
.args(&["clippy", "--version"])
99118
.stderr(Stdio::null())
100119
.stdout(Stdio::null())
101120
.status()
102121
{
103122
Ok(status) if status.success() => (),
104-
_ => install_clippy().context("install clippy")?,
123+
_ => bail!(
124+
"Failed run cargo clippy. \
125+
Please run `rustup component add clippy` to install it.",
126+
),
105127
};
106128

107129
let allowed_lints = [
@@ -110,17 +132,7 @@ pub fn run_clippy() -> Result<()> {
110132
"clippy::nonminimal_bool",
111133
"clippy::redundant_pattern_matching",
112134
];
113-
run!(
114-
"rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
115-
TOOLCHAIN,
116-
allowed_lints.join(" -A ")
117-
)?;
118-
Ok(())
119-
}
120-
121-
fn install_clippy() -> Result<()> {
122-
run!("rustup toolchain install {}", TOOLCHAIN)?;
123-
run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?;
135+
run!("cargo clippy --all-features --all-targets -- -A {}", allowed_lints.join(" -A "))?;
124136
Ok(())
125137
}
126138

@@ -130,7 +142,29 @@ pub fn run_fuzzer() -> Result<()> {
130142
run!("cargo install cargo-fuzz")?;
131143
};
132144

133-
run!("rustup run nightly -- cargo fuzz run parser")?;
145+
// Expecting nightly rustc
146+
match Command::new("rustc")
147+
.args(&["--version"])
148+
.env("RUSTUP_TOOLCHAIN", "nightly")
149+
.stdout(Stdio::piped())
150+
.stderr(Stdio::null())
151+
.spawn()
152+
.and_then(|child| child.wait_with_output())
153+
{
154+
Ok(output)
155+
if output.status.success()
156+
&& std::str::from_utf8(&output.stdout)?.contains("nightly") => {}
157+
_ => bail!("fuzz tests require nightly rustc"),
158+
}
159+
160+
let status = Command::new("cargo")
161+
.env("RUSTUP_TOOLCHAIN", "nightly")
162+
.args(&["fuzz", "run", "parser"])
163+
.stderr(Stdio::inherit())
164+
.status()?;
165+
if !status.success() {
166+
bail!("{}", status);
167+
}
134168
Ok(())
135169
}
136170

0 commit comments

Comments
 (0)