Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 57711e38b0 fix(java): keep a Maven build's processor output away from jdtls
jdtls runs annotation processors itself and writes their output to the same
generated-sources/annotations folder javac uses. When a Maven build rewrote
those files, the watched-file change reached jdtls, which compiled javac's copy
as an edited source, failed to resolve the mapper interface and overwrote the
implementation in target/classes with a class that throws 'Unresolved
compilation problems'. Changes under generated-sources/annotations and
generated-test-sources/test-annotations are no longer sent to jdtls.
2026-11-18 22:53:02 +08:00
4 змінених файлів з 59 додано та 0 видалено

No files matched your search

+1
Переглянути файл
@@ -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;
+26
Переглянути файл
@@ -25,6 +25,18 @@ pub(crate) struct GeneratedScan {
pub roots: Vec<GeneratedRoot>,
}
/// 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();
+5
Переглянути файл
@@ -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)))
+27
Переглянути файл
@@ -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