toolchain/
lib.rs

1//! Discovery of `cargo` & `rustc` executables.
2
3use std::{
4    env,
5    ffi::OsStr,
6    iter,
7    path::{Path, PathBuf},
8    process::Command,
9};
10
11use camino::{Utf8Path, Utf8PathBuf};
12
13#[derive(Copy, Clone)]
14pub enum Tool {
15    Cargo,
16    Rustc,
17    Rustup,
18    Rustfmt,
19}
20
21impl Tool {
22    pub fn proxy(self) -> Option<Utf8PathBuf> {
23        cargo_proxy(self.name())
24    }
25
26    /// Return a `PathBuf` to use for the given executable.
27    ///
28    /// The current implementation checks three places for an executable to use:
29    /// 1) `$CARGO_HOME/bin/<executable_name>`
30    ///    where $CARGO_HOME defaults to ~/.cargo (see <https://doc.rust-lang.org/cargo/guide/cargo-home.html>)
31    ///    example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
32    ///    It seems that this is a reasonable place to try for cargo, rustc, and rustup
33    /// 2) Appropriate environment variable (erroring if this is set but not a usable executable)
34    ///    example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
35    /// 3) $PATH/`<executable_name>`
36    ///    example: for cargo, this tries all paths in $PATH with appended `cargo`, returning the
37    ///    first that exists
38    /// 4) If all else fails, we just try to use the executable name directly
39    pub fn prefer_proxy(self) -> Utf8PathBuf {
40        invoke(&[cargo_proxy, lookup_as_env_var, lookup_in_path], self.name())
41    }
42
43    /// Return a `PathBuf` to use for the given executable.
44    ///
45    /// The current implementation checks three places for an executable to use:
46    /// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
47    ///    example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
48    /// 2) $PATH/`<executable_name>`
49    ///    example: for cargo, this tries all paths in $PATH with appended `cargo`, returning the
50    ///    first that exists
51    /// 3) `$CARGO_HOME/bin/<executable_name>`
52    ///    where $CARGO_HOME defaults to ~/.cargo (see <https://doc.rust-lang.org/cargo/guide/cargo-home.html>)
53    ///    example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
54    ///    It seems that this is a reasonable place to try for cargo, rustc, and rustup
55    /// 4) If all else fails, we just try to use the executable name directly
56    pub fn path(self) -> Utf8PathBuf {
57        invoke(&[lookup_as_env_var, lookup_in_path, cargo_proxy], self.name())
58    }
59
60    pub fn path_in(self, path: &Utf8Path) -> Option<Utf8PathBuf> {
61        probe_for_binary(path.join(self.name()))
62    }
63
64    pub fn name(self) -> &'static str {
65        match self {
66            Tool::Cargo => "cargo",
67            Tool::Rustc => "rustc",
68            Tool::Rustup => "rustup",
69            Tool::Rustfmt => "rustfmt",
70        }
71    }
72}
73
74#[allow(clippy::disallowed_types)] /* generic parameter allows for FxHashMap */
75pub fn command<H>(
76    cmd: impl AsRef<OsStr>,
77    working_directory: impl AsRef<Path>,
78    extra_env: &std::collections::HashMap<String, Option<String>, H>,
79) -> Command {
80    // we are `toolchain::command``
81    #[allow(clippy::disallowed_methods)]
82    let mut cmd = Command::new(cmd);
83    cmd.current_dir(working_directory);
84    for env in extra_env {
85        match env {
86            (key, Some(val)) => cmd.env(key, val),
87            (key, None) => cmd.env_remove(key),
88        };
89    }
90    cmd
91}
92
93fn invoke(list: &[fn(&str) -> Option<Utf8PathBuf>], executable: &str) -> Utf8PathBuf {
94    list.iter().find_map(|it| it(executable)).unwrap_or_else(|| executable.into())
95}
96
97/// Looks up the binary as its SCREAMING upper case in the env variables.
98fn lookup_as_env_var(executable_name: &str) -> Option<Utf8PathBuf> {
99    env::var_os(executable_name.to_ascii_uppercase())
100        .map(PathBuf::from)
101        .map(Utf8PathBuf::try_from)
102        .and_then(Result::ok)
103}
104
105/// Looks up the binary in the cargo home directory if it exists.
106fn cargo_proxy(executable_name: &str) -> Option<Utf8PathBuf> {
107    let mut path = get_cargo_home()?;
108    path.push("bin");
109    path.push(executable_name);
110    probe_for_binary(path)
111}
112
113fn get_cargo_home() -> Option<Utf8PathBuf> {
114    if let Some(path) = env::var_os("CARGO_HOME") {
115        return Utf8PathBuf::try_from(PathBuf::from(path)).ok();
116    }
117
118    if let Some(mut path) = home::home_dir() {
119        path.push(".cargo");
120        return Utf8PathBuf::try_from(path).ok();
121    }
122
123    None
124}
125
126fn lookup_in_path(exec: &str) -> Option<Utf8PathBuf> {
127    let paths = env::var_os("PATH").unwrap_or_default();
128    env::split_paths(&paths)
129        .map(|path| path.join(exec))
130        .map(Utf8PathBuf::try_from)
131        .filter_map(Result::ok)
132        .find_map(probe_for_binary)
133}
134
135pub fn probe_for_binary(path: Utf8PathBuf) -> Option<Utf8PathBuf> {
136    let with_extension = match env::consts::EXE_EXTENSION {
137        "" => None,
138        it => Some(path.with_extension(it)),
139    };
140    iter::once(path).chain(with_extension).find(|it| it.is_file())
141}