From 8a08f748cf42356ef73be1e23acb4606b3d44f25 Mon Sep 17 00:00:00 2001 From: "arookieofc-ai[bot]" <2128194521hzz@gmail.com> Date: Sun, 6 Dec 2026 21:43:46 +0800 Subject: [PATCH] fix(lsp): reopen Java files on jdtls once it imported the project (#8357) A Java file opened before jdtls finished importing the project resolved to the fallback standalone project and was never rebound: its didChange notifications were silently dropped, so the server kept the opened text and the file's diagnostics froze. When jdtls reports ServiceReady, each open Java file of that server instance is now reopened (didClose/didOpen with the current text and version) so it resolves against the imported project. Co-authored-by: arookieofc-ai <282213208+arookieofc-ai@users.noreply.github.com> --- crates/ide-lsp/src/jdtls.rs | 3 ++ crates/ide-lsp/src/lib.rs | 32 +++++++++++++++++++ .../ide-lsp/tests/integration/mock_server.rs | 23 +++++++++++++ 3 files changed, 58 insertions(+) diff --git a/crates/ide-lsp/src/jdtls.rs b/crates/ide-lsp/src/jdtls.rs index 94b0d5eb09..7806b44e21 100644 --- a/crates/ide-lsp/src/jdtls.rs +++ b/crates/ide-lsp/src/jdtls.rs @@ -697,6 +697,9 @@ impl LspManager { } pub(crate) fn java_import_ready(self: &Arc, server: Arc, generation: u64) { + // Files opened before this import never rebind to the imported project on the + // server side; reopen them so their changes and diagnostics flow again. + self.rebind_java_documents(&server, generation); let mgr = self.clone(); self.runtime.spawn(async move { let Some(run) = server.running_gen(generation) else { return }; diff --git a/crates/ide-lsp/src/lib.rs b/crates/ide-lsp/src/lib.rs index 8c32586f20..6e08bb0577 100644 --- a/crates/ide-lsp/src/lib.rs +++ b/crates/ide-lsp/src/lib.rs @@ -1676,6 +1676,38 @@ impl LspManager { } } + /// Reopens the Java files open on `server`'s instance `generation` (a `didClose`/`didOpen` + /// pair each) once jdtls has imported the project. A file opened before the import resolves + /// to the fallback standalone project, and jdtls never rebinds it to the real one: its later + /// `didChange`s silently drop (the re-resolved compilation unit is no working copy), so the + /// server's buffer keeps the text it was opened with and only workspace builds — nothing + /// triggers one for an open file — refresh its diagnostics. Reopening resolves the file + /// against the imported project; the document itself and its versions are unaffected. + pub(crate) fn rebind_java_documents(&self, server: &Server, generation: u64) { + let Some(run) = server.running_gen(generation) else { return }; + if run.caps.read().sync == SyncKind::None { + return; + } + let docs = self.docs.lock(); + for d in docs.values().filter(|d| d.notebook.is_none() && d.language.as_str() == "java" && d.path.is_absolute()) + { + if !d.servers.iter().any(|a| a.server.id == server.id && a.opened_gen == Some(generation)) { + continue; + } + if !run.opens_document(&d.language, &d.path) { + continue; + } + if let Err(e) = run.peer.notify("textDocument/didClose", json!({ "textDocument": { "uri": d.uri } })) { + tracing::debug!("{}: didClose: {e}", server.name); + continue; + } + let language_id = self.languages.server_language_id(&d.language, &server.name); + if let Err(e) = run.peer.notify("textDocument/didOpen", d.did_open_params(&language_id)) { + tracing::debug!("{}: didOpen: {e}", server.name); + } + } + } + pub fn did_save(&self, doc: DocId, text: &str) { let mut docs = self.docs.lock(); let Some(d) = docs.get_mut(&doc) else { return }; diff --git a/crates/ide-lsp/tests/integration/mock_server.rs b/crates/ide-lsp/tests/integration/mock_server.rs index ac62cb06c5..152a29ed3b 100644 --- a/crates/ide-lsp/tests/integration/mock_server.rs +++ b/crates/ide-lsp/tests/integration/mock_server.rs @@ -511,6 +511,29 @@ async fn java_sources_changed_on_disk_send_the_open_files_to_jdtls_again() { h.mgr.shutdown_all().await; } +// jdtls never rebinds a file opened before the Maven import to the imported project: its +// changes drop and its diagnostics go stale. ServiceReady reopens the open Java files. +#[tokio::test(flavor = "multi_thread")] +async fn java_files_open_before_the_import_are_reopened_when_it_finishes() { + let h = harness_for("java", "java", &[("jdtls", incremental_caps())], &[]); + let (_, main) = h.open("Main.java", "class Main { String s = Helper.greet(); }"); + h.wait_status("jdtls", ServerStatus::Ready).await; + let s = h.launcher.state("jdtls"); + let closed = + || s.notified("textDocument/didClose").iter().filter(|v| v["textDocument"]["uri"] == uri_of(&main)).count(); + let opened = + || s.notified("textDocument/didOpen").iter().filter(|v| v["textDocument"]["uri"] == uri_of(&main)).count(); + assert_eq!(closed(), 0); + assert_eq!(opened(), 1); + s.peer().notify("language/status", json!({"type": "ServiceReady", "message": "ServiceReady"})).unwrap(); + eventually(|| closed() == 1 && opened() == 2).await; + // The reopen carries the buffer's current text and version. + let reopen = + s.notified("textDocument/didOpen").iter().rfind(|v| v["textDocument"]["uri"] == uri_of(&main)).unwrap().clone(); + assert_eq!(reopen["textDocument"]["text"], "class Main { String s = Helper.greet(); }"); + h.mgr.shutdown_all().await; +} + #[tokio::test(flavor = "multi_thread")] async fn generated_maven_sources_reach_a_java_server_that_does_not_import_them() { let h = harness_for("java", "java", &[("java-analyzer", incremental_caps())], &[]);