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(lsp): serialize the jdt.ls classpath resolve with the build lock (#946)

arookieofc2161a2ca0387
diff --git a/crates/ide-host/src/ops/java.rs b/crates/ide-host/src/ops/java.rs
index d1cc0a616..00da18039 100644
--- a/crates/ide-host/src/ops/java.rs
+++ b/crates/ide-host/src/ops/java.rs
@@ -59,7 +59,7 @@ async fn resolve_launch(
.map_err(|e| IdeError::internal(e.to_string()))?
};
let external_build = if delegated { Some(project.lsp.java_external_build(&module, cancel).await?) } else { None };
- let mut info = project.lsp.java_launch(&module, test, build && !delegated, cancel).await?;
+ let mut info = project.lsp.java_launch(&module, test, build && !delegated, delegated, cancel).await?;
let lsp_ms = started.elapsed().as_millis();
let data_dir = host.local_data_dir.clone();
let cancel = cancel.clone();
diff --git a/crates/ide-lsp/src/jdtls.rs b/crates/ide-lsp/src/jdtls.rs
index 8198a443c..3b84f1563 100644
--- a/crates/ide-lsp/src/jdtls.rs
+++ b/crates/ide-lsp/src/jdtls.rs
@@ -521,11 +521,15 @@ impl LspManager {
}
/// Builds (when asked), then resolves the classpath and JDK of the module at `module`.
+ /// The classpath queries take the module's workspace scheduling rule like the build
+ /// commands do, so they run under the build lock; pass `build_lock_held` when the caller
+ /// already holds it (the delegated external build), or the wait would deadlock.
pub async fn java_launch(
&self,
module: &Path,
test: bool,
build: bool,
+ build_lock_held: bool,
cancel: &CancellationToken,
) -> IdeResult<JavaLaunchInfo> {
let build = if build { Some(self.java_build(module, cancel).await?) } else { None };
@@ -538,14 +542,23 @@ impl LspManager {
}
_ => (Vec::new(), 0),
};
- let cp = self.java_classpath(module, test, cancel).await?;
- let java_home = match self.java_vm_location(module, cancel).await {
- Ok(home) => home,
- Err(e) if e.kind == ErrorKind::Cancelled => return Err(e),
- Err(e) => {
- tracing::debug!("java.project.getSettings: {e}");
+ let (cp, java_home) = {
+ let (server, _) = self.jdtls(module, cancel).await?;
+ let _lock = if build_lock_held {
None
- }
+ } else {
+ Some(cancellable_wait(cancel, server.java_build_lock.lock()).await?)
+ };
+ let cp = self.java_classpath(module, test, cancel).await?;
+ let java_home = match self.java_vm_location(module, cancel).await {
+ Ok(home) => home,
+ Err(e) if e.kind == ErrorKind::Cancelled => return Err(e),
+ Err(e) => {
+ tracing::debug!("java.project.getSettings: {e}");
+ None
+ }
+ };
+ (cp, java_home)
};
Ok(JavaLaunchInfo {
build,
diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rs
index b33110a20..dc66dd967 100644
--- a/crates/ide-lsp/tests/mock_server.rs
+++ b/crates/ide-lsp/tests/mock_server.rs
@@ -1166,6 +1166,40 @@ async fn unmanaged_java_run_imports_sources_without_an_open_editor() {
assert!(state.notified("textDocument/didOpen").is_empty());
}
+#[tokio::test(flavor = "multi_thread")]
+async fn java_launch_serializes_the_classpath_with_the_build_lock() {
+ let h = harness_for("java", "java", &[("jdtls", incremental_caps())], &[]);
+ let (_, _file) = h.open("Main.java", "public class Main {}");
+ h.wait_status("jdtls", ServerStatus::Ready).await;
+ let state = h.launcher.state("jdtls");
+ state.respond("java/buildWorkspace", json!(1));
+
+ // A launch with a build re-takes the lock after java_build has released it.
+ let info = tokio::time::timeout(
+ Duration::from_secs(30),
+ h.mgr.java_launch(&h.root, false, true, false, &Default::default()),
+ )
+ .await
+ .expect("java_launch deadlocked on the build lock")
+ .unwrap();
+ assert!(info.class_paths.is_empty());
+
+ // The delegated external build holds the lock while java_launch resolves the
+ // classpath; java_launch must not re-take it, or the launch would deadlock.
+ let guard = h.mgr.java_external_build(&h.root, &Default::default()).await.unwrap();
+ tokio::time::timeout(Duration::from_secs(30), h.mgr.java_launch(&h.root, false, false, true, &Default::default()))
+ .await
+ .expect("java_launch deadlocked on the delegated external build's lock")
+ .unwrap();
+ drop(guard);
+ let commands: Vec<_> = state
+ .requested("workspace/executeCommand")
+ .iter()
+ .filter_map(|p| p["command"].as_str().map(str::to_owned))
+ .collect();
+ assert!(commands.contains(&"java.project.getClasspaths".to_owned()));
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn fallback_servers_only_run_when_the_preferred_one_is_missing() {
// Preferred installed: the alternative is never started.