From 74a009dcc3f96125a73c6177e9e7d31fdaec2479 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 24 Nov 2026 21:16:30 +0800 Subject: [PATCH] fix(java): keep jdtls's temporary folder off paths with spaces (#5637) jdtls ran with -Djava.io.tmpdir=/.tmp under macOS's Application Support; Buildship writes its Gradle init script there and splits stored arguments at spaces, so every Gradle import failed. When the workspace path has whitespace, jdtls now gets its own folder in the system temporary folder (stable hash of the workspace), still emptied before each start. Fixes #5557. --- crates/ide-sdk/src/jdtls_workspaces.rs | 49 +++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/crates/ide-sdk/src/jdtls_workspaces.rs b/crates/ide-sdk/src/jdtls_workspaces.rs index c8b2a9f333..21bacb20e4 100644 --- a/crates/ide-sdk/src/jdtls_workspaces.rs +++ b/crates/ide-sdk/src/jdtls_workspaces.rs @@ -28,11 +28,33 @@ fn workspace(cfg: &LanguageServerConfig) -> Option<&Path> { cfg.args.iter().position(|a| a == "-data").and_then(|i| cfg.args.get(i + 1)).map(Path::new) } -fn temp_arg(workspace: &Path) -> String { - format!("-Djava.io.tmpdir={}", workspace.join(TEMP_DIR).to_string_lossy()) +/// jdtls' temporary folder for `workspace`: `/.tmp`, or a folder of its own in the +/// system temporary folder when the workspace path has whitespace (macOS's `Application Support`). +/// Buildship writes its Gradle init script there and later splits the stored arguments at spaces, +/// so a path with a space reaches Gradle cut off and every Gradle import fails. +fn temp_dir(workspace: &Path) -> PathBuf { + let own = workspace.join(TEMP_DIR); + if !has_whitespace(&own) { + return own; + } + // FNV-1a: stable across builds, so a workspace keeps its folder from one start to the next. + let hash = workspace + .to_string_lossy() + .bytes() + .fold(0xcbf2_9ce4_8422_2325_u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)); + let shared = std::env::temp_dir().join(format!("openidea-jdtls-{hash:016x}")); + if has_whitespace(&shared) { own } else { shared } } -/// Gives jdtls a temporary folder inside a workspace of ours (`/.tmp`) unless the +fn has_whitespace(path: &Path) -> bool { + path.to_string_lossy().chars().any(char::is_whitespace) +} + +fn temp_arg(workspace: &Path) -> String { + format!("-Djava.io.tmpdir={}", temp_dir(workspace).to_string_lossy()) +} + +/// Gives jdtls a temporary folder of a workspace of ours ([`temp_dir`]) unless the /// user's JVM arguments choose one. JDT APT copies a project's annotation processor jars into a /// new `jdt-apt-*` folder for each processor class loader and deletes it only when the JVM exits /// normally — never on Windows, where the jars are still open then — so in the system temp @@ -56,7 +78,7 @@ fn clear_temp(cfg: &LanguageServerConfig, workspace: &Path) { if !cfg.args.iter().any(|a| a.strip_prefix("--jvm-arg=").unwrap_or(a) == arg) { return; } - let temp = workspace.join(TEMP_DIR); + let temp = temp_dir(workspace); for entry in std::fs::read_dir(&temp).into_iter().flatten().flatten() { let path = entry.path(); let removed = if entry.file_type().is_ok_and(|t| t.is_dir()) { @@ -232,6 +254,25 @@ mod tests { assert!(ws.join(TEMP_DIR).join("kept").exists()); } + #[test] + fn a_workspace_path_with_a_space_keeps_jdtls_temporary_files_out_of_it() { + let tmp = tempfile::tempdir().unwrap(); + let data = tmp.path().join("Application Support"); + let ws = root(&data).join("p"); + let mut cfg = cfg(&ws); + use_own_temp(&mut cfg, &data); + let arg = cfg.args.last().unwrap().strip_prefix("--jvm-arg=-Djava.io.tmpdir=").unwrap().to_owned(); + assert!(!arg.contains(' '), "{arg}"); + assert!(Path::new(&arg).starts_with(std::env::temp_dir())); + assert_eq!(temp_dir(&ws), temp_dir(&ws)); + assert_ne!(temp_dir(&ws), temp_dir(&root(&data).join("q"))); + + std::fs::create_dir_all(Path::new(&arg).join("jdt-apt-1")).unwrap(); + mark_used(&cfg); + assert_eq!(std::fs::read_dir(&arg).unwrap().count(), 0); + std::fs::remove_dir_all(&arg).unwrap(); + } + #[cfg(unix)] #[test] fn a_workspace_from_before_markers_is_judged_by_its_folders_and_log() {