Skip to content

Commit 3252f85

Browse files
committed
tidy spellcheck improvments based on code review
1 parent 715bbe0 commit 3252f85

File tree

3 files changed

+69
-69
lines changed

3 files changed

+69
-69
lines changed

src/build_helper/src/util.rs

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::env;
21
use std::fs::File;
3-
use std::io::{self, BufRead, BufReader};
4-
use std::path::{Path, PathBuf};
2+
use std::io::{BufRead, BufReader};
3+
use std::path::Path;
54
use std::process::Command;
65

76
/// Invokes `build_helper::util::detail_exit` with `cfg!(test)`
@@ -67,65 +66,3 @@ pub fn parse_gitmodules(target_dir: &Path) -> Vec<String> {
6766

6867
submodules_paths
6968
}
70-
71-
/// If the given executable is installed with the given version, use that,
72-
/// otherwise install via cargo.
73-
pub fn ensure_version_or_cargo_install(
74-
build_dir: &Path,
75-
cargo: &Path,
76-
pkg_name: &str,
77-
bin_name: &str,
78-
version: &str,
79-
) -> io::Result<PathBuf> {
80-
// ignore the process exit code here and instead just let the version number check fail.
81-
// we also importantly don't return if the program wasn't installed,
82-
// instead we want to continue to the fallback.
83-
'ck: {
84-
// FIXME: rewrite as if-let chain once this crate is 2024 edition.
85-
let Ok(output) = Command::new(bin_name).arg("--version").output() else {
86-
break 'ck;
87-
};
88-
let Ok(s) = str::from_utf8(&output.stdout) else {
89-
break 'ck;
90-
};
91-
let Some(v) = s.trim().split_whitespace().last() else {
92-
break 'ck;
93-
};
94-
if v == version {
95-
return Ok(PathBuf::from(bin_name));
96-
}
97-
}
98-
99-
let tool_root_dir = build_dir.join("misc-tools");
100-
let tool_bin_dir = tool_root_dir.join("bin");
101-
eprintln!("building external tool {bin_name} from package {pkg_name}@{version}");
102-
// use --force to ensure that if the required version is bumped, we update it.
103-
// use --target-dir to ensure we have a build cache so repeated invocations aren't slow.
104-
// modify PATH so that cargo doesn't print a warning telling the user to modify the path.
105-
let cargo_exit_code = Command::new(cargo)
106-
.args(["install", "--locked", "--force", "--quiet"])
107-
.arg("--root")
108-
.arg(&tool_root_dir)
109-
.arg("--target-dir")
110-
.arg(tool_root_dir.join("target"))
111-
.arg(format!("{pkg_name}@{version}"))
112-
.env(
113-
"PATH",
114-
env::join_paths(
115-
env::split_paths(&env::var("PATH").unwrap())
116-
.chain(std::iter::once(tool_bin_dir.clone())),
117-
)
118-
.expect("build dir contains invalid char"),
119-
)
120-
.spawn()?
121-
.wait()?;
122-
if !cargo_exit_code.success() {
123-
return Err(io::Error::other("cargo install failed"));
124-
}
125-
let bin_path = tool_bin_dir.join(bin_name);
126-
assert!(
127-
matches!(bin_path.try_exists(), Ok(true)),
128-
"cargo install did not produce the expected binary"
129-
);
130-
Ok(bin_path)
131-
}

src/tools/tidy/src/extra_checks/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ use std::process::Command;
2323
use std::str::FromStr;
2424
use std::{fmt, fs, io};
2525

26-
use build_helper::util::ensure_version_or_cargo_install;
27-
2826
use crate::CiInfo;
2927

