diff --git a/crates/ide-lsp/src/jdtls.rs b/crates/ide-lsp/src/jdtls.rs index fc72b5a0b9..a7294c18e3 100644 --- a/crates/ide-lsp/src/jdtls.rs +++ b/crates/ide-lsp/src/jdtls.rs @@ -23,6 +23,7 @@ pub(crate) mod configuration; mod extract_interface; mod formatter; mod generated; +pub(crate) use generated::is_processor_output; mod hierarchy; pub(crate) mod jvm_inspect; mod move_refactoring; diff --git a/crates/ide-lsp/src/jdtls/generated.rs b/crates/ide-lsp/src/jdtls/generated.rs index 81b5f1d5ee..a02f9f1f58 100644 --- a/crates/ide-lsp/src/jdtls/generated.rs +++ b/crates/ide-lsp/src/jdtls/generated.rs @@ -25,6 +25,18 @@ pub(crate) struct GeneratedScan { pub roots: Vec, } +/// Whether `path` is in (or is) an annotation-processor output folder: +/// `generated-sources/annotations` or `generated-test-sources/test-annotations`. jdtls +/// runs the processors itself and writes there; javac (a Maven build) writes the same +/// files, and jdtls must not take those as edits (#4128). +pub(crate) fn is_processor_output(path: &Path) -> bool { + let parts: Vec<_> = path.components().map(|c| c.as_os_str()).collect(); + parts.windows(2).any(|w| { + (w[0] == "generated-sources" && w[1] == "annotations") + || (w[0] == "generated-test-sources" && w[1] == "test-annotations") + }) +} + /// Generated Java source folders under `root` (a reactor or one module). pub(crate) fn scan(root: &Path) -> GeneratedScan { let mut modules = Vec::new(); @@ -260,6 +272,20 @@ fn plugins_removed(build: &str) -> String { mod tests { use super::*; + #[test] + fn recognizes_annotation_processor_output() { + for (path, expected) in [ + ("/p/m/target/generated-sources/annotations/com/x/FooMappingImpl.java", true), + ("/p/m/target/generated-sources/annotations", true), + ("/p/m/build-out/generated-test-sources/test-annotations/com/x/T.java", true), + ("/p/m/target/generated-sources/protobuf/java/com/x/Hello.java", false), + ("/p/m/src/main/java/annotations/Foo.java", false), + ("/p/m/target/classes/com/x/FooMappingImpl.class", false), + ] { + assert_eq!(is_processor_output(Path::new(path)), expected, "{path}"); + } + } + fn write(root: &Path, rel: &str, text: &str) { let path = root.join(rel); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); diff --git a/crates/ide-lsp/src/lib.rs b/crates/ide-lsp/src/lib.rs index bdf37c8dcc..a6871ca0c7 100644 --- a/crates/ide-lsp/src/lib.rs +++ b/crates/ide-lsp/src/lib.rs @@ -3057,7 +3057,12 @@ impl LspManager { for s in servers { let Some(run) = s.running() else { continue }; let open = open.get(&(s.id, run.generation)).filter(|_| run.caps.read().sync != SyncKind::None); + // jdtls owns its annotation-processor output; javac's copy of those files is not an edit. + let jdtls = s.name == "jdtls"; let send = |p: &PathBuf, change: Change| { + if jdtls && jdtls::is_processor_output(p) { + return false; + } change == Change::Deleted || !open .is_some_and(|paths| paths.contains(p) || canonical.get(p).is_some_and(|c| paths.contains(c))) diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rs index 0713e013a4..0f349ce0e2 100644 --- a/crates/ide-lsp/tests/mock_server.rs +++ b/crates/ide-lsp/tests/mock_server.rs @@ -991,6 +991,33 @@ async fn files_changed_on_disk_reach_the_servers_of_their_language() { h.mgr.shutdown_all().await; } +/// jdtls runs the annotation processors itself and writes their output; a Maven build's +/// copy of those files must not reach it as an edit, or jdtls recompiles javac's copy and +/// overwrites its classes with broken ones (#4128). +#[tokio::test(flavor = "multi_thread")] +async fn jdtls_does_not_hear_of_annotation_processor_output() { + let h = harness_for("java", "java", &[("jdtls", incremental_caps())], &[]); + let (doc, _) = h.open("Open.java", "class Open {}"); + h.wait_status("jdtls", ServerStatus::Ready).await; + let s = h.launcher.state("jdtls"); + let source = h.root.join("m/src/main/java/com/x/FooMapping.java"); + let generated = h.root.join("m/target/generated-sources/annotations/com/x/FooMappingImpl.java"); + let test_generated = h.root.join("m/target/generated-test-sources/test-annotations/com/x/T.java"); + h.mgr.watched_files_changed( + &[test_generated], + &[generated.clone(), source.clone()], + std::slice::from_ref(&generated), + &[h.root.join("m/target/generated-sources/annotations")], + ); + eventually(|| !s.notified("workspace/didChangeWatchedFiles").is_empty()).await; + h.mgr.hover(doc, Pos::new(0, 0), Default::default()).await.unwrap(); + assert_eq!( + s.notified("workspace/didChangeWatchedFiles"), + vec![json!({ "changes": [{ "uri": uri_of(&source), "type": 2 }] })] + ); + h.mgr.shutdown_all().await; +} + /// An external change to a document open on the server's current instance reaches it only /// through the reload's `didChange`: a watched-file change for it too would make a server /// that re-reads the file on one (csharp-ls) apply the reload and then the diff computed