From 303b444dbb66019fc916dd350e54f7675aa3007f Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Tue, 5 May 2020 14:07:10 -0700 Subject: pull function out into new crate ra_env; use in ra_flycheck as well --- crates/ra_env/Cargo.toml | 9 +++++++ crates/ra_env/src/lib.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 crates/ra_env/Cargo.toml create mode 100644 crates/ra_env/src/lib.rs (limited to 'crates/ra_env') 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 @@ +[package] +edition = "2018" +name = "ra_env" +version = "0.1.0" +authors = ["rust-analyzer developers"] + +[dependencies] +anyhow = "1.0.26" +dirs = "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 @@ +use anyhow::{Error, Result}; +use std::env; +use std::path::Path; +use std::process::Command; + +/// Return a `String` to use for the given executable. +/// +/// 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. +pub fn get_path_for_executable(executable_name: impl AsRef) -> Result { + // 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) `` + // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH + // 3) `~/.cargo/bin/` + // 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) { + Ok(path) + } else { + Err(Error::msg(format!("`{}` environment variable points to something that's not a valid executable", env_var))) + } + } else { + let final_path: Option = if is_valid_executable(executable_name) { + Some(executable_name.to_owned()) + } else { + if let Some(mut path) = dirs::home_dir() { + path.push(".cargo"); + path.push("bin"); + path.push(executable_name); + if is_valid_executable(&path) { + Some(path.into_os_string().into_string().expect("Invalid Unicode in path")) + } else { + None + } + } else { + None + } + }; + final_path.ok_or( + // 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 ` 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. + 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)) + ) + } +} + +/// 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) -> bool { + Command::new(p.as_ref()).arg("--version").output().is_ok() +} -- cgit v1.2.3 From 7e60264ba0dc33110559390868c7c966f0ab2e64 Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Tue, 5 May 2020 14:07:52 -0700 Subject: cargo fmt --- crates/ra_env/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'crates/ra_env') diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs index cb9fbf80c..df20f783d 100644 --- a/crates/ra_env/src/lib.rs +++ b/crates/ra_env/src/lib.rs @@ -23,7 +23,10 @@ pub fn get_path_for_executable(executable_name: impl AsRef) -> Result = if is_valid_executable(executable_name) { -- cgit v1.2.3 From 3e603a8fdd207f9ad5a2ad2898350f54d5bc2fb8 Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Tue, 5 May 2020 14:41:47 -0700 Subject: add module-level docs so that tests pass --- crates/ra_env/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/ra_env') diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs index df20f783d..c7b49e997 100644 --- a/crates/ra_env/src/lib.rs +++ b/crates/ra_env/src/lib.rs @@ -1,3 +1,7 @@ +//! This crate contains a single public function +//! [`get_path_for_executable`](fn.get_path_for_executable.html). +//! See docs there for more information. + use anyhow::{Error, Result}; use std::env; use std::path::Path; -- cgit v1.2.3 From 1b76b4281e90292922455a9192f82a2b6b80d279 Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Tue, 5 May 2020 16:09:39 -0700 Subject: simplify some code using early returns --- crates/ra_env/src/lib.rs | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) (limited to 'crates/ra_env') diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs index c7b49e997..8d6aa9268 100644 --- a/crates/ra_env/src/lib.rs +++ b/crates/ra_env/src/lib.rs @@ -33,31 +33,24 @@ pub fn get_path_for_executable(executable_name: impl AsRef) -> Result = if is_valid_executable(executable_name) { - Some(executable_name.to_owned()) - } else { - if let Some(mut path) = dirs::home_dir() { - path.push(".cargo"); - path.push("bin"); - path.push(executable_name); - if is_valid_executable(&path) { - Some(path.into_os_string().into_string().expect("Invalid Unicode in path")) - } else { - None - } - } else { - None + if is_valid_executable(executable_name) { + return Ok(executable_name.to_owned()); + } + if let Some(mut path) = dirs::home_dir() { + path.push(".cargo"); + path.push("bin"); + path.push(executable_name); + if is_valid_executable(&path) { + return Ok(path.into_os_string().into_string().expect("Invalid Unicode in path")); } - }; - final_path.ok_or( - // 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 ` 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. - 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)) - ) + } + // 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 ` 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. + Err(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))) } } -- cgit v1.2.3 From 44b01ccff3d993daae237c75d466050711d06268 Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Wed, 6 May 2020 12:39:11 -0700 Subject: return a PathBuf instead of String --- crates/ra_env/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_env') diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs index 8d6aa9268..a1c4239be 100644 --- a/crates/ra_env/src/lib.rs +++ b/crates/ra_env/src/lib.rs @@ -4,15 +4,15 @@ use anyhow::{Error, Result}; use std::env; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::Command; -/// Return a `String` to use for the given executable. +/// Return a `PathBuf` to use for the given executable. /// /// 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. -pub fn get_path_for_executable(executable_name: impl AsRef) -> Result { +pub fn get_path_for_executable(executable_name: impl AsRef) -> Result { // 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 @@ -25,7 +25,7 @@ pub fn get_path_for_executable(executable_name: impl AsRef) -> Result) -> Result Date: Wed, 6 May 2020 13:28:32 -0700 Subject: simplify by using bail! macro --- crates/ra_env/src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'crates/ra_env') diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs index a1c4239be..4f94bffde 100644 --- a/crates/ra_env/src/lib.rs +++ b/crates/ra_env/src/lib.rs @@ -2,7 +2,7 @@ //! [`get_path_for_executable`](fn.get_path_for_executable.html). //! See docs there for more information. -use anyhow::{Error, Result}; +use anyhow::{bail, Result}; use std::env; use std::path::{Path, PathBuf}; use std::process::Command; @@ -27,10 +27,10 @@ pub fn get_path_for_executable(executable_name: impl AsRef) -> Result) -> Result Date: Thu, 7 May 2020 12:06:44 -0700 Subject: use home crate instead of dirs --- crates/ra_env/Cargo.toml | 2 +- crates/ra_env/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_env') diff --git a/crates/ra_env/Cargo.toml b/crates/ra_env/Cargo.toml index 7fed446a7..f0a401be5 100644 --- a/crates/ra_env/Cargo.toml +++ b/crates/ra_env/Cargo.toml @@ -6,4 +6,4 @@ authors = ["rust-analyzer developers"] [dependencies] anyhow = "1.0.26" -dirs = "2.0" +home = "0.5.3" diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs index 4f94bffde..413da1982 100644 --- a/crates/ra_env/src/lib.rs +++ b/crates/ra_env/src/lib.rs @@ -36,7 +36,7 @@ pub fn get_path_for_executable(executable_name: impl AsRef) -> Result