From 4e735eadcb941e0e4f949f6d71cf691497d6c0b6 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 1 Dec 2026 07:03:06 +0800 Subject: [PATCH] fix(java): recheck open files when Java sources change on disk (#7121) When another tool rewrote an open Java file to call a class or method it added in another file, the open file kept a "cannot be resolved" error until it was reopened. jdtls rebuilds sources reported through workspace/didChangeWatchedFiles but does not re-reconcile the open working copies it already checked, so when the open file's reload reached it before the new class, the stale error stayed. After sending Java source changes to jdtls, the open documents are now sent again (resync_documents), so jdtls checks them against the new sources. Fixes #6986 --- crates/ide-lsp/src/lib.rs | 7 +++++++ crates/ide-lsp/tests/mock_server.rs | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/ide-lsp/src/lib.rs b/crates/ide-lsp/src/lib.rs index ac1fd0b437..a5962ef6f6 100644 --- a/crates/ide-lsp/src/lib.rs +++ b/crates/ide-lsp/src/lib.rs @@ -3405,10 +3405,17 @@ impl LspManager { if changes.is_empty() { continue; } + // jdtls rebuilds a source file that changed on disk but keeps the diagnostics of the + // open files it checked before: a reference to a class another tool adds after + // rewriting the open file stays "cannot be resolved" until the file is reopened. + // Sending the open files again makes it check them against the new sources. + let recheck_open = jdtls && changes.iter().any(|(p, _)| p.extension().is_some_and(|e| e == "java")); let changes: Vec = changes.iter().map(|(p, c)| json!({ "uri": uri::path_to_uri(p), "type": *c as u8 })).collect(); if let Err(e) = run.peer.notify("workspace/didChangeWatchedFiles", json!({ "changes": changes })) { tracing::debug!("{}: didChangeWatchedFiles: {e}", s.name); + } else if recheck_open { + self.resync_documents(&s, run.generation); } } } diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rs index 8d17ae1896..6f17730fd5 100644 --- a/crates/ide-lsp/tests/mock_server.rs +++ b/crates/ide-lsp/tests/mock_server.rs @@ -482,6 +482,31 @@ async fn dart_file_watching_requires_an_explicit_registration() { } } +#[tokio::test(flavor = "multi_thread")] +async fn java_sources_changed_on_disk_send_the_open_files_to_jdtls_again() { + 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 resent = || { + s.notified("textDocument/didChange") + .iter() + .filter(|v| v["textDocument"]["uri"] == uri_of(&main) && v["contentChanges"][0]["text"].is_string()) + .count() + }; + // Another tool adds the class the open file refers to. + let helper = h.root.join("Helper.java"); + h.mgr.watched_files_changed(std::slice::from_ref(&helper), &[], &[], &[]); + eventually(|| s.notified("workspace/didChangeWatchedFiles").len() == 1).await; + eventually(|| resent() == 1).await; + // Files that are not Java sources leave the open files alone. + h.mgr.watched_files_changed(&[], &[h.root.join("notes.txt")], &[], &[]); + h.mgr.watched_files_changed(&[], &[h.root.join("pom.xml")], &[], &[]); + tokio::time::sleep(Duration::from_millis(200)).await; + assert_eq!(resent(), 1); + h.mgr.shutdown_all().await; +} + // ------------------------------------------------------------------ tests #[tokio::test(flavor = "multi_thread")]