diff --git a/crates/ide-sdk/src/jdtls_workspaces.rs b/crates/ide-sdk/src/jdtls_workspaces.rs index 1f9f07b3d4..78d5d4c6b3 100644 --- a/crates/ide-sdk/src/jdtls_workspaces.rs +++ b/crates/ide-sdk/src/jdtls_workspaces.rs @@ -107,9 +107,11 @@ fn clear_temp(cfg: &LanguageServerConfig, workspace: &Path) { } /// Records that jdtls is starting on the workspace of `cfg` (its `-data` folder), and empties -/// the workspace's temporary folder. +/// the workspace's temporary folder. A workspace whose resource tree failed to load on the last +/// start is repaired first ([`repair_unreadable`]). pub fn mark_used(cfg: &LanguageServerConfig) { let Some(dir) = workspace(cfg) else { return }; + repair_unreadable(dir); clear_temp(cfg, dir); if let Err(e) = std::fs::create_dir_all(dir).and_then(|()| { let marker = std::fs::File::create(dir.join(USED_MARKER))?; @@ -119,6 +121,46 @@ pub fn mark_used(cfg: &LanguageServerConfig) { } } +/// How much of the end of jdtls' log is read for the last session. +const LOG_TAIL: u64 = 512 * 1024; + +/// Eclipse failed to restore the workspace's saved resource tree (`ObjectNotFoundException: Tree +/// element … not found`, `Could not read metadata for '….tree'`): a frame of every such failure, +/// after which each start exits with code 13 the same way. +const UNREADABLE_WORKSPACE: &str = "org.eclipse.core.internal.resources.SaveManager.restore"; + +/// Removes the saved resource tree of a workspace that the last jdtls start could not open, so +/// the next start rebuilds it (jdtls imports the projects again; JDT's own index is kept). +fn repair_unreadable(dir: &Path) { + let metadata = dir.join(".metadata"); + if !last_session_failed_to_open(&metadata.join(".log")) { + return; + } + let resources = metadata.join(".plugins").join("org.eclipse.core.resources"); + match std::fs::remove_dir_all(&resources) { + Ok(()) => tracing::warn!("jdtls: the workspace {} could not be opened; rebuilding it", dir.display()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => tracing::warn!("jdtls: cannot repair the workspace {}: {e}", dir.display()), + } +} + +/// Whether the latest session in jdtls' log (`!SESSION` blocks) failed to open the workspace. +fn last_session_failed_to_open(log: &Path) -> bool { + use std::io::{Read, Seek, SeekFrom}; + let Ok(mut file) = std::fs::File::open(log) else { return false }; + let len = file.metadata().map(|m| m.len()).unwrap_or(0); + if file.seek(SeekFrom::Start(len.saturating_sub(LOG_TAIL))).is_err() { + return false; + } + let mut bytes = Vec::new(); + if file.read_to_end(&mut bytes).is_err() { + return false; + } + let text = String::from_utf8_lossy(&bytes); + let last = text.rfind("!SESSION").map_or(&text[..], |i| &text[i..]); + last.contains(UNREADABLE_WORKSPACE) +} + /// When a workspace was last used: the latest of our marker (written when jdtls starts) and what /// jdtls itself touches while it runs and when it saves on exit (a server can run for weeks). fn last_used(dir: &Path) -> Option { @@ -197,6 +239,36 @@ mod tests { cfg } + #[test] + fn a_workspace_that_failed_to_open_loses_its_resource_tree_on_the_next_start() { + let data = tempfile::tempdir().unwrap(); + let cfg = cfg(data.path()); + let ws = workspace(&cfg).unwrap().to_path_buf(); + let resources = ws.join(".metadata/.plugins/org.eclipse.core.resources"); + let index = ws.join(".metadata/.plugins/org.eclipse.jdt.core"); + for dir in [&resources, &index] { + std::fs::create_dir_all(dir).unwrap(); + } + let failed = "!SESSION 2026-12-04 10:00:00\n!ENTRY org.eclipse.osgi 4 0\n!MESSAGE An error occurred\n\ + org.osgi.framework.BundleException: Exception in org.eclipse.core.resources.ResourcesPlugin.start() of bundle org.eclipse.core.resources.\n\ + Caused by: org.eclipse.core.internal.dtree.ObjectNotFoundException: Tree element '/app/target/classes' not found.\n\ + \tat org.eclipse.core.internal.resources.SaveManager.restore(SaveManager.java:830)\n"; + // An earlier failure followed by a good start: nothing to repair. + std::fs::write(ws.join(".metadata/.log"), format!("{failed}!SESSION 2026-12-04 11:00:00\n!MESSAGE started\n")) + .unwrap(); + mark_used(&cfg); + assert!(resources.is_dir()); + // The last start failed to open the workspace: its resource tree goes, the JDT index stays. + std::fs::write(ws.join(".metadata/.log"), format!("!SESSION 2026-12-04 09:00:00\n!MESSAGE ok\n{failed}")) + .unwrap(); + mark_used(&cfg); + assert!(!resources.exists()); + assert!(index.is_dir()); + // Starting again does nothing more. + mark_used(&cfg); + assert!(index.is_dir()); + } + #[test] fn removes_workspaces_unused_for_longer_than_the_limit() { let tmp = tempfile::tempdir().unwrap();