aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_env
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_env')
-rw-r--r--crates/ra_env/Cargo.toml9
-rw-r--r--crates/ra_env/src/lib.rs63
2 files changed, 72 insertions, 0 deletions
diff --git a/crates/ra_env/Cargo.toml b/crates/ra_env/Cargo.toml
new file mode 100644
index 000000000..7fed446a7
--- /dev/null
+++ b/crates/ra_env/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2edition = "2018"
3name = "ra_env"
4version = "0.1.0"
5authors = ["rust-analyzer developers"]
6
7[dependencies]
8anyhow = "1.0.26"
9dirs = "2.0"
diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs
new file mode 100644
index 000000000..cb9fbf80c
--- /dev/null
+++ b/crates/ra_env/src/lib.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` is 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}