From 6713be0b130670324c61d9deb38b7b6aee6a8bac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 May 2020 12:25:36 +0200 Subject: Rename ra_env -> ra_toolchain --- crates/ra_env/Cargo.toml | 9 ---- crates/ra_env/src/lib.rs | 66 ------------------------- crates/ra_flycheck/Cargo.toml | 2 +- crates/ra_flycheck/src/lib.rs | 2 +- crates/ra_project_model/Cargo.toml | 2 +- crates/ra_project_model/src/cargo_workspace.rs | 2 +- crates/ra_project_model/src/lib.rs | 2 +- crates/ra_project_model/src/sysroot.rs | 4 +- crates/ra_toolchain/Cargo.toml | 9 ++++ crates/ra_toolchain/src/lib.rs | 68 ++++++++++++++++++++++++++ 10 files changed, 84 insertions(+), 82 deletions(-) delete mode 100644 crates/ra_env/Cargo.toml delete mode 100644 crates/ra_env/src/lib.rs create mode 100644 crates/ra_toolchain/Cargo.toml create mode 100644 crates/ra_toolchain/src/lib.rs (limited to 'crates') diff --git a/crates/ra_env/Cargo.toml b/crates/ra_env/Cargo.toml deleted file mode 100644 index f0a401be5..000000000 --- a/crates/ra_env/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -edition = "2018" -name = "ra_env" -version = "0.1.0" -authors = ["rust-analyzer developers"] - -[dependencies] -anyhow = "1.0.26" -home = "0.5.3" diff --git a/crates/ra_env/src/lib.rs b/crates/ra_env/src/lib.rs deleted file mode 100644 index 413da1982..000000000 --- a/crates/ra_env/src/lib.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! 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::{bail, Result}; -use std::env; -use std::path::{Path, PathBuf}; -use std::process::Command; - -/// 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 { - // 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.into()) - } else { - bail!( - "`{}` environment variable points to something that's not a valid executable", - env_var - ) - } - } else { - if is_valid_executable(executable_name) { - return Ok(executable_name.into()); - } - if let Some(mut path) = ::home::home_dir() { - path.push(".cargo"); - path.push("bin"); - path.push(executable_name); - if is_valid_executable(&path) { - return Ok(path); - } - } - // 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. - bail!( - "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() -} diff --git a/crates/ra_flycheck/Cargo.toml b/crates/ra_flycheck/Cargo.toml index d0f7fb2dc..03e557148 100644 --- a/crates/ra_flycheck/Cargo.toml +++ b/crates/ra_flycheck/Cargo.toml @@ -14,7 +14,7 @@ log = "0.4.8" cargo_metadata = "0.9.1" serde_json = "1.0.48" jod-thread = "0.1.1" -ra_env = { path = "../ra_env" } +ra_toolchain = { path = "../ra_toolchain" } [dev-dependencies] insta = "0.16.0" diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index d8b727b0e..561657edb 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs @@ -16,7 +16,7 @@ use lsp_types::{ CodeAction, CodeActionOrCommand, Diagnostic, Url, WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressEnd, WorkDoneProgressReport, }; -use ra_env::get_path_for_executable; +use ra_toolchain::get_path_for_executable; use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic}; diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml index 626478468..a32a5daab 100644 --- a/crates/ra_project_model/Cargo.toml +++ b/crates/ra_project_model/Cargo.toml @@ -16,7 +16,7 @@ cargo_metadata = "0.9.1" ra_arena = { path = "../ra_arena" } ra_cfg = { path = "../ra_cfg" } ra_db = { path = "../ra_db" } -ra_env = { path = "../ra_env" } +ra_toolchain = { path = "../ra_toolchain" } ra_proc_macro = { path = "../ra_proc_macro" } serde = { version = "1.0.106", features = ["derive"] } diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index eb9f33ee8..9683bfcc0 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -11,7 +11,7 @@ use anyhow::{Context, Result}; use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; use ra_arena::{Arena, Idx}; use ra_db::Edition; -use ra_env::get_path_for_executable; +use ra_toolchain::get_path_for_executable; use rustc_hash::FxHashMap; /// `CargoWorkspace` represents the logical structure of, well, a Cargo diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 88a6ffb2a..4f0b9c77e 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -14,7 +14,7 @@ use std::{ use anyhow::{bail, Context, Result}; use ra_cfg::CfgOptions; use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; -use ra_env::get_path_for_executable; +use ra_toolchain::get_path_for_executable; use rustc_hash::FxHashMap; use serde_json::from_reader; diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 11c26ad89..2b628c2a3 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -1,14 +1,14 @@ //! FIXME: write short doc here -use anyhow::{bail, Context, Result}; use std::{ env, ops, path::{Path, PathBuf}, process::{Command, Output}, }; +use anyhow::{bail, Context, Result}; use ra_arena::{Arena, Idx}; -use ra_env::get_path_for_executable; +use ra_toolchain::get_path_for_executable; #[derive(Default, Debug, Clone)] pub struct Sysroot { diff --git a/crates/ra_toolchain/Cargo.toml b/crates/ra_toolchain/Cargo.toml new file mode 100644 index 000000000..fbad1073e --- /dev/null +++ b/crates/ra_toolchain/Cargo.toml @@ -0,0 +1,9 @@ +[package] +edition = "2018" +name = "ra_toolchain" +version = "0.1.0" +authors = ["rust-analyzer developers"] + +[dependencies] +anyhow = "1.0.26" +home = "0.5.3" diff --git a/crates/ra_toolchain/src/lib.rs b/crates/ra_toolchain/src/lib.rs new file mode 100644 index 000000000..110c92c88 --- /dev/null +++ b/crates/ra_toolchain/src/lib.rs @@ -0,0 +1,68 @@ +//! This crate contains a single public function +//! [`get_path_for_executable`](fn.get_path_for_executable.html). +//! See docs there for more information. +use std::{ + env, + path::{Path, PathBuf}, + process::Command, +}; + +use anyhow::{bail, Result}; + +/// 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 { + // 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.into()) + } else { + bail!( + "`{}` environment variable points to something that's not a valid executable", + env_var + ) + } + } else { + if is_valid_executable(executable_name) { + return Ok(executable_name.into()); + } + if let Some(mut path) = ::home::home_dir() { + path.push(".cargo"); + path.push("bin"); + path.push(executable_name); + if is_valid_executable(&path) { + return Ok(path); + } + } + // 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. + bail!( + "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