aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_env/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-08 11:11:19 +0100
committerGitHub <[email protected]>2020-05-08 11:11:19 +0100
commit8295a9340c1fbda805497035054ead0b10c0d88e (patch)
treeccab3f149b9633ae95570d78fa5d6b8d3b3392e6 /crates/ra_env/src
parent363c1f2f493d206f2cc10c348a02f3efadd8c77a (diff)
parent3077eae2a61f97c28c0d4e3456f6ab873126e5b8 (diff)
Merge #4329
4329: Look for `cargo`, `rustc`, and `rustup` in standard installation path r=matklad a=cdisselkoen Discussed in #3118. This is approximately a 90% fix for the issue described there. This PR creates a new crate `ra_env` with a function `get_path_for_executable()`; see docs there. `get_path_for_executable()` improves and generalizes the function `cargo_binary()` which was previously duplicated in the `ra_project_model` and `ra_flycheck` crates. (Both of those crates now depend on the new `ra_env` crate.) The new function checks (e.g.) `$CARGO` and `$PATH`, but also falls back on `~/.cargo/bin` manually before erroring out. This should allow most users to not have to worry about setting the `$CARGO` or `$PATH` variables for VSCode, which can be difficult e.g. on macOS as discussed in #3118. I've attempted to replace all calls to `cargo`, `rustc`, and `rustup` in rust-analyzer with appropriate invocations of `get_path_for_executable()`; I don't think I've missed any in Rust code, but there is at least one invocation in TypeScript code which I haven't fixed. (I'm not sure whether it's affected by the same problem or not.) https://github.com/rust-analyzer/rust-analyzer/blob/a4778ddb7a00f552a8e653bbf56ae9fd69cfe1d3/editors/code/src/cargo.ts#L79 I'm sure this PR could be improved a bunch, so I'm happy to take feedback/suggestions on how to solve this problem better, or just bikeshedding variable/function/crate names etc. cc @Veetaha Fixes #3118. Co-authored-by: Craig Disselkoen <[email protected]> Co-authored-by: veetaha <[email protected]>
Diffstat (limited to 'crates/ra_env/src')
-rw-r--r--crates/ra_env/src/lib.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs
new file mode 100644
index 000000000..413da1982
--- /dev/null
+++ b/crates/ra_env/src/lib.rs
@@ -0,0 +1,66 @@
1//! This crate contains a single public function
2//! [`get_path_for_executable`](fn.get_path_for_executable.html).
3//! See docs there for more information.
4
5use anyhow::{bail, Result};
6use std::env;
7use std::path::{Path, PathBuf};
8use std::process::Command;
9
10/// Return a `PathBuf` to use for the given executable.
11///
12/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
13/// gives a valid Cargo executable; or it may return a full path to a valid
14/// Cargo.
15pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<PathBuf> {
16 // The current implementation checks three places for an executable to use:
17 // 1) Appropriate environment variable (erroring if this is set but not a usable executable)
18 // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
19 // 2) `<executable_name>`
20 // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
21 // 3) `~/.cargo/bin/<executable_name>`
22 // example: for cargo, this tries ~/.cargo/bin/cargo
23 // It seems that this is a reasonable place to try for cargo, rustc, and rustup
24 let executable_name = executable_name.as_ref();
25 let env_var = executable_name.to_ascii_uppercase();
26 if let Ok(path) = env::var(&env_var) {
27 if is_valid_executable(&path) {
28 Ok(path.into())
29 } else {
30 bail!(
31 "`{}` environment variable points to something that's not a valid executable",
32 env_var
33 )
34 }
35 } else {
36 if is_valid_executable(executable_name) {
37 return Ok(executable_name.into());
38 }
39 if let Some(mut path) = ::home::home_dir() {
40 path.push(".cargo");
41 path.push("bin");
42 path.push(executable_name);
43 if is_valid_executable(&path) {
44 return Ok(path);
45 }
46 }
47 // This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
48 // for VSCode, even if they are set correctly in a terminal.
49 // On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
50 // to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
51 // but launching VSCode from Dock does not inherit environment variables from a terminal.
52 // For more discussion, see #3118.
53 bail!(
54 "Failed to find `{}` executable. Make sure `{}` is in `$PATH`, or set `${}` to point to a valid executable.",
55 executable_name, executable_name, env_var
56 )
57 }
58}
59
60/// Does the given `Path` point to a usable executable?
61///
62/// (assumes the executable takes a `--version` switch and writes to stdout,
63/// which is true for `cargo`, `rustc`, and `rustup`)
64fn is_valid_executable(p: impl AsRef<Path>) -> bool {
65 Command::new(p.as_ref()).arg("--version").output().is_ok()
66}