Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(java): count only the launched module's jdtls compile errors (#2147)

misswellc1e5db9511d8
diff --git a/crates/ide-lsp/src/jdtls.rs b/crates/ide-lsp/src/jdtls.rs
index f06c1d2a5..021ade51a 100644
--- a/crates/ide-lsp/src/jdtls.rs
+++ b/crates/ide-lsp/src/jdtls.rs
@@ -351,6 +351,115 @@ pub fn compile_errors(
(errors, count)
}
+fn launch_path(path: &Path) -> PathBuf {
+ // Resolve existing ancestors too: diagnostics may name a file that was just deleted.
+ let resolved = path.ancestors().find_map(|ancestor| {
+ dunce::canonicalize(ancestor).ok().map(|root| root.join(path.strip_prefix(ancestor).unwrap()))
+ });
+ let mut path = resolved.as_deref().unwrap_or(path).to_string_lossy().replace('\\', "/");
+ if path.as_bytes().get(1) == Some(&b':') {
+ path[..1].make_ascii_lowercase();
+ }
+ PathBuf::from(path)
+}
+
+/// The runtime classpath identifies the launched module's dependencies: a directory
+/// filter alone either drops their errors or includes unrelated reactor modules.
+struct LaunchScope {
+ module: PathBuf,
+ /// None means the dependency scope could not be resolved; retain all errors.
+ roots: Option<Vec<PathBuf>>,
+ test: bool,
+}
+
+impl LaunchScope {
+ fn new(module: &Path, test: bool, cp: Option<&JavaClasspath>) -> Self {
+ let started = Instant::now();
+ let module = launch_path(module);
+ let roots = cp.and_then(|cp| {
+ let mut roots = vec![module.clone()];
+ let mut has_output = false;
+ let mut projects = std::collections::HashMap::<PathBuf, Option<PathBuf>>::new();
+ for entry in cp.class_paths.iter().chain(&cp.module_paths) {
+ let path = project_path(entry).unwrap_or_else(|| PathBuf::from(entry));
+ if path
+ .extension()
+ .is_some_and(|ext| ext.eq_ignore_ascii_case("jar") || ext.eq_ignore_ascii_case("zip"))
+ {
+ continue;
+ }
+ let path = launch_path(&path);
+ let root = path
+ .ancestors()
+ .find_map(|ancestor| {
+ let relative = path.strip_prefix(ancestor).ok()?;
+ let output = match ancestor.file_name()?.to_str()? {
+ "target" => relative == Path::new("classes") || relative == Path::new("test-classes"),
+ "build" => relative.starts_with("classes") || relative.starts_with("resources"),
+ _ => false,
+ };
+ output.then(|| ancestor.parent().map(Path::to_path_buf)).flatten()
+ })
+ .or_else(|| {
+ let mut visited = Vec::new();
+ let mut project = None;
+ for ancestor in path.ancestors() {
+ if let Some(cached) = projects.get(ancestor) {
+ project = cached.clone();
+ break;
+ }
+ visited.push(ancestor.to_path_buf());
+ if ["pom.xml", "build.gradle", "build.gradle.kts", ".project"]
+ .iter()
+ .any(|name| ancestor.join(name).is_file())
+ {
+ project = Some(ancestor.to_path_buf());
+ break;
+ }
+ }
+ for ancestor in visited {
+ projects.insert(ancestor, project.clone());
+ }
+ project
+ })?;
+ has_output = true;
+ if !roots.contains(&root) {
+ roots.push(root);
+ }
+ }
+ has_output.then_some(roots)
+ });
+ tracing::debug!(
+ elapsed_us = started.elapsed().as_micros(),
+ scoped = roots.is_some(),
+ "Java launch diagnostic scope"
+ );
+ Self { module, roots, test }
+ }
+
+ fn includes(&self, path: &Path) -> bool {
+ let Some(roots) = &self.roots else { return true };
+ let path = launch_path(path);
+ let Some(root) =
+ roots.iter().filter(|root| path.starts_with(root)).max_by_key(|root| root.components().count())
+ else {
+ return false;
+ };
+ let relative = path.strip_prefix(root).unwrap();
+ let parts: Vec<_> = relative.components().collect();
+ let test_source = parts.windows(2).any(|pair| pair[0].as_os_str() == "src" && pair[1].as_os_str() == "test");
+ !test_source || (self.test && *root == self.module)
+ }
+}
+
+/// JDTLS reports all imported projects; report errors for the launch and its dependencies.
+fn launch_compile_errors(
+ scope: &LaunchScope,
+ diagnostics: Vec<(PathBuf, lsp_types::Diagnostic)>,
+) -> (Vec<JavaCompileError>, u32) {
+ compile_errors(diagnostics.into_iter().filter(|(path, _)| scope.includes(path)).collect(), MAX_LAUNCH_ERRORS)
+}
+
/// `java.project.getClasspaths` result.
pub fn parse_classpaths(v: &Value) -> JavaClasspath {
let strings = |key: &str| -> Vec<String> {
@@ -740,12 +849,6 @@ impl LspManager {
if build == Some(JavaBuildStatus::Cancelled) {
return Err(IdeError::cancelled());
}
- let (errors, error_count) = match build {
- Some(JavaBuildStatus::WithErrors | JavaBuildStatus::Failed) => {
- cancellable_wait(cancel, self.java_compile_errors(module)).await?
- }
- _ => (Vec::new(), 0),
- };
let (cp, java_home) = {
let (server, _) = self.jdtls(module, cancel).await?;
let _lock = if build_lock_held {
@@ -753,7 +856,15 @@ impl LspManager {
} else {
Some(cancellable_wait(cancel, server.java_build_lock.lock()).await?)
};
- let cp = self.java_classpath(module, test, cancel).await?;
+ let cp = match self.java_classpath(module, test, cancel).await {
+ Ok(cp) => Some(cp),
+ Err(e) if e.kind == ErrorKind::Cancelled => return Err(e),
+ Err(e) if matches!(build, Some(JavaBuildStatus::WithErrors | JavaBuildStatus::Failed)) => {
+ tracing::debug!("java.project.getClasspaths: {e}; retaining all compile errors");
+ None
+ }
+ Err(e) => return Err(e),
+ };
let java_home = match self.java_vm_location(module, cancel).await {
Ok(home) => home,
Err(e) if e.kind == ErrorKind::Cancelled => return Err(e),
@@ -764,6 +875,25 @@ impl LspManager {
};
(cp, java_home)
};
+ let (errors, error_count, scoped) = match build {
+ Some(JavaBuildStatus::WithErrors | JavaBuildStatus::Failed) => {
+ let scope_module = module.to_path_buf();
+ let cp = cp.clone();
+ let scope = tokio::task::spawn_blocking(move || LaunchScope::new(&scope_module, test, cp.as_ref()))
+ .await
+ .map_err(|e| IdeError::internal(e.to_string()))?;
+ let scoped = scope.roots.is_some();
+ let (errors, count) = cancellable_wait(cancel, self.java_compile_errors(module, scope)).await??;
+ (errors, count, scoped)
+ }
+ _ => (Vec::new(), 0, false),
+ };
+ let build = if build == Some(JavaBuildStatus::WithErrors) && error_count == 0 && scoped {
+ Some(JavaBuildStatus::Succeeded)
+ } else {
+ build
+ };
+ let cp = cp.unwrap_or_default();
Ok(JavaLaunchInfo {
build,
project_root: cp.project_root,
@@ -779,16 +909,20 @@ impl LspManager {
})
}
- /// Compile errors jdtls reports for its workspace (all modules it imported).
- async fn java_compile_errors(&self, module: &Path) -> (Vec<JavaCompileError>, u32) {
- let Ok((server, _)) = self.jdtls(module, &CancellationToken::new()).await else { return (Vec::new(), 0) };
- let collect = || compile_errors(server.diagnostics_snapshot(), MAX_LAUNCH_ERRORS);
- let found = collect();
- if found.1 > 0 {
- return found;
- }
- let _ = tokio::time::timeout(ERRORS_WAIT, server.published_notify.notified()).await;
- collect()
+ /// Wait for late build markers only when no error diagnostics have arrived yet.
+ async fn java_compile_errors(&self, module: &Path, scope: LaunchScope) -> IdeResult<(Vec<JavaCompileError>, u32)> {
+ let (server, _) = self.jdtls(module, &CancellationToken::new()).await?;
+ let diagnostics = server.diagnostics_snapshot();
+ let has_errors = diagnostics.iter().any(|(_, d)| d.severity == Some(lsp_types::DiagnosticSeverity::ERROR));
+ let diagnostics = if has_errors {
+ diagnostics
+ } else {
+ let _ = tokio::time::timeout(ERRORS_WAIT, server.published_notify.notified()).await;
+ server.diagnostics_snapshot()
+ };
+ tokio::task::spawn_blocking(move || launch_compile_errors(&scope, diagnostics))
+ .await
+ .map_err(|e| IdeError::internal(e.to_string()))
}
/// Resolve a disk/archive class to project source or the server's binary document.
@@ -1058,6 +1192,95 @@ mod tests {
assert_eq!(count, 1);
}
+ fn error_at(path: impl Into<PathBuf>) -> (PathBuf, lsp_types::Diagnostic) {
+ (
+ path.into(),
+ lsp_types::Diagnostic {
+ severity: Some(lsp_types::DiagnosticSeverity::ERROR),
+ message: "boom".into(),
+ ..Default::default()
+ },
+ )
+ }
+
+ #[test]
+ fn launch_errors_include_dependencies_but_only_the_launched_modules_tests() {
+ for output in ["target/classes", "build/classes/java/main", "build/classes/kotlin/main"] {
+ let cp = JavaClasspath {
+ class_paths: vec!["/p/web/target/classes".into(), format!("/p/service/{output}"), "/m2/lib.jar".into()],
+ module_paths: vec!["/p/api/target/classes".into()],
+ ..Default::default()
+ };
+ let paths = [
+ "/p/web/src/main/java/Main.java",
+ "/p/web/src/test/java/MainTest.java",
+ "/p/service/src/main/java/Service.java",
+ "/p/service/src/test/java/ServiceTest.java",
+ "/p/other/src/main/java/Other.java",
+ "/p/web-other/src/main/java/Other.java",
+ "/p/api/src/main/java/Api.java",
+ "/p/web/src/testing/Real.java",
+ ];
+ for (test, expected) in [(false, 4), (true, 5)] {
+ let scope = LaunchScope::new(Path::new("/p/web"), test, Some(&cp));
+ assert_eq!(launch_compile_errors(&scope, paths.iter().map(|p| error_at(*p)).collect()).1, expected);
+ assert!(scope.includes(Path::new("/p/service/src/main/java/Service.java")));
+ assert!(!scope.includes(Path::new("/p/service/src/test/java/ServiceTest.java")));
+ }
+ }
+ }
+
+ #[test]
+ fn unresolved_classpaths_retain_all_errors() {
+ for cp in [
+ None,
+ Some(JavaClasspath::default()),
+ Some(JavaClasspath { class_paths: vec!["/unknown/output".into()], ..Default::default() }),
+ Some(JavaClasspath { class_paths: vec!["/m2/lib.jar".into()], ..Default::default() }),
+ Some(JavaClasspath {
+ class_paths: vec!["/p/web/target/classes".into(), "/unknown/output".into()],
+ ..Default::default()
+ }),
+ ] {
+ let scope = LaunchScope::new(Path::new("/p/web"), false, cp.as_ref());
+ let diagnostics = ["/p/service/src/main/java/Service.java", "/p/other/src/test/java/Test.java"];
+ assert_eq!(launch_compile_errors(&scope, diagnostics.into_iter().map(error_at).collect()).1, 2);
+ }
+ }
+
+ #[test]
+ fn launch_scope_normalizes_windows_drive_letters_and_separators() {
+ let cp = JavaClasspath { class_paths: vec![r"C:\p\web\target\classes".into()], ..Default::default() };
+ let scope = LaunchScope::new(Path::new(r"c:\p\web"), false, Some(&cp));
+ assert!(scope.includes(Path::new("C:/p/web/src/main/java/Main.java")));
+ assert!(!scope.includes(Path::new(r"C:\p\web\src\test\java\MainTest.java")));
+ assert!(!scope.includes(Path::new("C:/p/web-other/src/main/java/Main.java")));
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn launch_scope_resolves_symlinks_and_project_reference_entries() {
+ let tmp = tempfile::tempdir().unwrap();
+ let module = tmp.path().join("web");
+ let dependency = tmp.path().join("service");
+ std::fs::create_dir_all(module.join("src/main/java")).unwrap();
+ std::fs::create_dir_all(&dependency).unwrap();
+ std::fs::write(dependency.join(".project"), "").unwrap();
+ let alias = tmp.path().join("alias");
+ std::os::unix::fs::symlink(&module, &alias).unwrap();
+ let cp = JavaClasspath {
+ class_paths: vec![
+ alias.join("target/classes").to_string_lossy().into_owned(),
+ crate::uri::path_to_uri_string(&dependency),
+ ],
+ ..Default::default()
+ };
+ let scope = LaunchScope::new(&alias, false, Some(&cp));
+ assert!(scope.includes(&dunce::canonicalize(&module).unwrap().join("src/main/java/Deleted.java")));
+ assert!(scope.includes(&dependency.join("src/main/java/Service.java")));
+ assert!(!scope.includes(&dependency.join("src/test/java/ServiceTest.java")));
+ }
+
#[test]
fn classpaths() {
let v = json!({
diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rs
index 6cb2c5d3b..06b730de8 100644
--- a/crates/ide-lsp/tests/mock_server.rs
+++ b/crates/ide-lsp/tests/mock_server.rs
@@ -1821,6 +1821,59 @@ async fn java_launch_serializes_the_classpath_with_the_build_lock() {
assert!(commands.contains(&"java.project.getClasspaths".to_owned()));
}
+#[tokio::test(flavor = "multi_thread")]
+async fn java_launch_scopes_build_status_and_does_not_wait_for_unrelated_errors() {
+ use ide_lsp::jdtls::JavaBuildStatus;
+
+ let h = harness_for("java", "java", &[("jdtls", incremental_caps())], &[]);
+ h.open("Main.java", "class Main {}");
+ h.wait_status("jdtls", ServerStatus::Ready).await;
+ let state = h.launcher.state("jdtls");
+ state.respond("java/buildWorkspace", json!(2));
+ let web = h.root.join("web");
+ let classpath = json!({"classpaths": [web.join("target/classes"), h.root.join("service/target/classes")]});
+ for (path, test, expected, classpaths_available) in [
+ ("other/src/main/java/Other.java", false, JavaBuildStatus::Succeeded, true),
+ ("other/src/main/java/Other.java", false, JavaBuildStatus::Succeeded, true),
+ ("web/src/test/java/MainTest.java", false, JavaBuildStatus::Succeeded, true),
+ ("web/src/test/java/MainTest.java", true, JavaBuildStatus::WithErrors, true),
+ ("service/src/test/java/ServiceTest.java", true, JavaBuildStatus::Succeeded, true),
+ ("service/src/main/java/Service.java", false, JavaBuildStatus::WithErrors, true),
+ ("web/src/main/java/Main.java", false, JavaBuildStatus::WithErrors, true),
+ ("other/src/main/java/Other.java", false, JavaBuildStatus::WithErrors, false),
+ ] {
+ state.respond("workspace/executeCommand", classpath.clone());
+ if !classpaths_available {
+ state.errors.lock().insert("workspace/executeCommand".into(), (-32603, "classpath unavailable".into()));
+ }
+ let file = h.root.join(path);
+ std::fs::create_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::write(&file, "broken").unwrap();
+ let diag = json!({"range": {"start":{"line":0,"character":0},"end":{"line":0,"character":1}},
+ "severity": 1, "message": path});
+ state
+ .peer()
+ .notify("textDocument/publishDiagnostics", json!({"uri": uri_of(&file), "diagnostics": [diag]}))
+ .unwrap();
+ eventually(|| diagnostics_for(&h, &file) == 1).await;
+ let info = tokio::time::timeout(
+ Duration::from_secs(1),
+ h.mgr.java_launch(&web, test, true, false, &Default::default()),
+ )
+ .await
+ .expect("an already-published error must not incur the diagnostics wait")
+ .unwrap();
+ assert_eq!(info.build, Some(expected), "{path}, test={test}, classpath={classpaths_available}");
+ assert_eq!(info.error_count, u32::from(expected == JavaBuildStatus::WithErrors));
+ state
+ .peer()
+ .notify("textDocument/publishDiagnostics", json!({"uri": uri_of(&file), "diagnostics": []}))
+ .unwrap();
+ eventually(|| diagnostics_for(&h, &file) == 2).await;
+ h.events.lock().clear();
+ }
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn open_java_files_are_checked_again_after_a_project_update() {
let h = harness_for("java", "java", &[("jdtls", incremental_caps())], &[]);