aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src
diff options
context:
space:
mode:
authorCraig Disselkoen <[email protected]>2020-05-05 22:07:10 +0100
committerCraig Disselkoen <[email protected]>2020-05-06 00:12:56 +0100
commit303b444dbb66019fc916dd350e54f7675aa3007f (patch)
tree163086a232e07ed91452122da5346383adaca58f /crates/ra_project_model/src
parent5aa1bba107ef434e61c3136120b9478a307d67a9 (diff)
pull function out into new crate ra_env; use in ra_flycheck as well
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r--crates/ra_project_model/src/cargo_workspace.rs2
-rw-r--r--crates/ra_project_model/src/find_executables.rs63
-rw-r--r--crates/ra_project_model/src/lib.rs4
-rw-r--r--crates/ra_project_model/src/sysroot.rs5
4 files changed, 6 insertions, 68 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs
index c7350d1e0..4027f020f 100644
--- a/crates/ra_project_model/src/cargo_workspace.rs
+++ b/crates/ra_project_model/src/cargo_workspace.rs
@@ -7,11 +7,11 @@ use std::{
7 process::Command, 7 process::Command,
8}; 8};
9 9
10use super::find_executables::get_path_for_executable;
11use anyhow::{Context, Result}; 10use anyhow::{Context, Result};
12use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; 11use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
13use ra_arena::{Arena, Idx}; 12use ra_arena::{Arena, Idx};
14use ra_db::Edition; 13use ra_db::Edition;
14use ra_env::get_path_for_executable;
15use rustc_hash::FxHashMap; 15use rustc_hash::FxHashMap;
16 16
17/// `CargoWorkspace` represents the logical structure of, well, a Cargo 17/// `CargoWorkspace` represents the logical structure of, well, a Cargo
diff --git a/crates/ra_project_model/src/find_executables.rs b/crates/ra_project_model/src/find_executables.rs
deleted file mode 100644
index 9b020d3da..000000000
--- a/crates/ra_project_model/src/find_executables.rs
+++ /dev/null
@@ -1,63 +0,0 @@
1use anyhow::{Error, Result};
2use std::env;
3use std::path::Path;
4use std::process::Command;
5
6/// Return a `String` to use for the given executable.
7///
8/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
9/// gives a valid Cargo executable; or it may return a full path to a valid
10/// Cargo.
11pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<String> {
12 // The current implementation checks three places for an executable to use:
13 // 1) Appropriate environment variable (erroring if this is set but not a usable executable)
14 // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
15 // 2) `<executable_name>`
16 // example: for cargo, this tries just `cargo`, which will succeed if `cargo` in on the $PATH
17 // 3) `~/.cargo/bin/<executable_name>`
18 // example: for cargo, this tries ~/.cargo/bin/cargo
19 // It seems that this is a reasonable place to try for cargo, rustc, and rustup
20 let executable_name = executable_name.as_ref();
21 let env_var = executable_name.to_ascii_uppercase();
22 if let Ok(path) = env::var(&env_var) {
23 if is_valid_executable(&path) {
24 Ok(path)
25 } else {
26 Err(Error::msg(format!("`{}` environment variable points to something that's not a valid executable", env_var)))
27 }
28 } else {
29 let final_path: Option<String> = if is_valid_executable(executable_name) {
30 Some(executable_name.to_owned())
31 } else {
32 if let Some(mut path) = dirs::home_dir() {
33 path.push(".cargo");
34 path.push("bin");
35 path.push(executable_name);
36 if is_valid_executable(&path) {
37 Some(path.into_os_string().into_string().expect("Invalid Unicode in path"))
38 } else {
39 None
40 }
41 } else {
42 None
43 }
44 };
45 final_path.ok_or(
46 // This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
47 // for VSCode, even if they are set correctly in a terminal.
48 // On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
49 // to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
50 // but launching VSCode from Dock does not inherit environment variables from a terminal.
51 // For more discussion, see #3118.
52 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))
53 )
54 }
55}
56
57/// Does the given `Path` point to a usable executable?
58///
59/// (assumes the executable takes a `--version` switch and writes to stdout,
60/// which is true for `cargo`, `rustc`, and `rustup`)
61fn is_valid_executable(p: impl AsRef<Path>) -> bool {
62 Command::new(p.as_ref()).arg("--version").output().is_ok()
63}
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 5028b6b6d..e4b86f1e2 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -1,7 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3mod cargo_workspace; 3mod cargo_workspace;
4mod find_executables;
5mod json_project; 4mod json_project;
6mod sysroot; 5mod sysroot;
7 6
@@ -15,6 +14,7 @@ use std::{
15use anyhow::{bail, Context, Result}; 14use anyhow::{bail, Context, Result};
16use ra_cfg::CfgOptions; 15use ra_cfg::CfgOptions;
17use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; 16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
17use ra_env::get_path_for_executable;
18use rustc_hash::FxHashMap; 18use rustc_hash::FxHashMap;
19use serde_json::from_reader; 19use serde_json::from_reader;
20 20
@@ -559,7 +559,7 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
559 559
560 match (|| -> Result<String> { 560 match (|| -> Result<String> {
561 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. 561 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
562 let mut cmd = Command::new("rustc"); 562 let mut cmd = Command::new(get_path_for_executable("rustc")?);
563 cmd.args(&["--print", "cfg", "-O"]); 563 cmd.args(&["--print", "cfg", "-O"]);
564 if let Some(target) = target { 564 if let Some(target) = target {
565 cmd.args(&["--target", target.as_str()]); 565 cmd.args(&["--target", target.as_str()]);
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 8d68032b2..516e0472d 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,6 +1,5 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use super::find_executables::get_path_for_executable;
4use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
5use std::{ 4use std::{
6 env, ops, 5 env, ops,
@@ -9,6 +8,7 @@ use std::{
9}; 8};
10 9
11use ra_arena::{Arena, Idx}; 10use ra_arena::{Arena, Idx};
11use ra_env::get_path_for_executable;
12 12
13#[derive(Default, Debug, Clone)] 13#[derive(Default, Debug, Clone)]
14pub struct Sysroot { 14pub struct Sysroot {
@@ -122,7 +122,8 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
122 let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); 122 let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
123 123
124 if !src_path.exists() { 124 if !src_path.exists() {
125 run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?; 125 let rustup = get_path_for_executable("rustup")?;
126 run_command_in_cargo_dir(cargo_toml, &rustup, &["component", "add", "rust-src"])?;
126 } 127 }
127 if !src_path.exists() { 128 if !src_path.exists() {
128 bail!( 129 bail!(