Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b093423

Browse files
committed
Auto merge of rust-lang#14556 - Veykril:sysroot-no-core-warn, r=Veykril
internal: Warn when loading sysroot fails to find the core library Should help a bit more with user experience, before we only logged this now we show it in the status Closes rust-lang/rust-analyzer#11606
2 parents 1ee88db + dd5c3c3 commit b093423

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

crates/project-model/src/sysroot.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ impl Sysroot {
7474
pub fn is_empty(&self) -> bool {
7575
self.crates.is_empty()
7676
}
77+
78+
pub fn loading_warning(&self) -> Option<String> {
79+
if self.by_name("core").is_none() {
80+
let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
81+
" (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
82+
} else {
83+
" try running `rustup component add rust-src` to possible fix this"
84+
};
85+
Some(format!(
86+
"could not find libcore in loaded sysroot at `{}`{}",
87+
self.src_root.as_path().display(),
88+
var_note,
89+
))
90+
} else {
91+
None
92+
}
93+
}
7794
}
7895

7996
// FIXME: Expose a builder api as loading the sysroot got way too modular and complicated.
@@ -103,7 +120,7 @@ impl Sysroot {
103120

104121
pub fn with_sysroot_dir(sysroot_dir: AbsPathBuf) -> Result<Sysroot> {
105122
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir).ok_or_else(|| {
106-
format_err!("can't load standard library from sysroot {}", sysroot_dir.display())
123+
format_err!("can't load standard library from sysroot path {}", sysroot_dir.display())
107124
})?;
108125
Ok(Sysroot::load(sysroot_dir, sysroot_src_dir))
109126
}
@@ -153,19 +170,6 @@ impl Sysroot {
153170
}
154171
}
155172

156-
if sysroot.by_name("core").is_none() {
157-
let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
158-
" (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
159-
} else {
160-
""
161-
};
162-
tracing::error!(
163-
"could not find libcore in sysroot path `{}`{}",
164-
sysroot.src_root.as_path().display(),
165-
var_note,
166-
);
167-
}
168-
169173
sysroot
170174
}
171175

crates/rust-analyzer/src/reload.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,20 @@ impl GlobalState {
138138
let (ProjectWorkspace::Cargo { sysroot, .. }
139139
| ProjectWorkspace::Json { sysroot, .. }
140140
| ProjectWorkspace::DetachedFiles { sysroot, .. }) = ws;
141-
if let Err(Some(e)) = sysroot {
142-
status.health = lsp_ext::Health::Warning;
143-
message.push_str(e);
144-
message.push_str("\n\n");
141+
match sysroot {
142+
Err(None) => (),
143+
Err(Some(e)) => {
144+
status.health = lsp_ext::Health::Warning;
145+
message.push_str(e);
146+
message.push_str("\n\n");
147+
}
148+
Ok(s) => {
149+
if let Some(e) = s.loading_warning() {
150+
status.health = lsp_ext::Health::Warning;
151+
message.push_str(&e);
152+
message.push_str("\n\n");
153+
}
154+
}
145155
}
146156
if let ProjectWorkspace::Cargo { rustc: Err(Some(e)), .. } = ws {
147157
status.health = lsp_ext::Health::Warning;

0 commit comments

Comments
 (0)