about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-04-13 08:40:14 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-04-13 08:40:14 +0200
commitdd5c3c30b6379a92d69e964327cf97963a6d6143 (patch)
tree9a280be2b752aeb1817d6ff59ce7941c671a8b95
parent7501d3b721560637e27f904d9fce79182c41bef7 (diff)
downloadrust-dd5c3c30b6379a92d69e964327cf97963a6d6143.tar.gz
rust-dd5c3c30b6379a92d69e964327cf97963a6d6143.zip
internal: Warn when loading sysroot fails to find the core library
-rw-r--r--crates/project-model/src/sysroot.rs32
-rw-r--r--crates/rust-analyzer/src/reload.rs18
2 files changed, 32 insertions, 18 deletions
diff --git a/crates/project-model/src/sysroot.rs b/crates/project-model/src/sysroot.rs
index 74e41eda763..6c468f5ee66 100644
--- a/crates/project-model/src/sysroot.rs
+++ b/crates/project-model/src/sysroot.rs
@@ -74,6 +74,23 @@ impl Sysroot {
     pub fn is_empty(&self) -> bool {
         self.crates.is_empty()
     }
+
+    pub fn loading_warning(&self) -> Option<String> {
+        if self.by_name("core").is_none() {
+            let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
+                " (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
+            } else {
+                " try running `rustup component add rust-src` to possible fix this"
+            };
+            Some(format!(
+                "could not find libcore in loaded sysroot at `{}`{}",
+                self.src_root.as_path().display(),
+                var_note,
+            ))
+        } else {
+            None
+        }
+    }
 }
 
 // FIXME: Expose a builder api as loading the sysroot got way too modular and complicated.
@@ -103,7 +120,7 @@ impl Sysroot {
 
     pub fn with_sysroot_dir(sysroot_dir: AbsPathBuf) -> Result<Sysroot> {
         let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir).ok_or_else(|| {
-            format_err!("can't load standard library from sysroot {}", sysroot_dir.display())
+            format_err!("can't load standard library from sysroot path {}", sysroot_dir.display())
         })?;
         Ok(Sysroot::load(sysroot_dir, sysroot_src_dir))
     }
@@ -153,19 +170,6 @@ impl Sysroot {
             }
         }
 
-        if sysroot.by_name("core").is_none() {
-            let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
-                " (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
-            } else {
-                ""
-            };
-            tracing::error!(
-                "could not find libcore in sysroot path `{}`{}",
-                sysroot.src_root.as_path().display(),
-                var_note,
-            );
-        }
-
         sysroot
     }
 
diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs
index ddf130e08dd..00826754c3f 100644
--- a/crates/rust-analyzer/src/reload.rs
+++ b/crates/rust-analyzer/src/reload.rs
@@ -138,10 +138,20 @@ impl GlobalState {
             let (ProjectWorkspace::Cargo { sysroot, .. }
             | ProjectWorkspace::Json { sysroot, .. }
             | ProjectWorkspace::DetachedFiles { sysroot, .. }) = ws;
-            if let Err(Some(e)) = sysroot {
-                status.health = lsp_ext::Health::Warning;
-                message.push_str(e);
-                message.push_str("\n\n");
+            match sysroot {
+                Err(None) => (),
+                Err(Some(e)) => {
+                    status.health = lsp_ext::Health::Warning;
+                    message.push_str(e);
+                    message.push_str("\n\n");
+                }
+                Ok(s) => {
+                    if let Some(e) = s.loading_warning() {
+                        status.health = lsp_ext::Health::Warning;
+                        message.push_str(&e);
+                        message.push_str("\n\n");
+                    }
+                }
             }
             if let ProjectWorkspace::Cargo { rustc: Err(Some(e)), .. } = ws {
                 status.health = lsp_ext::Health::Warning;