diff --git a/crates/oxc_language_server/src/backend.rs b/crates/oxc_language_server/src/backend.rs index 4e904744b9ed4..7d626296205bc 100644 --- a/crates/oxc_language_server/src/backend.rs +++ b/crates/oxc_language_server/src/backend.rs @@ -39,6 +39,10 @@ use crate::{ /// - Editor sends `textDocument/didOpen`, `textDocument/didChange`, `textDocument/didSave`, and `textDocument/didClose` notifications. /// - Editor sends `shutdown` request when the user closes the editor. /// - Editor sends `exit` notification and the server exits. +/// +/// Because `initialized` is a notification, the client will not wait for a response from the server. +/// Therefore, the server must be able to handle requests and notifications that may arrive directly after `initialized` notification, +/// such as `textDocument/didOpen`. pub struct Backend { // The LSP client to communicate with the editor or IDE. client: Client, @@ -191,10 +195,30 @@ impl LanguageServer for Backend { vec![serde_json::Value::Null; needed_configurations.len()] }; + let known_files = self.file_system.read().await.keys(); + let mut new_diagnostics = Vec::new(); + for (index, worker) in needed_configurations.values().enumerate() { + // get the configuration from the response and start the worker let configuration = configurations.get(index).unwrap_or(&serde_json::Value::Null); - worker.start_worker(configuration.clone(), &self.tool_builders).await; + + // run diagnostics for all known files in the workspace of the worker. + // This is necessary because the worker was not started before. + for uri in &known_files { + if !worker.is_responsible_for_uri(uri) { + continue; + } + let content = self.file_system.read().await.get(uri); + if let Some(diagnostics) = worker.run_diagnostic(uri, content.as_deref()).await + { + new_diagnostics.push((uri.to_string(), diagnostics)); + } + } + } + + if !new_diagnostics.is_empty() { + self.publish_all_diagnostics(&new_diagnostics).await; } } diff --git a/crates/oxc_language_server/src/file_system.rs b/crates/oxc_language_server/src/file_system.rs index 252e71645110c..66df02cce693d 100644 --- a/crates/oxc_language_server/src/file_system.rs +++ b/crates/oxc_language_server/src/file_system.rs @@ -23,4 +23,8 @@ impl LSPFileSystem { pub fn remove(&self, uri: &Uri) { self.files.pin().remove(uri); } + + pub fn keys(&self) -> Vec { + self.files.pin().keys().cloned().collect() + } }