Skip to content

Commit 32891e1

Browse files
authored
Rollup merge of #108021 - zephaniahong:oldx, r=albertlarsan68
make x look for x.py if shell script does not exist Fixes #107907 Manually tested by doing the following after changes were made: 1. `cargo install --path src/tools/x` 2. checked out old version: commit hash `775c3c0` from #99992 3. Ran `x --help` and it works. Previously, it was giving the error `x.py not found`
2 parents 8f65e25 + 54cfc10 commit 32891e1

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

src/tools/x/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "x"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "Run x.py slightly more conveniently"
55
edition = "2021"
66
publish = false

src/tools/x/src/main.rs

+59-15
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,47 @@
99
//! We also don't use `pwsh` on Windows, because it is not installed by default;
1010
1111
use std::{
12-
env, io,
12+
env::{self, consts::EXE_EXTENSION},
13+
io,
1314
path::Path,
1415
process::{self, Command, ExitStatus},
1516
};
1617

18+
const PYTHON: &str = "python";
19+
const PYTHON2: &str = "python2";
20+
const PYTHON3: &str = "python3";
21+
22+
fn python() -> &'static str {
23+
let val = match env::var_os("PATH") {
24+
Some(val) => val,
25+
None => return PYTHON,
26+
};
27+
28+
let mut python2 = false;
29+
let mut python3 = false;
30+
31+
for dir in env::split_paths(&val) {
32+
// `python` should always take precedence over python2 / python3 if it exists
33+
if dir.join(PYTHON).with_extension(EXE_EXTENSION).exists() {
34+
return PYTHON;
35+
}
36+
37+
python2 |= dir.join(PYTHON2).with_extension(EXE_EXTENSION).exists();
38+
python3 |= dir.join(PYTHON3).with_extension(EXE_EXTENSION).exists();
39+
}
40+
41+
// try 3 before 2
42+
if python3 {
43+
PYTHON3
44+
} else if python2 {
45+
PYTHON2
46+
} else {
47+
// Python was not found on path, so exit
48+
eprintln!("Unable to find python in your PATH. Please check it is installed.");
49+
process::exit(1);
50+
}
51+
}
52+
1753
#[cfg(windows)]
1854
fn x_command(dir: &Path) -> Command {
1955
let mut cmd = Command::new("powershell.exe");
@@ -51,6 +87,17 @@ fn exec_or_status(command: &mut Command) -> io::Result<ExitStatus> {
5187
command.status()
5288
}
5389

90+
fn handle_result(result: io::Result<ExitStatus>, cmd: Command) {
91+
match result {
92+
Err(error) => {
93+
eprintln!("Failed to invoke `{:?}`: {}", cmd, error);
94+
}
95+
Ok(status) => {
96+
process::exit(status.code().unwrap_or(1));
97+
}
98+
}
99+
}
100+
54101
fn main() {
55102
match env::args().skip(1).next().as_deref() {
56103
Some("--wrapper-version") => {
@@ -70,22 +117,19 @@ fn main() {
70117

71118
for dir in current.ancestors() {
72119
let candidate = dir.join("x.py");
73-
74120
if candidate.exists() {
75-
let mut cmd = x_command(dir);
76-
77-
cmd.args(env::args().skip(1)).current_dir(dir);
78-
79-
let result = exec_or_status(&mut cmd);
80-
81-
match result {
82-
Err(error) => {
83-
eprintln!("Failed to invoke `{:?}`: {}", cmd, error);
84-
}
85-
Ok(status) => {
86-
process::exit(status.code().unwrap_or(1));
87-
}
121+
let shell_script_candidate = dir.join("x");
122+
let mut cmd: Command;
123+
if shell_script_candidate.exists() {
124+
cmd = x_command(dir);
125+
cmd.args(env::args().skip(1)).current_dir(dir);
126+
} else {
127+
// For older checkouts that do not have the x shell script, default to python
128+
cmd = Command::new(python());
129+
cmd.arg(&candidate).args(env::args().skip(1)).current_dir(dir);
88130
}
131+
let result = exec_or_status(&mut cmd);
132+
handle_result(result, cmd);
89133
}
90134
}
91135

0 commit comments

Comments
 (0)