3028
mod rustdoc_js;
@@ -43,7 +41,6 @@ const RUFF_CONFIG_PATH: &[&str] = &["src", "tools", "tidy", "config", "ruff.toml
4341
const RUFF_CACHE_PATH: &[&str] = &["cache", "ruff_cache"];
4442
const PIP_REQ_PATH: &[&str] = &["src", "tools", "tidy", "config", "requirements.txt"];
4543

46-
// this must be kept in sync with with .github/workflows/spellcheck.yml
4744
const SPELLCHECK_DIRS: &[&str] = &["compiler", "library", "src/bootstrap", "src/librustdoc"];
4845

4946
pub fn check(
@@ -588,7 +585,8 @@ fn spellcheck_runner(
588585
cargo: &Path,
589586
args: &[&str],
590587
) -> Result<(), Error> {
591-
let bin_path = ensure_version_or_cargo_install(outdir, cargo, "typos-cli", "typos", "1.34.0")?;
588+
let bin_path =
589+
crate::ensure_version_or_cargo_install(outdir, cargo, "typos-cli", "typos", "1.34.0")?;
592590

593591
match Command::new(bin_path).current_dir(src_root).args(args).status() {
594592
Ok(status) => {

src/tools/tidy/src/lib.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
//! to be used by tools.
55
66
use std::ffi::OsStr;
7+
use std::path::{Path, PathBuf};
78
use std::process::Command;
9+
use std::{env, io};
810

911
use build_helper::ci::CiEnv;
1012
use build_helper::git::{GitConfig, get_closest_upstream_commit};
@@ -180,6 +182,69 @@ pub fn files_modified(ci_info: &CiInfo, pred: impl Fn(&str) -> bool) -> bool {
180182
!v.is_empty()
181183
}
182184

185+
/// If the given executable is installed with the given version, use that,
186+
/// otherwise install via cargo.
187+
pub fn ensure_version_or_cargo_install(
188+
build_dir: &Path,
189+
cargo: &Path,
190+
pkg_name: &str,
191+
bin_name: &str,
192+
version: &str,
193+
) -> io::Result<PathBuf> {
194+
// ignore the process exit code here and instead just let the version number check fail.
195+
// we also importantly don't return if the program wasn't installed,
196+
// instead we want to continue to the fallback.
197+
'ck: {
198+
// FIXME: rewrite as if-let chain once this crate is 2024 edition.
199+
let Ok(output) = Command::new(bin_name).arg("--version").output() else {
200+
break 'ck;
201+
};
202+
let Ok(s) = str::from_utf8(&output.stdout) else {
203+
break 'ck;
204+
};
205+
let Some(v) = s.trim().split_whitespace().last() else {
206+
break 'ck;
207+
};
208+
if v == version {
209+
return Ok(PathBuf::from(bin_name));
210+
}
211+
}
212+
213+
let tool_root_dir = build_dir.join("misc-tools");
214+
let tool_bin_dir = tool_root_dir.join("bin");
215+
eprintln!("building external tool {bin_name} from package {pkg_name}@{version}");
216+
// use --force to ensure that if the required version is bumped, we update it.
217+
// use --target-dir to ensure we have a build cache so repeated invocations aren't slow.
218+
// modify PATH so that cargo doesn't print a warning telling the user to modify the path.
219+
let cargo_exit_code = Command::new(cargo)
220+
.args(["install", "--locked", "--force", "--quiet"])
221+
.arg("--root")
222+
.arg(&tool_root_dir)
223+
.arg("--target-dir")
224+
.arg(tool_root_dir.join("target"))
225+
.arg(format!("{pkg_name}@{version}"))
226+
.env(
227+
"PATH",
228+
env::join_paths(
229+
env::split_paths(&env::var("PATH").unwrap())
230+
.chain(std::iter::once(tool_bin_dir.clone())),
231+
)
232+
.expect("build dir contains invalid char"),
233+
)
234+
.env("RUSTFLAGS", "-Copt-level=0")
235+
.spawn()?
236+
.wait()?;
237+
if !cargo_exit_code.success() {
238+
return Err(io::Error::other("cargo install failed"));
239+
}
240+
let bin_path = tool_bin_dir.join(bin_name);
241+
assert!(
242+
matches!(bin_path.try_exists(), Ok(true)),
243+
"cargo install did not produce the expected binary"
244+
);
245+
Ok(bin_path)
246+
}
247+
183248
pub mod alphabetical;
184249
pub mod bins;
185250
pub mod debug_artifacts;

0 commit comments

Comments
 (0)