-
Notifications
You must be signed in to change notification settings - Fork 911
/
Copy pathtoolchain.rs
490 lines (446 loc) · 19.2 KB
/
toolchain.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use std::{
env::{self, consts::EXE_SUFFIX},
ffi::{OsStr, OsString},
fmt::Debug,
fs,
io::{self, BufRead, BufReader},
path::{Path, PathBuf},
process::{Command, Stdio},
str::FromStr,
time::Duration,
};
use anyhow::{Context, anyhow, bail};
use fs_at::OpenOptions;
use tracing::info;
use url::Url;
use wait_timeout::ChildExt;
use crate::{
RustupError,
config::{ActiveReason, Cfg, InstalledPath},
dist::PartialToolchainDesc,
env_var, install,
notifications::Notification,
utils::{self, raw::open_dir_following_links},
};
mod distributable;
pub(crate) use distributable::DistributableToolchain;
mod names;
pub(crate) use names::{
CustomToolchainName, LocalToolchainName, MaybeOfficialToolchainName,
MaybeResolvableToolchainName, PathBasedToolchainName, ResolvableLocalToolchainName,
ResolvableToolchainName, ToolchainName,
};
/// A toolchain installed on the local disk
#[derive(Clone, Debug)]
pub(crate) struct Toolchain<'a> {
pub(super) cfg: &'a Cfg<'a>,
name: LocalToolchainName,
path: PathBuf,
}
impl<'a> Toolchain<'a> {
pub(crate) async fn from_local(
name: LocalToolchainName,
install_if_missing: bool,
cfg: &'a Cfg<'a>,
) -> anyhow::Result<Toolchain<'a>> {
match Self::new(cfg, name) {
Ok(tc) => Ok(tc),
Err(RustupError::ToolchainNotInstalled {
name: ToolchainName::Official(desc),
}) if install_if_missing => {
Ok(
DistributableToolchain::install(cfg, &desc, &[], &[], cfg.get_profile()?, true)
.await?
.1
.toolchain,
)
}
Err(e) => Err(e.into()),
}
}
/// Calls Toolchain::new(), but augments the error message with more context
/// from the ActiveReason if the toolchain isn't installed.
pub(crate) fn with_reason(
cfg: &'a Cfg<'a>,
name: LocalToolchainName,
reason: &ActiveReason,
) -> anyhow::Result<Self> {
match Self::new(cfg, name.clone()) {
Err(RustupError::ToolchainNotInstalled { .. }) => (),
result => {
return Ok(result?);
}
}
let reason_err = match reason {
ActiveReason::Environment => {
"the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain"
.to_string()
}
ActiveReason::CommandLine => {
"the +toolchain on the command line specifies an uninstalled toolchain".to_string()
}
ActiveReason::OverrideDB(path) => format!(
"the directory override for '{}' specifies an uninstalled toolchain",
utils::canonicalize_path(path, cfg.notify_handler.as_ref()).display(),
),
ActiveReason::ToolchainFile(path) => format!(
"the toolchain file at '{}' specifies an uninstalled toolchain",
utils::canonicalize_path(path, cfg.notify_handler.as_ref()).display(),
),
ActiveReason::Default => {
"the default toolchain does not describe an installed toolchain".to_string()
}
};
Err(anyhow!(reason_err).context(format!("override toolchain '{name}' is not installed")))
}
pub(crate) fn new(cfg: &'a Cfg<'a>, name: LocalToolchainName) -> Result<Self, RustupError> {
let path = cfg.toolchain_path(&name);
if !Toolchain::exists(cfg, &name)? {
return Err(match name {
LocalToolchainName::Named(name) => RustupError::ToolchainNotInstalled { name },
LocalToolchainName::Path(name) => RustupError::PathToolchainNotInstalled(name),
});
}
Ok(Self { cfg, name, path })
}
/// Ok(True) if the toolchain exists. Ok(False) if the toolchain or its
/// containing directory don't exist. Err otherwise.
pub(crate) fn exists(cfg: &Cfg<'_>, name: &LocalToolchainName) -> Result<bool, RustupError> {
let path = cfg.toolchain_path(name);
// toolchain validation should have prevented a situation where there is
// no base dir, but defensive programming is defensive.
let parent = path
.parent()
.ok_or_else(|| RustupError::InvalidToolchainName(name.to_string()))?;
let base_name = path
.file_name()
.ok_or_else(|| RustupError::InvalidToolchainName(name.to_string()))?;
let parent_dir = match open_dir_following_links(parent) {
Ok(d) => d,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(false),
e => e?,
};
let opened = OpenOptions::default()
.read(true)
.follow(true)
.open_dir_at(&parent_dir, base_name);
Ok(opened.is_ok())
}
pub(crate) fn name(&self) -> &LocalToolchainName {
&self.name
}
pub(super) fn path(&self) -> &Path {
&self.path
}
/// The path to a binary within the toolchain, without regard for cargo-fallback logic
pub fn binary_file(&self, name: &str) -> PathBuf {
let mut path = self.path.clone();
path.push("bin");
path.push(name.to_owned() + env::consts::EXE_SUFFIX);
path
}
/// Not intended to be public, but more code golf required to get it hidden.
/// pub because of create_fallback_command
pub fn set_env(&self, cmd: &mut Command) {
self.set_ldpath(cmd);
// Older versions of Cargo used a slightly different definition of
// cargo home. Rustup does not read HOME on Windows whereas the older
// versions of Cargo did. Rustup and Cargo should be in sync now (both
// using the same `home` crate), but this is retained to ensure cargo
// and rustup agree in older versions.
if let Ok(cargo_home) = self.cfg.process.cargo_home() {
cmd.env("CARGO_HOME", &cargo_home);
}
env_var::inc("RUST_RECURSION_COUNT", cmd, self.cfg.process);
cmd.env("RUSTUP_TOOLCHAIN", format!("{}", self.name));
cmd.env("RUSTUP_HOME", &self.cfg.rustup_dir);
}
/// Apply the appropriate LD path for a command being run from a toolchain.
fn set_ldpath(&self, cmd: &mut Command) {
#[cfg_attr(not(target_os = "macos"), allow(unused_mut))]
let mut new_path = vec![self.path.join("lib")];
#[cfg(not(target_os = "macos"))]
mod sysenv {
pub const LOADER_PATH: &str = "LD_LIBRARY_PATH";
}
#[cfg(target_os = "macos")]
mod sysenv {
// When loading and linking a dynamic library or bundle, dlopen
// searches in LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, PWD, and
// DYLD_FALLBACK_LIBRARY_PATH.
// In the Mach-O format, a dynamic library has an "install path."
// Clients linking against the library record this path, and the
// dynamic linker, dyld, uses it to locate the library.
// dyld searches DYLD_LIBRARY_PATH *before* the install path.
// dyld searches DYLD_FALLBACK_LIBRARY_PATH only if it cannot
// find the library in the install path.
// Setting DYLD_LIBRARY_PATH can easily have unintended
// consequences.
pub const LOADER_PATH: &str = "DYLD_FALLBACK_LIBRARY_PATH";
}
#[cfg(target_os = "macos")]
if self
.cfg
.process
.var_os(sysenv::LOADER_PATH)
.filter(|x| !x.is_empty())
.is_none()
{
// These are the defaults when DYLD_FALLBACK_LIBRARY_PATH isn't
// set or set to an empty string. Since we are explicitly setting
// the value, make sure the defaults still work.
if let Some(home) = self.cfg.process.var_os("HOME") {
new_path.push(PathBuf::from(home).join("lib"));
}
new_path.push(PathBuf::from("/usr/local/lib"));
new_path.push(PathBuf::from("/usr/lib"));
}
env_var::prepend_path(sysenv::LOADER_PATH, new_path, cmd, self.cfg.process);
// Prepend CARGO_HOME/bin to the PATH variable so that we're sure to run
// cargo/rustc via the proxy bins. There is no fallback case for if the
// proxy bins don't exist. We'll just be running whatever happens to
// be on the PATH.
let mut path_entries = vec![];
if let Ok(cargo_home) = self.cfg.process.cargo_home() {
path_entries.push(cargo_home.join("bin"));
}
// Historically rustup included the bin directory in PATH to
// work around some bugs (see
// https://github.com/rust-lang/rustup/pull/3178 for more
// information). This shouldn't be needed anymore, and it causes
// problems because calling tools recursively (like `cargo
// +nightly metadata` from within a cargo subcommand). The
// recursive call won't work because it is not executing the
// proxy, so the `+` toolchain override doesn't work.
//
// The RUSTUP_WINDOWS_PATH_ADD_BIN env var was added to opt-in to
// testing the fix. The default is now off, but this is left here
// just in case there are problems. Consider removing in the
// future if it doesn't seem necessary.
#[cfg(target_os = "windows")]
if self
.cfg
.process
.var_os("RUSTUP_WINDOWS_PATH_ADD_BIN")
.is_some_and(|s| s == "1")
{
path_entries.push(self.path.join("bin"));
}
env_var::prepend_path("PATH", path_entries, cmd, self.cfg.process);
}
/// Infallible function that describes the version of rustc in an installed distribution
#[tracing::instrument(level = "trace")]
pub fn rustc_version(&self) -> String {
match self.create_command("rustc") {
Ok(mut cmd) => {
cmd.arg("--version");
cmd.stdin(Stdio::null());
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
self.set_ldpath(&mut cmd);
// some toolchains are faulty with some combinations of platforms and
// may fail to launch but also to timely terminate.
// (known cases include Rust 1.3.0 through 1.10.0 in recent macOS Sierra.)
// we guard against such cases by enforcing a reasonable timeout to read.
let mut line1 = None;
if let Ok(mut child) = cmd.spawn() {
let timeout = Duration::new(10, 0);
match child.wait_timeout(timeout) {
Ok(Some(status)) if status.success() => {
let out = child
.stdout
.expect("Child::stdout requested but not present");
let mut line = String::new();
if BufReader::new(out).read_line(&mut line).is_ok() {
let lineend = line.trim_end_matches(&['\r', '\n'][..]).len();
line.truncate(lineend);
line1 = Some(line);
}
}
Ok(None) => {
let _ = child.kill();
return String::from("(timeout reading rustc version)");
}
Ok(Some(_)) | Err(_) => {}
}
}
if let Some(line1) = line1 {
line1
} else {
String::from("(error reading rustc version)")
}
}
Err(_) => String::from("(rustc does not exist)"),
}
}
pub(crate) fn command(&self, binary: &str) -> anyhow::Result<Command> {
// Should push the cargo fallback into a custom toolchain type? And then
// perhaps a trait that create command layers on?
if !matches!(
self.name(),
LocalToolchainName::Named(ToolchainName::Official(_))
) {
if let Some(cmd) = self.maybe_do_cargo_fallback(binary)? {
return Ok(cmd);
}
}
self.create_command(binary)
}
// Custom toolchains don't have cargo, so here we detect that situation and
// try to find a different cargo.
pub(crate) fn maybe_do_cargo_fallback(&self, binary: &str) -> anyhow::Result<Option<Command>> {
if binary != "cargo" && binary != "cargo.exe" {
return Ok(None);
}
let cargo_path = self.binary_file("cargo");
// breadcrumb in case of regression: we used to get the cargo path and
// cargo.exe path separately, not using the binary_file helper. This may
// matter if calling a binary with some personality that allows .exe and
// not .exe to coexist (e.g. wine) - but that's not something we aim to
// support : the host should always be correct.
if cargo_path.exists() {
return Ok(None);
}
let default_host_triple = self.cfg.get_default_host_triple()?;
// XXX: This could actually consider all installed distributable
// toolchains in principle.
for fallback in ["nightly", "beta", "stable"] {
let resolved =
PartialToolchainDesc::from_str(fallback)?.resolve(&default_host_triple)?;
if let Ok(fallback) = DistributableToolchain::new(self.cfg, resolved) {
let cmd = fallback.create_fallback_command("cargo", self)?;
return Ok(Some(cmd));
}
}
Ok(None)
}
#[cfg_attr(feature="otel", tracing::instrument(err,fields(binary, recursion=self.cfg.process.var("RUST_RECURSION_COUNT").ok())))]
fn create_command<T: AsRef<OsStr> + Debug>(&self, binary: T) -> Result<Command, anyhow::Error> {
// Create the path to this binary within the current toolchain sysroot
let binary = if let Some(binary_str) = binary.as_ref().to_str() {
if binary_str.to_lowercase().ends_with(EXE_SUFFIX) {
binary.as_ref().to_owned()
} else {
OsString::from(format!("{binary_str}{EXE_SUFFIX}"))
}
} else {
// Very weird case. Non-unicode command.
binary.as_ref().to_owned()
};
let bin_path = self.path.join("bin").join(&binary);
let path = if utils::is_file(&bin_path) {
&bin_path
} else {
let recursion_count = self
.cfg
.process
.var("RUST_RECURSION_COUNT")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(0);
if recursion_count > env_var::RUST_RECURSION_COUNT_MAX - 1 {
let binary_lossy: String = binary.to_string_lossy().into();
if matches!(
&self.name,
LocalToolchainName::Named(ToolchainName::Official(_))
) {
let distributable = DistributableToolchain::try_from(self)?;
// Design note: this is a bit of an awkward cast from
// general (toolchain) to more specialised (distributable);
// perhaps this function should something implemented on a
// trait, permitting removal of that case.
return Err(distributable.recursion_error(binary_lossy).unwrap_err());
} else {
let t = &self.name;
return Err(anyhow!(
"'{binary_lossy}' is not installed for the custom toolchain '{t}'.\nnote: this is a custom toolchain, which cannot use `rustup component add`\n\
help: if you built this toolchain from source, and used `rustup toolchain link`, then you may be able to build the component with `x.py`"
));
}
}
Path::new(&binary)
};
let mut cmd = Command::new(path);
self.set_env(&mut cmd);
Ok(cmd)
}
#[cfg(not(windows))]
pub(crate) fn man_path(&self) -> PathBuf {
let mut buf = PathBuf::from(&self.path);
buf.extend(["share", "man"]);
buf
}
pub fn doc_path(&self, relative: impl AsRef<Path>) -> anyhow::Result<PathBuf> {
let relative = relative.as_ref();
if relative.is_absolute() {
return Ok(relative.to_owned());
}
let mut doc_dir = self.path.clone();
doc_dir.extend(["share", "doc", "rust", "html"]);
doc_dir.push(relative);
Ok(doc_dir)
}
pub fn open_docs(
&self,
relative: impl AsRef<Path>,
fragment: Option<&str>,
) -> anyhow::Result<()> {
let relative = relative.as_ref();
let mut doc_url = Url::from_file_path(self.doc_path(relative)?)
.ok()
.with_context(|| anyhow!("invalid doc file absolute path `{}`", relative.display()))?;
doc_url.set_fragment(fragment);
utils::open_browser(doc_url.to_string())
}
/// Remove the toolchain from disk
///
///
pub fn ensure_removed(cfg: &Cfg<'_>, name: LocalToolchainName) -> anyhow::Result<()> {
let path = cfg.toolchain_path(&name);
let name = match name {
LocalToolchainName::Named(t) => t,
LocalToolchainName::Path(_) => bail!("Cannot remove a path based toolchain"),
};
let fs_modified = match Self::exists(cfg, &(&name).into())? {
true => {
(cfg.notify_handler)(Notification::UninstallingToolchain(&name));
let installed_paths = match &name {
ToolchainName::Custom(_) => Ok(vec![InstalledPath::Dir { path: &path }]),
ToolchainName::Official(desc) => cfg.installed_paths(desc, &path),
}?;
for path in installed_paths {
match path {
InstalledPath::File { name, path } => {
utils::ensure_file_removed(name, &path)?
}
InstalledPath::Dir { path } => {
install::uninstall(path, &|n| (cfg.notify_handler)(n.into()))?
}
}
}
true
}
false => {
// Might be a dangling symlink
if path.is_symlink() {
(cfg.notify_handler)(Notification::UninstallingToolchain(&name));
fs::remove_dir_all(&path)?;
true
} else {
let name = name.to_string();
info!("no toolchain installed for '{name}'");
if name == "self" {
info!(
"if you meant to uninstall rustup itself, use `rustup self uninstall`"
);
}
false
}
}
};
if !path.is_symlink() && !path.exists() && fs_modified {
(cfg.notify_handler)(Notification::UninstalledToolchain(&name));
}
Ok(())
}
}