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 <[email protected]>
Цей коміт міститься в:
arookieofc-ai[bot]таarookieofc-ai authored and GitHub committed 2026-12-06 21:43:46 +08:00
1 джерело 3ee1062d91
коміт 8a08f748cf
3 files changed
+58

No files matched your search

+3
Переглянути файл
@@ -697,6 +697,9 @@ impl LspManager {
}
pub(crate) fn java_import_ready(self: &Arc<Self>, server: Arc<Server>, 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 };
+32
Переглянути файл
@@ -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 };
+23
Переглянути файл
@@ -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())], &[]);