aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/find_executables.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src/find_executables.rs')
-rw-r--r--crates/ra_project_model/src/find_executables.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/ra_project_model/src/find_executables.rs b/crates/ra_project_model/src/find_executables.rs
new file mode 100644
index 000000000..9b020d3da
--- /dev/null
+++ b/crates/ra_project_model/src/find_executables.rs
@@ -0,0 +1,63 @@
1use anyhow::{Error, Result};
2use std::env;
3use std::path::Path;
4use std::process::Command;
5
6/// Return a `String` to use for the given executable.
7///
8/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
9/// gives a valid Cargo executable; or it may return a full path to a valid
10/// Cargo.
11pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<String> {
12 // The current implementation checks three places for an executable to use:
13 // 1) Appropriate environment variable (erroring if this is set but not a usable executable)
14 // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
15 // 2) `<executable_name>`
16 // example: for cargo, this tries just `cargo`, which will succeed if `cargo` in on the $PATH
17 // 3) `~/.cargo/bin/<executable_name>`
18 // example: for cargo, this tries ~/.cargo/bin/cargo
19 // It seems that this is a reasonable place to try for cargo, rustc, and rustup
20 let executable_name = executable_name.as_ref();
21 let env_var = executable_name.to_ascii_uppercase();
22 if let Ok(path) = env::var(&env_var) {
23 if is_valid_executable(&path) {
24 Ok(path)
25 } else {
26 Err(Error::msg(format!("`{}` environment variable points to something that's not a valid executable", env_var)))
27 }
28 } else {
29 let final_path: Option<String> = if is_valid_executable(executable_name) {
30 Some(executable_name.to_owned())
31 } else {
32 if let Some(mut path) = dirs::home_dir() {
33 path.push(".cargo");
34 path.push("bin");
35 path.push(executable_name);
36 if is_valid_executable(&path) {
37 Some(path.into_os_string().into_string().expect("Invalid Unicode in path"))
38 } else {
39 None
40 }
41 } else {
42 None
43 }
44 };
45 final_path.ok_or(
46 // This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
47 // for VSCode, even if they are set correctly in a terminal.
48 // On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
49 // to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
50 // but launching VSCode from Dock does not inherit environment variables from a terminal.
51 // For more discussion, see #3118.
52 Error::msg(format!("Failed to find `{}` executable. Make sure `{}` is in `$PATH`, or set `${}` to point to a valid executable.", executable_name, executable_name, env_var))
53 )
54 }
55}
56
57/// Does the given `Path` point to a usable executable?
58///
59/// (assumes the executable takes a `--version` switch and writes to stdout,
60/// which is true for `cargo`, `rustc`, and `rustup`)
61fn is_valid_executable(p: impl AsRef<Path>) -> bool {
62 Command::new(p.as_ref()).arg("--version").output().is_ok()
63}