aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/sysroot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src/sysroot.rs')
-rw-r--r--crates/ra_project_model/src/sysroot.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 10ca391b6..24c3f47a2 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -8,7 +8,7 @@ use std::{
8 8
9use ra_arena::{impl_arena_id, Arena, RawId}; 9use ra_arena::{impl_arena_id, Arena, RawId};
10 10
11use crate::Result; 11use crate::WorkspaceError;
12 12
13#[derive(Default, Debug, Clone)] 13#[derive(Default, Debug, Clone)]
14pub struct Sysroot { 14pub struct Sysroot {
@@ -47,16 +47,11 @@ impl Sysroot {
47 self.crates.iter().map(|(id, _data)| id) 47 self.crates.iter().map(|(id, _data)| id)
48 } 48 }
49 49
50 pub fn discover(cargo_toml: &Path) -> Result<Sysroot> { 50 pub fn discover(cargo_toml: &Path) -> Result<Sysroot, WorkspaceError> {
51 let src = try_find_src_path(cargo_toml)?; 51 let src = try_find_src_path(cargo_toml)?;
52 52
53 if !src.exists() { 53 if !src.exists() {
54 Err(format!( 54 return Err(WorkspaceError::NoStdLib(src));
55 "can't load standard library from sysroot\n\
56 {:?}\n\
57 try running `rustup component add rust-src` or set `RUST_SRC_PATH`",
58 src,
59 ))?;
60 } 55 }
61 56
62 let mut sysroot = Sysroot { crates: Arena::default() }; 57 let mut sysroot = Sysroot { crates: Arena::default() };
@@ -90,7 +85,7 @@ impl Sysroot {
90 } 85 }
91} 86}
92 87
93fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> { 88fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf, WorkspaceError> {
94 if let Ok(path) = env::var("RUST_SRC_PATH") { 89 if let Ok(path) = env::var("RUST_SRC_PATH") {
95 return Ok(path.into()); 90 return Ok(path.into());
96 } 91 }
@@ -98,11 +93,13 @@ fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> {
98 let rustc_output = Command::new("rustc") 93 let rustc_output = Command::new("rustc")
99 .current_dir(cargo_toml.parent().unwrap()) 94 .current_dir(cargo_toml.parent().unwrap())
100 .args(&["--print", "sysroot"]) 95 .args(&["--print", "sysroot"])
101 .output()?; 96 .output()
97 .map_err(|err| WorkspaceError::RustcError(err))?;
102 if !rustc_output.status.success() { 98 if !rustc_output.status.success() {
103 Err("failed to locate sysroot")?; 99 Err(WorkspaceError::SysrootNotFound)?;
104 } 100 }
105 let stdout = String::from_utf8(rustc_output.stdout)?; 101 let stdout = String::from_utf8(rustc_output.stdout)
102 .map_err(|err| WorkspaceError::RustcOutputError(err))?;
106 let sysroot_path = Path::new(stdout.trim()); 103 let sysroot_path = Path::new(stdout.trim());
107 Ok(sysroot_path.join("lib/rustlib/src/rust/src")) 104 Ok(sysroot_path.join("lib/rustlib/src/rust/src"))
108} 105}