From 8d34262956059aca7e6fded351a9299b3581a5cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 16:52:28 +0200 Subject: Rename ra_toolchain -> toolchain --- Cargo.lock | 22 ++++----- crates/flycheck/Cargo.toml | 2 +- crates/flycheck/src/lib.rs | 2 +- crates/ra_proc_macro_srv/Cargo.toml | 2 +- crates/ra_proc_macro_srv/src/tests/utils.rs | 2 +- crates/ra_project_model/Cargo.toml | 2 +- crates/ra_project_model/src/cargo_workspace.rs | 4 +- crates/ra_project_model/src/lib.rs | 2 +- crates/ra_project_model/src/sysroot.rs | 4 +- crates/ra_toolchain/Cargo.toml | 12 ----- crates/ra_toolchain/src/lib.rs | 68 -------------------------- crates/rust-analyzer/Cargo.toml | 2 +- crates/rust-analyzer/src/handlers.rs | 2 +- crates/toolchain/Cargo.toml | 12 +++++ crates/toolchain/src/lib.rs | 66 +++++++++++++++++++++++++ editors/code/src/toolchain.ts | 4 +- 16 files changed, 103 insertions(+), 105 deletions(-) delete mode 100644 crates/ra_toolchain/Cargo.toml delete mode 100644 crates/ra_toolchain/src/lib.rs create mode 100644 crates/toolchain/Cargo.toml create mode 100644 crates/toolchain/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 598b739c7..c48c6a597 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -358,8 +358,8 @@ dependencies = [ "crossbeam-channel", "jod-thread", "log", - "ra_toolchain", "serde_json", + "toolchain", ] [[package]] @@ -1143,9 +1143,9 @@ dependencies = [ "memmap", "ra_mbe", "ra_proc_macro", - "ra_toolchain", "serde_derive", "test_utils", + "toolchain", "tt", ] @@ -1161,11 +1161,11 @@ dependencies = [ "ra_cfg", "ra_db", "ra_proc_macro", - "ra_toolchain", "rustc-hash", "serde", "serde_json", "stdx", + "toolchain", ] [[package]] @@ -1210,13 +1210,6 @@ dependencies = [ "text-size", ] -[[package]] -name = "ra_toolchain" -version = "0.1.0" -dependencies = [ - "home", -] - [[package]] name = "rayon" version = "1.3.1" @@ -1320,7 +1313,6 @@ dependencies = [ "ra_ssr", "ra_syntax", "ra_text_edit", - "ra_toolchain", "rayon", "rustc-hash", "serde", @@ -1328,6 +1320,7 @@ dependencies = [ "stdx", "test_utils", "threadpool", + "toolchain", "tt", "vfs", "vfs-notify", @@ -1612,6 +1605,13 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53953d2d3a5ad81d9f844a32f14ebb121f50b650cd59d0ee2a07cf13c617efed" +[[package]] +name = "toolchain" +version = "0.0.0" +dependencies = [ + "home", +] + [[package]] name = "tracing" version = "0.1.18" diff --git a/crates/flycheck/Cargo.toml b/crates/flycheck/Cargo.toml index ff8a1e568..acc72bc59 100644 --- a/crates/flycheck/Cargo.toml +++ b/crates/flycheck/Cargo.toml @@ -14,4 +14,4 @@ log = "0.4.8" cargo_metadata = "0.11.1" serde_json = "1.0.48" jod-thread = "0.1.1" -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs index 31e14246d..16078d104 100644 --- a/crates/flycheck/src/lib.rs +++ b/crates/flycheck/src/lib.rs @@ -193,7 +193,7 @@ impl FlycheckActor { extra_args, features, } => { - let mut cmd = Command::new(ra_toolchain::cargo()); + let mut cmd = Command::new(toolchain::cargo()); cmd.arg(command); cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]) .arg(self.workspace_root.join("Cargo.toml")); diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml index a690cc044..1c25e7229 100644 --- a/crates/ra_proc_macro_srv/Cargo.toml +++ b/crates/ra_proc_macro_srv/Cargo.toml @@ -23,4 +23,4 @@ cargo_metadata = "0.11.1" difference = "2.0.0" # used as proc macro test target serde_derive = "1.0.106" -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs index dcb00671f..1b6a0b6fb 100644 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -12,7 +12,7 @@ mod fixtures { // Use current project metadata to get the proc-macro dylib path pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf { - let command = Command::new(ra_toolchain::cargo()) + let command = Command::new(toolchain::cargo()) .args(&["check", "--message-format", "json"]) .output() .unwrap() diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml index 27b1f5d33..171fe8626 100644 --- a/crates/ra_project_model/Cargo.toml +++ b/crates/ra_project_model/Cargo.toml @@ -17,7 +17,7 @@ cargo_metadata = "0.11.1" arena = { path = "../arena" } ra_cfg = { path = "../ra_cfg" } ra_db = { path = "../ra_db" } -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } ra_proc_macro = { path = "../ra_proc_macro" } paths = { path = "../paths" } stdx = { path = "../stdx" } diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index a526d743d..abf8dca96 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -140,7 +140,7 @@ impl CargoWorkspace { cargo_features: &CargoConfig, ) -> Result { let mut meta = MetadataCommand::new(); - meta.cargo_path(ra_toolchain::cargo()); + meta.cargo_path(toolchain::cargo()); meta.manifest_path(cargo_toml.to_path_buf()); if cargo_features.all_features { meta.features(CargoOpt::AllFeatures); @@ -288,7 +288,7 @@ pub fn load_extern_resources( cargo_toml: &Path, cargo_features: &CargoConfig, ) -> Result { - let mut cmd = Command::new(ra_toolchain::cargo()); + let mut cmd = Command::new(toolchain::cargo()); cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); if cargo_features.all_features { cmd.arg("--all-features"); diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 300e75135..46f44910c 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -513,7 +513,7 @@ fn get_rustc_cfg_options(target: Option<&str>) -> Vec { } let rustc_cfgs = { - let mut cmd = Command::new(ra_toolchain::rustc()); + let mut cmd = Command::new(toolchain::rustc()); cmd.args(&["--print", "cfg", "-O"]); if let Some(target) = target { cmd.args(&["--target", target]); diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 6ef001769..8239797b6 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -101,13 +101,13 @@ fn get_or_install_rust_src(cargo_toml: &AbsPath) -> Result { return Ok(path); } let current_dir = cargo_toml.parent().unwrap(); - let mut rustc = Command::new(ra_toolchain::rustc()); + let mut rustc = Command::new(toolchain::rustc()); rustc.current_dir(current_dir).args(&["--print", "sysroot"]); let stdout = utf8_stdout(rustc)?; let sysroot_path = AbsPath::assert(Path::new(stdout.trim())); let mut src = get_rust_src(sysroot_path); if src.is_none() { - let mut rustup = Command::new(ra_toolchain::rustup()); + let mut rustup = Command::new(toolchain::rustup()); rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]); utf8_stdout(rustup)?; src = get_rust_src(sysroot_path); diff --git a/crates/ra_toolchain/Cargo.toml b/crates/ra_toolchain/Cargo.toml deleted file mode 100644 index 84b748c0a..000000000 --- a/crates/ra_toolchain/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -edition = "2018" -name = "ra_toolchain" -version = "0.1.0" -authors = ["rust-analyzer developers"] -license = "MIT OR Apache-2.0" - -[lib] -doctest = false - -[dependencies] -home = "0.5.3" diff --git a/crates/ra_toolchain/src/lib.rs b/crates/ra_toolchain/src/lib.rs deleted file mode 100644 index 9916e52c4..000000000 --- a/crates/ra_toolchain/src/lib.rs +++ /dev/null @@ -1,68 +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 std::{env, iter, path::PathBuf}; - -pub fn cargo() -> PathBuf { - get_path_for_executable("cargo") -} - -pub fn rustc() -> PathBuf { - get_path_for_executable("rustc") -} - -pub fn rustup() -> PathBuf { - get_path_for_executable("rustup") -} - -pub fn rustfmt() -> PathBuf { - get_path_for_executable("rustfmt") -} - -/// 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. -fn get_path_for_executable(executable_name: &'static str) -> PathBuf { - // 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 env_var = executable_name.to_ascii_uppercase(); - if let Some(path) = env::var_os(&env_var) { - return path.into(); - } - - if lookup_in_path(executable_name) { - return executable_name.into(); - } - - if let Some(mut path) = home::home_dir() { - path.push(".cargo"); - path.push("bin"); - path.push(executable_name); - if let Some(path) = probe(path) { - return path; - } - } - - executable_name.into() -} - -fn lookup_in_path(exec: &str) -> bool { - let paths = env::var_os("PATH").unwrap_or_default(); - env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some() -} - -fn probe(path: PathBuf) -> Option { - let with_extension = match env::consts::EXE_EXTENSION { - "" => None, - it => Some(path.with_extension(it)), - }; - iter::once(path).chain(with_extension).find(|it| it.is_file()) -} diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 9bc5cc631..3f70510fd 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -43,7 +43,7 @@ ra_text_edit = { path = "../ra_text_edit" } vfs = { path = "../vfs" } vfs-notify = { path = "../vfs-notify" } ra_cfg = { path = "../ra_cfg" } -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } # This should only be used in CLI ra_db = { path = "../ra_db" } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index d9b75eed4..07f4af3d3 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -692,7 +692,7 @@ pub(crate) fn handle_formatting( let mut rustfmt = match &snap.config.rustfmt { RustfmtConfig::Rustfmt { extra_args } => { - let mut cmd = process::Command::new(ra_toolchain::rustfmt()); + let mut cmd = process::Command::new(toolchain::rustfmt()); cmd.args(extra_args); if let Some(&crate_id) = crate_ids.first() { // Assume all crates are in the same edition diff --git a/crates/toolchain/Cargo.toml b/crates/toolchain/Cargo.toml new file mode 100644 index 000000000..4856668f8 --- /dev/null +++ b/crates/toolchain/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "toolchain" +version = "0.0.0" +license = "MIT OR Apache-2.0" +authors = ["rust-analyzer developers"] +edition = "2018" + +[lib] +doctest = false + +[dependencies] +home = "0.5.3" diff --git a/crates/toolchain/src/lib.rs b/crates/toolchain/src/lib.rs new file mode 100644 index 000000000..3b6886f5b --- /dev/null +++ b/crates/toolchain/src/lib.rs @@ -0,0 +1,66 @@ +//! Discovery of `cargo` & `rustc` executables. +use std::{env, iter, path::PathBuf}; + +pub fn cargo() -> PathBuf { + get_path_for_executable("cargo") +} + +pub fn rustc() -> PathBuf { + get_path_for_executable("rustc") +} + +pub fn rustup() -> PathBuf { + get_path_for_executable("rustup") +} + +pub fn rustfmt() -> PathBuf { + get_path_for_executable("rustfmt") +} + +/// 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. +fn get_path_for_executable(executable_name: &'static str) -> PathBuf { + // 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 env_var = executable_name.to_ascii_uppercase(); + if let Some(path) = env::var_os(&env_var) { + return path.into(); + } + + if lookup_in_path(executable_name) { + return executable_name.into(); + } + + if let Some(mut path) = home::home_dir() { + path.push(".cargo"); + path.push("bin"); + path.push(executable_name); + if let Some(path) = probe(path) { + return path; + } + } + + executable_name.into() +} + +fn lookup_in_path(exec: &str) -> bool { + let paths = env::var_os("PATH").unwrap_or_default(); + env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some() +} + +fn probe(path: PathBuf) -> Option { + let with_extension = match env::consts::EXE_EXTENSION { + "" => None, + it => Some(path.with_extension(it)), + }; + iter::once(path).chain(with_extension).find(|it| it.is_file()) +} diff --git a/editors/code/src/toolchain.ts b/editors/code/src/toolchain.ts index 80a7915e9..a5dc3cf0c 100644 --- a/editors/code/src/toolchain.ts +++ b/editors/code/src/toolchain.ts @@ -121,12 +121,12 @@ export class Cargo { } } -/** Mirrors `ra_toolchain::cargo()` implementation */ +/** Mirrors `toolchain::cargo()` implementation */ export function cargoPath(): string { return getPathForExecutable("cargo"); } -/** Mirrors `ra_toolchain::get_path_for_executable()` implementation */ +/** Mirrors `toolchain::get_path_for_executable()` implementation */ export const getPathForExecutable = memoize( // We apply caching to decrease file-system interactions (executableName: "cargo" | "rustc" | "rustup"): string => { -- cgit v1.2.3