Skip to content

Commit 0935bc8

Browse files
committed
tidy now installs typos-cli as-needed via cargo
1 parent c4b3769 commit 0935bc8

File tree

6 files changed

+81
-33
lines changed

6 files changed

+81
-33
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
870870
}
871871
ImportKind::Glob { .. } => {
872872
// FIXME: Use mutable resolver directly as a hack, this should be an output of
873-
// specualtive resolution.
873+
// speculative resolution.
874874
self.get_mut_unchecked().resolve_glob_import(import);
875875
return 0;
876876
}
@@ -907,7 +907,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
907907
// We need the `target`, `source` can be extracted.
908908
let imported_binding = this.import(binding, import);
909909
// FIXME: Use mutable resolver directly as a hack, this should be an output of
910-
// specualtive resolution.
910+
// speculative resolution.
911911
this.get_mut_unchecked().define_binding_local(
912912
parent,
913913
target,
@@ -921,7 +921,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
921921
if target.name != kw::Underscore {
922922
let key = BindingKey::new(target, ns);
923923
// FIXME: Use mutable resolver directly as a hack, this should be an output of
924-
// specualtive resolution.
924+
// speculative resolution.
925925
this.get_mut_unchecked().update_local_resolution(
926926
parent,
927927
key,

library/std/src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unsafe_op_in_unsafe_fn)]
22

33
/// The configure builtins provides runtime support compiler-builtin features
4-
/// which require dynamic intialization to work as expected, e.g. aarch64
4+
/// which require dynamic initialization to work as expected, e.g. aarch64
55
/// outline-atomics.
66
mod configure_builtins;
77

src/librustdoc/html/static/js/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3333,7 +3333,7 @@ class DocSearch {
33333333
}
33343334

33353335
// sort unstable items later
3336-
// FIXME: there is some doubt if this is the most effecient way to implement this.
3336+
// FIXME: there is some doubt if this is the most efficient way to implement this.
33373337
// alternative options include:
33383338
// * put is_unstable on each item when the index is built.
33393339
// increases memory usage but avoids a hashmap lookup.

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const RUFF_CONFIG_PATH: &[&str] = &["src", "tools", "tidy", "config", "ruff.toml
4141
const RUFF_CACHE_PATH: &[&str] = &["cache", "ruff_cache"];
4242
const PIP_REQ_PATH: &[&str] = &["src", "tools", "tidy", "config", "requirements.txt"];
4343

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

4746
pub fn check(
@@ -51,6 +50,7 @@ pub fn check(
5150
librustdoc_path: &Path,
5251
tools_path: &Path,
5352
npm: &Path,
53+
cargo: &Path,
5454
bless: bool,
5555
extra_checks: Option<&str>,
5656
pos_args: &[String],
@@ -63,6 +63,7 @@ pub fn check(
6363
librustdoc_path,
6464
tools_path,
6565
npm,
66+
cargo,
6667
bless,
6768
extra_checks,
6869
pos_args,
@@ -78,6 +79,7 @@ fn check_impl(
7879
librustdoc_path: &Path,
7980
tools_path: &Path,
8081
npm: &Path,
82+
cargo: &Path,
8183
bless: bool,
8284
extra_checks: Option<&str>,
8385
pos_args: &[String],
@@ -293,7 +295,7 @@ fn check_impl(
293295
} else {
294296
eprintln!("spellcheck files");
295297
}
296-
spellcheck_runner(&args)?;
298+
spellcheck_runner(&outdir, &cargo, &args)?;
297299
}
298300

299301
if js_lint || js_typecheck {
@@ -576,33 +578,12 @@ fn shellcheck_runner(args: &[&OsStr]) -> Result<(), Error> {
576578
if status.success() { Ok(()) } else { Err(Error::FailedCheck("shellcheck")) }
577579
}
578580

579-
/// Check that spellchecker is installed then run it at the given path
580-
fn spellcheck_runner(args: &[&str]) -> Result<(), Error> {
581-
// sync version with .github/workflows/spellcheck.yml
582-
let expected_version = "typos-cli 1.34.0";
583-
match Command::new("typos").arg("--version").output() {
584-
Ok(o) => {
585-
let stdout = String::from_utf8_lossy(&o.stdout);
586-
if stdout.trim() != expected_version {
587-
return Err(Error::Version {
588-
program: "typos",
589-
required: expected_version,
590-
installed: stdout.trim().to_string(),
591-
});
592-
}
593-
}
594-
Err(e) if e.kind() == io::ErrorKind::NotFound => {
595-
return Err(Error::MissingReq(
596-
"typos",
597-
"spellcheck file checks",
598-
// sync version with .github/workflows/spellcheck.yml
599-
Some("install tool via `cargo install [email protected]`".to_owned()),
600-
));
601-
}
602-
Err(e) => return Err(e.into()),
603-
}
581+
/// Ensure that spellchecker is installed then run it at the given path
582+
fn spellcheck_runner(outdir: &Path, cargo: &Path, args: &[&str]) -> Result<(), Error> {
583+
let bin_path =
584+
crate::ensure_version_or_cargo_install(outdir, cargo, "typos-cli", "typos", "1.34.0")?;
604585

605-
let status = Command::new("typos").args(args).status()?;
586+
let status = Command::new(bin_path).args(args).status()?;
606587
if status.success() { Ok(()) } else { Err(Error::FailedCheck("typos")) }
607588
}
608589

src/tools/tidy/src/lib.rs

Lines changed: 66 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,70 @@ 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+
eprintln!("finished building tool {bin_name}");
246+
Ok(bin_path)
247+
}
248+
183249
pub mod alphabetical;
184250
pub mod bins;
185251
pub mod debug_artifacts;

src/tools/tidy/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ fn main() {
184184
&librustdoc_path,
185185
&tools_path,
186186
&npm,
187+
&cargo,
187188
bless,
188189
extra_checks,
189190
pos_args

0 commit comments

Comments
 (0)