aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src
diff options
context:
space:
mode:
authorBastian Köcher <[email protected]>2019-08-22 20:08:34 +0100
committerBastian Köcher <[email protected]>2019-08-22 20:59:23 +0100
commit18c7a1ebe7ced9d8039132da06dcd954affec2eb (patch)
treea854609466bb15d923bc117b7d5204c91cad66c4 /crates/ra_project_model/src
parent08e5d394dfbca28b15ed5dc772d55d48f87c3f54 (diff)
Make sysroot use `RUST_SRC_PATH` if set
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r--crates/ra_project_model/src/sysroot.rs32
1 files changed, 21 insertions, 11 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 2a7927f7e..0c27d4f4b 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,4 +1,5 @@
1use std::{ 1use std::{
2 env,
2 path::{Path, PathBuf}, 3 path::{Path, PathBuf},
3 process::Command, 4 process::Command,
4}; 5};
@@ -33,21 +34,13 @@ impl Sysroot {
33 } 34 }
34 35
35 pub fn discover(cargo_toml: &Path) -> Result<Sysroot> { 36 pub fn discover(cargo_toml: &Path) -> Result<Sysroot> {
36 let rustc_output = Command::new("rustc") 37 let src = try_find_src_path(cargo_toml)?;
37 .current_dir(cargo_toml.parent().unwrap()) 38
38 .args(&["--print", "sysroot"])
39 .output()?;
40 if !rustc_output.status.success() {
41 Err("failed to locate sysroot")?
42 }
43 let stdout = String::from_utf8(rustc_output.stdout)?;
44 let sysroot_path = Path::new(stdout.trim());
45 let src = sysroot_path.join("lib/rustlib/src/rust/src");
46 if !src.exists() { 39 if !src.exists() {
47 Err(format!( 40 Err(format!(
48 "can't load standard library from sysroot\n\ 41 "can't load standard library from sysroot\n\
49 {:?}\n\ 42 {:?}\n\
50 try running `rustup component add rust-src`", 43 try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
51 src, 44 src,
52 ))?; 45 ))?;
53 } 46 }
@@ -83,6 +76,23 @@ impl Sysroot {
83 } 76 }
84} 77}
85 78
79fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> {
80 if let Ok(path) = env::var("RUST_SRC_PATH") {
81 return Ok(path.into());
82 }
83
84 let rustc_output = Command::new("rustc")
85 .current_dir(cargo_toml.parent().unwrap())
86 .args(&["--print", "sysroot"])
87 .output()?;
88 if !rustc_output.status.success() {
89 Err("failed to locate sysroot")?;
90 }
91 let stdout = String::from_utf8(rustc_output.stdout)?;
92 let sysroot_path = Path::new(stdout.trim());
93 Ok(sysroot_path.join("lib/rustlib/src/rust/src"))
94}
95
86impl SysrootCrate { 96impl SysrootCrate {
87 pub fn name(self, sysroot: &Sysroot) -> &str { 97 pub fn name(self, sysroot: &Sysroot) -> &str {
88 &sysroot.crates[self].name 98 &sysroot.crates[self].name