Skip to content

Commit e593da1

Browse files
committed
Don't respond to LSP requests before startup.
A common error found while using emacs + lsp-mode + rust-analyzer is that on startup I receive the following logs: ``` LSP :: Error from the Language Server: waiting for cargo metadata or cargo check (Unknown error) [4 times] ``` This looks similar to rust-lang#10910, and as some people mention, everything works well once `cargo metadata` finishes running. I stumbled accross a [helpful comment](https://github.com/rust-lang/rust-analyzer/blob/d382e24a11c8706b201c8437894506d191691334/crates/rust-analyzer/src/main_loop.rs#L567-L568) which made me think that it might be worth waiting for startup to finish, before attempting to respond to LSP requests.
1 parent 50ed1a5 commit e593da1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

crates/rust-analyzer/src/main_loop.rs

+23
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ impl GlobalState {
154154
self.fetch_workspaces(cause);
155155
}
156156

157+
// handle our startup tasks, such as `cargo metadata`, before
158+
// responding to LSP requests
159+
while !self.is_quiescent() {
160+
if let Some(event) = self.next_non_lsp_event() {
161+
self.handle_event(event)?
162+
} else {
163+
break;
164+
}
165+
}
166+
157167
while let Some(event) = self.next_event(&inbox) {
158168
if let Event::Lsp(lsp_server::Message::Notification(not)) = &event {
159169
if not.method == lsp_types::notification::Exit::METHOD {
@@ -166,6 +176,19 @@ impl GlobalState {
166176
Err("client exited without proper shutdown sequence".into())
167177
}
168178

179+
fn next_non_lsp_event(&self) -> Option<Event> {
180+
select! {
181+
recv(self.task_pool.receiver) -> task =>
182+
Some(Event::Task(task.unwrap())),
183+
184+
recv(self.loader.receiver) -> task =>
185+
Some(Event::Vfs(task.unwrap())),
186+
187+
recv(self.flycheck_receiver) -> task =>
188+
Some(Event::Flycheck(task.unwrap())),
189+
}
190+
}
191+
169192
fn next_event(&self, inbox: &Receiver<lsp_server::Message>) -> Option<Event> {
170193
select! {
171194
recv(inbox) -> msg =>

0 commit comments

Comments
 (0)