Skip to content

Commit d26c88c

Browse files
Rollup merge of rust-lang#107048 - DebugSteven:newer-x-check-cargo, r=albertlarsan68
check for x version updates This PR adds a check to tidy to assert that the installed version of `x` is equal to the version in `src/tools/x/Cargo.toml`. It checks the installed version of `x` by parsing the output of `cargo install --list` (as an option proposed in this [issue](rust-lang#106469)). It does not warn if `x` has not yet been installed, on the assumption that the user isn't interested in using it.
2 parents 68b390a + 540ca2f commit d26c88c

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -5608,6 +5608,7 @@ dependencies = [
56085608
"lazy_static",
56095609
"miropt-test-tools",
56105610
"regex",
5611+
"semver",
56115612
"termcolor",
56125613
"walkdir",
56135614
]

src/tools/tidy/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ miropt-test-tools = { path = "../miropt-test-tools" }
1212
lazy_static = "1"
1313
walkdir = "2"
1414
ignore = "0.4.18"
15+
semver = "1.0"
1516
termcolor = "1.1.3"
1617

1718
[[bin]]

src/tools/tidy/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ pub mod ui_tests;
7070
pub mod unit_tests;
7171
pub mod unstable_book;
7272
pub mod walk;
73+
pub mod x_version;

src/tools/tidy/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ fn main() {
114114
check!(alphabetical, &compiler_path);
115115
check!(alphabetical, &library_path);
116116

117+
check!(x_version, &root_path, &cargo);
118+
117119
let collected = {
118120
drain_handles(&mut handles);
119121

src/tools/tidy/src/x_version.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use semver::Version;
2+
use std::path::Path;
3+
use std::process::{Command, Stdio};
4+
5+
pub fn check(root: &Path, cargo: &Path, bad: &mut bool) {
6+
let cargo_list = Command::new(cargo).args(["install", "--list"]).stdout(Stdio::piped()).spawn();
7+
8+
let child = match cargo_list {
9+
Ok(child) => child,
10+
Err(e) => return tidy_error!(bad, "failed to run `cargo`: {}", e),
11+
};
12+
13+
let cargo_list = child.wait_with_output().unwrap();
14+
15+
if cargo_list.status.success() {
16+
let exe_list = String::from_utf8_lossy(&cargo_list.stdout);
17+
let exe_list = exe_list.lines();
18+
19+
let mut installed: Option<Version> = None;
20+
21+
for line in exe_list {
22+
let mut iter = line.split_whitespace();
23+
if iter.next() == Some("x") {
24+
if let Some(version) = iter.next() {
25+
// Check this is the rust-lang/rust x tool installation since it should be
26+
// installed at a path containing `src/tools/x`.
27+
if let Some(path) = iter.next() {
28+
if path.contains(&"src/tools/x") {
29+
let version = version.strip_prefix("v").unwrap();
30+
installed = Some(Version::parse(version).unwrap());
31+
break;
32+
}
33+
};
34+
}
35+
} else {
36+
continue;
37+
}
38+
}
39+
// Unwrap the some if x is installed, otherwise return because it's fine if x isn't installed.
40+
let installed = if let Some(i) = installed { i } else { return };
41+
42+
if let Some(expected) = get_x_wrapper_version(root, cargo) {
43+
if installed < expected {
44+
return println!(
45+
"Current version of x is {installed}, but the latest version is {expected}\nConsider updating to the newer version of x by running `cargo install --path src/tools/x`"
46+
);
47+
}
48+
} else {
49+
return tidy_error!(
50+
bad,
51+
"Unable to parse the latest version of `x` at `src/tools/x/Cargo.toml`"
52+
);
53+
}
54+
} else {
55+
return tidy_error!(bad, "failed to check version of `x`: {}", cargo_list.status);
56+
}
57+
}
58+
59+
// Parse latest version out of `x` Cargo.toml
60+
fn get_x_wrapper_version(root: &Path, cargo: &Path) -> Option<Version> {
61+
let mut cmd = cargo_metadata::MetadataCommand::new();
62+
cmd.cargo_path(cargo)
63+
.manifest_path(root.join("src/tools/x/Cargo.toml"))
64+
.no_deps()
65+
.features(cargo_metadata::CargoOpt::AllFeatures);
66+
let mut metadata = t!(cmd.exec());
67+
metadata.packages.pop().map(|x| x.version)
68+
}

0 commit comments

Comments
 (0)