Skip to content

Commit 0b97080

Browse files
committed
remove try_run and change vanilla command execution to bootstrapCommand monitored lifecycle run methods in download.rs
1 parent dc4d32d commit 0b97080

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

src/bootstrap/src/core/download.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::ffi::OsString;
33
use std::fs::{self, File};
44
use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Write};
55
use std::path::{Path, PathBuf};
6-
use std::process::{Command, Stdio};
76
use std::sync::OnceLock;
87

98
use xz2::bufread::XzDecoder;
@@ -16,14 +15,7 @@ use crate::{Config, t};
1615

1716
static SHOULD_FIX_BINS_AND_DYLIBS: OnceLock<bool> = OnceLock::new();
1817

19-
/// `Config::try_run` wrapper for this module to avoid warnings on `try_run`, since we don't have access to a `builder` yet.
20-
fn try_run(config: &Config, cmd: &mut Command) -> Result<(), ()> {
21-
#[expect(deprecated)]
22-
config.try_run(cmd)
23-
}
24-
25-
fn extract_curl_version(out: &[u8]) -> semver::Version {
26-
let out = String::from_utf8_lossy(out);
18+
fn extract_curl_version(out: String) -> semver::Version {
2719
// The output should look like this: "curl <major>.<minor>.<patch> ..."
2820
out.lines()
2921
.next()
@@ -32,16 +24,21 @@ fn extract_curl_version(out: &[u8]) -> semver::Version {
3224
.unwrap_or(semver::Version::new(1, 0, 0))
3325
}
3426

35-
fn curl_version() -> semver::Version {
36-
let mut curl = Command::new("curl");
37-
curl.arg("-V");
38-
let Ok(out) = curl.output() else { return semver::Version::new(1, 0, 0) };
39-
let out = out.stdout;
40-
extract_curl_version(&out)
41-
}
42-
27+
#[allow(warnings)]
4328
/// Generic helpers that are useful anywhere in bootstrap.
4429
impl Config {
30+
fn curl_version(&self) -> semver::Version {
31+
let mut curl = command("curl");
32+
curl.arg("-V");
33+
let curl = curl.run_capture_stdout(self.context());
34+
if curl.is_failure() {
35+
return semver::Version::new(1, 0, 0);
36+
}
37+
let output = curl.stdout();
38+
let out = output;
39+
extract_curl_version(out)
40+
}
41+
4542
pub fn is_verbose(&self) -> bool {
4643
self.verbose > 0
4744
}
@@ -85,18 +82,13 @@ impl Config {
8582
/// on NixOS
8683
fn should_fix_bins_and_dylibs(&self) -> bool {
8784
let val = *SHOULD_FIX_BINS_AND_DYLIBS.get_or_init(|| {
88-
match Command::new("uname").arg("-s").stderr(Stdio::inherit()).output() {
89-
Err(_) => return false,
90-
Ok(output) if !output.status.success() => return false,
91-
Ok(output) => {
92-
let mut os_name = output.stdout;
93-
if os_name.last() == Some(&b'\n') {
94-
os_name.pop();
95-
}
96-
if os_name != b"Linux" {
97-
return false;
98-
}
99-
}
85+
let uname = command("uname").arg("-s").run_capture_stdout(self.context());
86+
if uname.is_failure() {
87+
return false;
88+
}
89+
let output = uname.stdout();
90+
if output.starts_with("Linux") {
91+
return false;
10092
}
10193

10294
// If the user has asked binaries to be patched for Nix, then
@@ -173,35 +165,31 @@ impl Config {
173165
];
174166
}
175167
";
176-
nix_build_succeeded = try_run(
177-
self,
178-
Command::new("nix-build").args([
179-
Path::new("-E"),
180-
Path::new(NIX_EXPR),
181-
Path::new("-o"),
182-
&nix_deps_dir,
183-
]),
184-
)
185-
.is_ok();
168+
nix_build_succeeded = command("nix-build")
169+
.allow_failure()
170+
.args([Path::new("-E"), Path::new(NIX_EXPR), Path::new("-o"), &nix_deps_dir])
171+
.run_capture(self.context())
172+
.is_success();
186173
nix_deps_dir
187174
});
188175
if !nix_build_succeeded {
189176
return;
190177
}
191178

192-
let mut patchelf = Command::new(nix_deps_dir.join("bin/patchelf"));
193-
patchelf.args(&[
179+
let mut patchelf = command(nix_deps_dir.join("bin/patcheld"));
180+
let patchelf = patchelf.args(&[
194181
OsString::from("--add-rpath"),
195182
OsString::from(t!(fs::canonicalize(nix_deps_dir)).join("lib")),
196183
]);
184+
197185
if !path_is_dylib(fname) {
198186
// Finally, set the correct .interp for binaries
199187
let dynamic_linker_path = nix_deps_dir.join("nix-support/dynamic-linker");
200188
let dynamic_linker = t!(fs::read_to_string(dynamic_linker_path));
201189
patchelf.args(["--set-interpreter", dynamic_linker.trim_end()]);
202190
}
203191

204-
let _ = try_run(self, patchelf.arg(fname));
192+
let _ = patchelf.run_capture(&self.context());
205193
}
206194

207195
fn download_file(&self, url: &str, dest_path: &Path, help_on_error: &str) {
@@ -267,23 +255,25 @@ impl Config {
267255
curl.arg("--progress-bar");
268256
}
269257
// --retry-all-errors was added in 7.71.0, don't use it if curl is old.
270-
if curl_version() >= semver::Version::new(7, 71, 0) {
258+
if self.curl_version() >= semver::Version::new(7, 71, 0) {
271259
curl.arg("--retry-all-errors");
272260
}
273261
curl.arg(url);
274262
if !self.check_run(&mut curl) {
275263
if self.build.contains("windows-msvc") {
276264
eprintln!("Fallback to PowerShell");
277265
for _ in 0..3 {
278-
if try_run(self, Command::new("PowerShell.exe").args([
266+
let powershell = command("PowerShell.exe").allow_failure().args([
279267
"/nologo",
280268
"-Command",
281269
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",
282270
&format!(
283271
"(New-Object System.Net.WebClient).DownloadFile('{}', '{}')",
284272
url, tempfile.to_str().expect("invalid UTF-8 not supported with powershell downloads"),
285273
),
286-
])).is_err() {
274+
]).run_capture_stdout(&self.context());
275+
276+
if powershell.is_failure() {
287277
return;
288278
}
289279
eprintln!("\nspurious failure, trying again");

0 commit comments

Comments
 (0)