rust/crates/ra_toolchain/src/lib.rs

69 lines
2.9 KiB
Rust
Raw Normal View History

//! This crate contains a single public function
//! [`get_path_for_executable`](fn.get_path_for_executable.html).
//! See docs there for more information.
2020-05-08 05:25:36 -05:00
use std::{
env,
path::{Path, PathBuf},
process::Command,
};
2020-05-06 15:28:32 -05:00
use anyhow::{bail, Result};
2020-05-05 15:44:43 -05:00
2020-05-06 14:39:11 -05:00
/// Return a `PathBuf` to use for the given executable.
2020-05-05 15:44:43 -05:00
///
/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
/// gives a valid Cargo executable; or it may return a full path to a valid
/// Cargo.
2020-05-06 14:39:11 -05:00
pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<PathBuf> {
2020-05-05 15:44:43 -05:00
// The current implementation checks three places for an executable to use:
// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
// 2) `<executable_name>`
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
2020-05-05 15:44:43 -05:00
// 3) `~/.cargo/bin/<executable_name>`
// example: for cargo, this tries ~/.cargo/bin/cargo
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
let executable_name = executable_name.as_ref();
let env_var = executable_name.to_ascii_uppercase();
if let Ok(path) = env::var(&env_var) {
if is_valid_executable(&path) {
2020-05-06 14:39:11 -05:00
Ok(path.into())
2020-05-05 15:44:43 -05:00
} else {
2020-05-06 15:28:32 -05:00
bail!(
2020-05-05 16:07:52 -05:00
"`{}` environment variable points to something that's not a valid executable",
env_var
2020-05-06 15:28:32 -05:00
)
2020-05-05 15:44:43 -05:00
}
} else {
2020-05-05 18:09:39 -05:00
if is_valid_executable(executable_name) {
2020-05-06 14:39:11 -05:00
return Ok(executable_name.into());
2020-05-05 18:09:39 -05:00
}
2020-05-07 14:06:44 -05:00
if let Some(mut path) = ::home::home_dir() {
2020-05-05 18:09:39 -05:00
path.push(".cargo");
path.push("bin");
path.push(executable_name);
if is_valid_executable(&path) {
2020-05-06 14:39:11 -05:00
return Ok(path);
2020-05-05 15:44:43 -05:00
}
2020-05-05 18:09:39 -05:00
}
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
// for VSCode, even if they are set correctly in a terminal.
// On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
// to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
// but launching VSCode from Dock does not inherit environment variables from a terminal.
// For more discussion, see #3118.
2020-05-06 15:28:32 -05:00
bail!(
"Failed to find `{}` executable. Make sure `{}` is in `$PATH`, or set `${}` to point to a valid executable.",
executable_name, executable_name, env_var
)
2020-05-05 15:44:43 -05:00
}
}
/// Does the given `Path` point to a usable executable?
///
/// (assumes the executable takes a `--version` switch and writes to stdout,
/// which is true for `cargo`, `rustc`, and `rustup`)
fn is_valid_executable(p: impl AsRef<Path>) -> bool {
Command::new(p.as_ref()).arg("--version").output().is_ok()
}