1 //! The main loop of `ra_lsp_server` responsible for dispatching LSP requests/replies and
2 //! notifications back to the client.
6 pub(crate) mod pending_requests;
8 use std::{error::Error, fmt, panic, path::PathBuf, sync::Arc, time::Instant};
10 use crossbeam_channel::{select, unbounded, RecvError, Sender};
11 use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response};
12 use lsp_types::{ClientCapabilities, NumberOrString};
13 use ra_ide::{Canceled, FeatureFlags, FileId, LibraryData, SourceRootId};
15 use ra_vfs::{VfsTask, Watch};
16 use relative_path::RelativePathBuf;
17 use rustc_hash::FxHashSet;
18 use serde::{de::DeserializeOwned, Serialize};
19 use threadpool::ThreadPool;
22 cargo_check::CheckTask,
24 pending_requests::{PendingRequest, PendingRequests},
25 subscriptions::Subscriptions,
28 world::{Options, WorldSnapshot, WorldState},
32 const THREADPOOL_SIZE: usize = 8;
33 const MAX_IN_FLIGHT_LIBS: usize = THREADPOOL_SIZE - 3;
42 pub fn new(code: i32, message: String) -> LspError {
43 LspError { code, message }
47 impl fmt::Display for LspError {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
53 impl Error for LspError {}
56 ws_roots: Vec<PathBuf>,
57 client_caps: ClientCapabilities,
59 connection: Connection,
61 log::info!("server_config: {:#?}", config);
63 let mut loop_state = LoopState::default();
64 let mut world_state = {
65 // FIXME: support dynamic workspace loading.
67 let mut loaded_workspaces = Vec::new();
68 for ws_root in &ws_roots {
69 let workspace = ra_project_model::ProjectWorkspace::discover_with_sysroot(
72 &config.cargo_features,
75 Ok(workspace) => loaded_workspaces.push(workspace),
77 log::error!("loading workspace failed: {}", e);
80 req::MessageType::Error,
81 format!("rust-analyzer failed to load workspace: {}", e),
93 .map(|glob| ra_vfs_glob::Glob::new(glob))
94 .collect::<std::result::Result<Vec<_>, _>>()?;
96 if config.use_client_watching {
97 let registration_options = req::DidChangeWatchedFilesRegistrationOptions {
100 .flat_map(|ws| ws.to_roots())
101 .filter(|root| root.is_member())
102 .map(|root| format!("{}/**/*.rs", root.path().display()))
103 .map(|glob_pattern| req::FileSystemWatcher { glob_pattern, kind: None })
106 let registration = req::Registration {
107 id: "file-watcher".to_string(),
108 method: "workspace/didChangeWatchedFiles".to_string(),
109 register_options: Some(serde_json::to_value(registration_options).unwrap()),
111 let params = req::RegistrationParams { registrations: vec![registration] };
113 request_new::<req::RegisterCapability>(loop_state.next_request_id(), params);
114 connection.sender.send(request.into()).unwrap();
118 let text_document_caps = client_caps.text_document.as_ref();
120 publish_decorations: config.publish_decorations,
121 supports_location_link: text_document_caps
122 .and_then(|it| it.definition)
123 .and_then(|it| it.link_support)
125 line_folding_only: text_document_caps
126 .and_then(|it| it.folding_range.as_ref())
127 .and_then(|it| it.line_folding_only)
129 max_inlay_hint_length: config.max_inlay_hint_length,
130 cargo_check_enable: config.cargo_check_enable,
131 cargo_check_command: config.cargo_check_command,
132 cargo_check_args: config.cargo_check_args,
136 let feature_flags = {
137 let mut ff = FeatureFlags::default();
138 for (flag, value) in config.feature_flags {
139 if ff.set(flag.as_str(), value).is_err() {
140 log::error!("unknown feature flag: {:?}", flag);
142 req::MessageType::Error,
143 format!("unknown feature flag: {:?}", flag),
150 log::info!("feature_flags: {:#?}", feature_flags);
157 Watch(!config.use_client_watching),
163 let pool = ThreadPool::new(THREADPOOL_SIZE);
164 let (task_sender, task_receiver) = unbounded::<Task>();
165 let (libdata_sender, libdata_receiver) = unbounded::<LibraryData>();
167 log::info!("server initialized, serving requests");
169 let task_sender = task_sender;
170 let libdata_sender = libdata_sender;
172 log::trace!("selecting");
173 let event = select! {
174 recv(&connection.receiver) -> msg => match msg {
175 Ok(msg) => Event::Msg(msg),
176 Err(RecvError) => Err("client exited without shutdown")?,
178 recv(task_receiver) -> task => Event::Task(task.unwrap()),
179 recv(world_state.task_receiver) -> task => match task {
180 Ok(task) => Event::Vfs(task),
181 Err(RecvError) => Err("vfs died")?,
183 recv(libdata_receiver) -> data => Event::Lib(data.unwrap()),
184 recv(world_state.check_watcher.task_recv) -> task => Event::CheckWatcher(task.unwrap())
186 if let Event::Msg(Message::Request(req)) = &event {
187 if connection.handle_shutdown(&req)? {
203 log::info!("waiting for tasks to finish...");
204 task_receiver.into_iter().for_each(|task| {
205 on_task(task, &connection.sender, &mut loop_state.pending_requests, &mut world_state)
207 libdata_receiver.into_iter().for_each(drop);
208 log::info!("...tasks have finished");
209 log::info!("joining threadpool...");
211 log::info!("...threadpool has finished");
213 let vfs = Arc::try_unwrap(world_state.vfs).expect("all snapshots should be dead");
222 Notify(Notification),
230 CheckWatcher(CheckTask),
233 impl fmt::Debug for Event {
234 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
235 let debug_verbose_not = |not: &Notification, f: &mut fmt::Formatter| {
236 f.debug_struct("Notification").field("method", ¬.method).finish()
240 Event::Msg(Message::Notification(not)) => {
241 if notification_is::<req::DidOpenTextDocument>(not)
242 || notification_is::<req::DidChangeTextDocument>(not)
244 return debug_verbose_not(not, f);
247 Event::Task(Task::Notify(not)) => {
248 if notification_is::<req::PublishDecorations>(not)
249 || notification_is::<req::PublishDiagnostics>(not)
251 return debug_verbose_not(not, f);
254 Event::Task(Task::Respond(resp)) => {
256 .debug_struct("Response")
257 .field("id", &resp.id)
258 .field("error", &resp.error)
264 Event::Msg(it) => fmt::Debug::fmt(it, f),
265 Event::Task(it) => fmt::Debug::fmt(it, f),
266 Event::Vfs(it) => fmt::Debug::fmt(it, f),
267 Event::Lib(it) => fmt::Debug::fmt(it, f),
268 Event::CheckWatcher(it) => fmt::Debug::fmt(it, f),
273 #[derive(Debug, Default)]
275 next_request_id: u64,
276 pending_responses: FxHashSet<RequestId>,
277 pending_requests: PendingRequests,
278 subscriptions: Subscriptions,
279 // We try not to index more than MAX_IN_FLIGHT_LIBS libraries at the same
280 // time to always have a thread ready to react to input.
281 in_flight_libraries: usize,
282 pending_libraries: Vec<(SourceRootId, Vec<(FileId, RelativePathBuf, Arc<String>)>)>,
283 workspace_loaded: bool,
287 fn next_request_id(&mut self) -> RequestId {
288 self.next_request_id += 1;
289 let res: RequestId = self.next_request_id.into();
290 let inserted = self.pending_responses.insert(res.clone());
298 task_sender: &Sender<Task>,
299 libdata_sender: &Sender<LibraryData>,
300 connection: &Connection,
301 world_state: &mut WorldState,
302 loop_state: &mut LoopState,
305 let loop_start = Instant::now();
307 // NOTE: don't count blocking select! call as a loop-turn time
308 let _p = profile("main_loop_inner/loop-turn");
309 log::info!("loop turn = {:?}", event);
310 let queue_count = pool.queued_count();
312 log::info!("queued count = {}", queue_count);
316 Event::Task(task) => {
317 on_task(task, &connection.sender, &mut loop_state.pending_requests, world_state);
318 world_state.maybe_collect_garbage();
320 Event::Vfs(task) => {
321 world_state.vfs.write().handle_task(task);
324 world_state.add_lib(lib);
325 world_state.maybe_collect_garbage();
326 loop_state.in_flight_libraries -= 1;
328 Event::CheckWatcher(task) => match task {
329 CheckTask::Update(uri) => {
330 // We manually send a diagnostic update when the watcher asks
331 // us to, to avoid the issue of having to change the file to
332 // receive updated diagnostics.
333 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
334 if let Some(file_id) = world_state.vfs.read().path2file(&path) {
336 handlers::publish_diagnostics(&world_state.snapshot(), FileId(file_id.0))?;
337 let not = notification_new::<req::PublishDiagnostics>(params);
338 task_sender.send(Task::Notify(not)).unwrap();
341 CheckTask::Status(progress) => {
342 let params = req::ProgressParams {
343 token: req::ProgressToken::String("rustAnalyzer/cargoWatcher".to_string()),
344 value: req::ProgressParamsValue::WorkDone(progress),
346 let not = notification_new::<req::Progress>(params);
347 task_sender.send(Task::Notify(not)).unwrap();
350 Event::Msg(msg) => match msg {
351 Message::Request(req) => on_request(
353 &mut loop_state.pending_requests,
360 Message::Notification(not) => {
364 &mut loop_state.pending_requests,
365 &mut loop_state.subscriptions,
369 Message::Response(resp) => {
370 let removed = loop_state.pending_responses.remove(&resp.id);
372 log::error!("unexpected response: {:?}", resp)
378 let mut state_changed = false;
379 if let Some(changes) = world_state.process_changes() {
380 state_changed = true;
381 loop_state.pending_libraries.extend(changes);
384 while loop_state.in_flight_libraries < MAX_IN_FLIGHT_LIBS
385 && !loop_state.pending_libraries.is_empty()
387 let (root, files) = loop_state.pending_libraries.pop().unwrap();
388 loop_state.in_flight_libraries += 1;
389 let sender = libdata_sender.clone();
390 pool.execute(move || {
391 log::info!("indexing {:?} ... ", root);
392 let _p = profile(&format!("indexed {:?}", root));
393 let data = LibraryData::prepare(root, files);
394 sender.send(data).unwrap();
398 if !loop_state.workspace_loaded
399 && world_state.roots_to_scan == 0
400 && loop_state.pending_libraries.is_empty()
401 && loop_state.in_flight_libraries == 0
403 loop_state.workspace_loaded = true;
404 let n_packages: usize = world_state.workspaces.iter().map(|it| it.n_packages()).sum();
405 if world_state.feature_flags().get("notifications.workspace-loaded") {
406 let msg = format!("workspace loaded, {} rust packages", n_packages);
407 show_message(req::MessageType::Info, msg, &connection.sender);
412 update_file_notifications_on_threadpool(
414 world_state.snapshot(),
415 world_state.options.publish_decorations,
417 loop_state.subscriptions.subscriptions(),
425 msg_sender: &Sender<Message>,
426 pending_requests: &mut PendingRequests,
427 state: &mut WorldState,
430 Task::Respond(response) => {
431 if let Some(completed) = pending_requests.finish(&response.id) {
432 log::info!("handled req#{} in {:?}", completed.id, completed.duration);
433 state.complete_request(completed);
434 msg_sender.send(response.into()).unwrap();
438 msg_sender.send(n.into()).unwrap();
444 world: &mut WorldState,
445 pending_requests: &mut PendingRequests,
447 sender: &Sender<Task>,
448 msg_sender: &Sender<Message>,
449 request_received: Instant,
452 let mut pool_dispatcher = PoolDispatcher {
462 .on_sync::<req::CollectGarbage>(|s, ()| Ok(s.collect_garbage()))?
463 .on_sync::<req::JoinLines>(|s, p| handlers::handle_join_lines(s.snapshot(), p))?
464 .on_sync::<req::OnEnter>(|s, p| handlers::handle_on_enter(s.snapshot(), p))?
465 .on_sync::<req::SelectionRangeRequest>(|s, p| {
466 handlers::handle_selection_range(s.snapshot(), p)
468 .on_sync::<req::FindMatchingBrace>(|s, p| {
469 handlers::handle_find_matching_brace(s.snapshot(), p)
471 .on::<req::AnalyzerStatus>(handlers::handle_analyzer_status)?
472 .on::<req::SyntaxTree>(handlers::handle_syntax_tree)?
473 .on::<req::ExpandMacro>(handlers::handle_expand_macro)?
474 .on::<req::OnTypeFormatting>(handlers::handle_on_type_formatting)?
475 .on::<req::DocumentSymbolRequest>(handlers::handle_document_symbol)?
476 .on::<req::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
477 .on::<req::GotoDefinition>(handlers::handle_goto_definition)?
478 .on::<req::GotoImplementation>(handlers::handle_goto_implementation)?
479 .on::<req::GotoTypeDefinition>(handlers::handle_goto_type_definition)?
480 .on::<req::ParentModule>(handlers::handle_parent_module)?
481 .on::<req::Runnables>(handlers::handle_runnables)?
482 .on::<req::DecorationsRequest>(handlers::handle_decorations)?
483 .on::<req::Completion>(handlers::handle_completion)?
484 .on::<req::CodeActionRequest>(handlers::handle_code_action)?
485 .on::<req::CodeLensRequest>(handlers::handle_code_lens)?
486 .on::<req::CodeLensResolve>(handlers::handle_code_lens_resolve)?
487 .on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
488 .on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
489 .on::<req::HoverRequest>(handlers::handle_hover)?
490 .on::<req::PrepareRenameRequest>(handlers::handle_prepare_rename)?
491 .on::<req::Rename>(handlers::handle_rename)?
492 .on::<req::References>(handlers::handle_references)?
493 .on::<req::Formatting>(handlers::handle_formatting)?
494 .on::<req::DocumentHighlightRequest>(handlers::handle_document_highlight)?
495 .on::<req::InlayHints>(handlers::handle_inlay_hints)?
501 msg_sender: &Sender<Message>,
502 state: &mut WorldState,
503 pending_requests: &mut PendingRequests,
504 subs: &mut Subscriptions,
507 let not = match notification_cast::<req::Cancel>(not) {
509 let id: RequestId = match params.id {
510 NumberOrString::Number(id) => id.into(),
511 NumberOrString::String(id) => id.into(),
513 if pending_requests.cancel(&id) {
514 let response = Response::new_err(
516 ErrorCode::RequestCanceled as i32,
517 "canceled by client".to_string(),
519 msg_sender.send(response.into()).unwrap()
525 let not = match notification_cast::<req::DidOpenTextDocument>(not) {
527 let uri = params.text_document.uri;
528 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
529 if let Some(file_id) =
530 state.vfs.write().add_file_overlay(&path, params.text_document.text)
532 subs.add_sub(FileId(file_id.0));
538 let not = match notification_cast::<req::DidChangeTextDocument>(not) {
540 let uri = params.text_document.uri;
541 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
543 params.content_changes.pop().ok_or_else(|| "empty changes".to_string())?.text;
544 state.vfs.write().change_file_overlay(path.as_path(), text);
549 let not = match notification_cast::<req::DidSaveTextDocument>(not) {
551 state.check_watcher.update();
556 let not = match notification_cast::<req::DidCloseTextDocument>(not) {
558 let uri = params.text_document.uri;
559 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
560 if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) {
561 subs.remove_sub(FileId(file_id.0));
564 req::PublishDiagnosticsParams { uri, diagnostics: Vec::new(), version: None };
565 let not = notification_new::<req::PublishDiagnostics>(params);
566 msg_sender.send(not.into()).unwrap();
571 let not = match notification_cast::<req::DidChangeConfiguration>(not) {
577 let not = match notification_cast::<req::DidChangeWatchedFiles>(not) {
579 let mut vfs = state.vfs.write();
580 for change in params.changes {
581 let uri = change.uri;
582 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
583 vfs.notify_changed(path)
589 log::error!("unhandled notification: {:?}", not);
593 struct PoolDispatcher<'a> {
594 req: Option<Request>,
595 pool: &'a ThreadPool,
596 world: &'a mut WorldState,
597 pending_requests: &'a mut PendingRequests,
598 msg_sender: &'a Sender<Message>,
599 sender: &'a Sender<Task>,
600 request_received: Instant,
603 impl<'a> PoolDispatcher<'a> {
604 /// Dispatches the request onto the current thread
607 f: fn(&mut WorldState, R::Params) -> Result<R::Result>,
608 ) -> Result<&mut Self>
610 R: req::Request + 'static,
611 R::Params: DeserializeOwned + panic::UnwindSafe + 'static,
612 R::Result: Serialize + 'static,
614 let (id, params) = match self.parse::<R>() {
620 let world = panic::AssertUnwindSafe(&mut *self.world);
621 let task = panic::catch_unwind(move || {
622 let result = f(world.0, params);
623 result_to_task::<R>(id, result)
625 .map_err(|_| format!("sync task {:?} panicked", R::METHOD))?;
626 on_task(task, self.msg_sender, self.pending_requests, self.world);
630 /// Dispatches the request onto thread pool
631 fn on<R>(&mut self, f: fn(WorldSnapshot, R::Params) -> Result<R::Result>) -> Result<&mut Self>
633 R: req::Request + 'static,
634 R::Params: DeserializeOwned + Send + 'static,
635 R::Result: Serialize + 'static,
637 let (id, params) = match self.parse::<R>() {
645 let world = self.world.snapshot();
646 let sender = self.sender.clone();
648 let result = f(world, params);
649 let task = result_to_task::<R>(id, result);
650 sender.send(task).unwrap();
657 fn parse<R>(&mut self) -> Option<(RequestId, R::Params)>
659 R: req::Request + 'static,
660 R::Params: DeserializeOwned + 'static,
662 let req = self.req.take()?;
663 let (id, params) = match req.extract::<R::Params>(R::METHOD) {
666 self.req = Some(req);
670 self.pending_requests.start(PendingRequest {
672 method: R::METHOD.to_string(),
673 received: self.request_received,
678 fn finish(&mut self) {
679 match self.req.take() {
682 log::error!("unknown request: {:?}", req);
683 let resp = Response::new_err(
685 ErrorCode::MethodNotFound as i32,
686 "unknown request".to_string(),
688 self.msg_sender.send(resp.into()).unwrap();
694 fn result_to_task<R>(id: RequestId, result: Result<R::Result>) -> Task
696 R: req::Request + 'static,
697 R::Params: DeserializeOwned + 'static,
698 R::Result: Serialize + 'static,
700 let response = match result {
701 Ok(resp) => Response::new_ok(id, &resp),
702 Err(e) => match e.downcast::<LspError>() {
703 Ok(lsp_error) => Response::new_err(id, lsp_error.code, lsp_error.message),
706 // FIXME: When https://github.com/Microsoft/vscode-languageserver-node/issues/457
707 // gets fixed, we can return the proper response.
708 // This works around the issue where "content modified" error would continuously
709 // show an message pop-up in VsCode
712 // ErrorCode::ContentModified as i32,
713 // "content modified".to_string(),
715 Response::new_ok(id, ())
717 Response::new_err(id, ErrorCode::InternalError as i32, e.to_string())
722 Task::Respond(response)
725 fn update_file_notifications_on_threadpool(
727 world: WorldSnapshot,
728 publish_decorations: bool,
729 sender: Sender<Task>,
730 subscriptions: Vec<FileId>,
732 log::trace!("updating notifications for {:?}", subscriptions);
733 let publish_diagnostics = world.feature_flags().get("lsp.diagnostics");
734 pool.execute(move || {
735 for file_id in subscriptions {
736 if publish_diagnostics {
737 match handlers::publish_diagnostics(&world, file_id) {
739 if !is_canceled(&e) {
740 log::error!("failed to compute diagnostics: {:?}", e);
744 let not = notification_new::<req::PublishDiagnostics>(params);
745 sender.send(Task::Notify(not)).unwrap();
749 if publish_decorations {
750 match handlers::publish_decorations(&world, file_id) {
752 if !is_canceled(&e) {
753 log::error!("failed to compute decorations: {:?}", e);
757 let not = notification_new::<req::PublishDecorations>(params);
758 sender.send(Task::Notify(not)).unwrap();
766 pub fn show_message(typ: req::MessageType, message: impl Into<String>, sender: &Sender<Message>) {
767 let message = message.into();
768 let params = req::ShowMessageParams { typ, message };
769 let not = notification_new::<req::ShowMessage>(params);
770 sender.send(not.into()).unwrap();
773 fn is_canceled(e: &Box<dyn std::error::Error + Send + Sync>) -> bool {
774 e.downcast_ref::<Canceled>().is_some()
777 fn notification_is<N: lsp_types::notification::Notification>(notification: &Notification) -> bool {
778 notification.method == N::METHOD
781 fn notification_cast<N>(notification: Notification) -> std::result::Result<N::Params, Notification>
783 N: lsp_types::notification::Notification,
784 N::Params: DeserializeOwned,
786 notification.extract(N::METHOD)
789 fn notification_new<N>(params: N::Params) -> Notification
791 N: lsp_types::notification::Notification,
792 N::Params: Serialize,
794 Notification::new(N::METHOD.to_string(), params)
797 fn request_new<R>(id: RequestId, params: R::Params) -> Request
799 R: lsp_types::request::Request,
800 R::Params: Serialize,
802 Request::new(id, R::METHOD.to_string(), params)