From 8131eaae5bd7be0ea69e574cae15d1de143ac5c3 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:21:16 +0800 Subject: [PATCH] fix(data): use isolated write probes and correct migration guidance (#2430) The fixed probe path could delete existing files, follow symlinks, and race between launches. Use an exclusively created temporary file instead. Limit migration instructions to rebuildable local entries so user data stays in place. Follow-up to #2373 --- crates/ide-host/src/data_dirs.rs | 51 ++++++++++++++++++++++++++++++-- docs/data-directories.md | 7 +++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/ide-host/src/data_dirs.rs b/crates/ide-host/src/data_dirs.rs index 531e47d13..f163b2fd5 100644 --- a/crates/ide-host/src/data_dirs.rs +++ b/crates/ide-host/src/data_dirs.rs @@ -26,10 +26,8 @@ fn resolve_local(var: Option, os: Option, identifier: &str) - return Err(format!("{LOCAL_DATA_ENV} must be an absolute path: {}", root.display())); } let dir = root.join(identifier); - let probe = dir.join(".openidea-write-check"); fs::create_dir_all(&dir) - .and_then(|()| fs::write(&probe, b"")) - .and_then(|()| fs::remove_file(&probe)) + .and_then(|()| tempfile::NamedTempFile::new_in(&dir).map(drop)) .map_err(|error| format!("{LOCAL_DATA_ENV}: cannot write to {}: {error}", dir.display()))?; Ok(dir) } @@ -327,6 +325,53 @@ mod tests { assert_ne!(resolve_local(Some(root.clone().into()), None, "dev-a").unwrap(), dir); } + #[test] + fn local_dir_preserves_an_existing_probe_file() { + let tmp = tempfile::tempdir().unwrap(); + let dir = tmp.path().join("id"); + fs::create_dir(&dir).unwrap(); + let probe = dir.join(".openidea-write-check"); + fs::write(&probe, "keep").unwrap(); + assert_eq!(resolve_local(Some(tmp.path().into()), None, "id").unwrap(), dir); + assert_eq!(fs::read_to_string(probe).unwrap(), "keep"); + assert_eq!(fs::read_dir(dir).unwrap().count(), 1); + } + + #[cfg(unix)] + #[test] + fn local_dir_preserves_an_existing_probe_symlink_and_its_target() { + let tmp = tempfile::tempdir().unwrap(); + let dir = tmp.path().join("id"); + fs::create_dir(&dir).unwrap(); + let target = tmp.path().join("target"); + fs::write(&target, "keep").unwrap(); + let probe = dir.join(".openidea-write-check"); + std::os::unix::fs::symlink(&target, &probe).unwrap(); + assert_eq!(resolve_local(Some(tmp.path().into()), None, "id").unwrap(), dir); + assert_eq!(fs::read_to_string(target).unwrap(), "keep"); + assert!(fs::symlink_metadata(probe).unwrap().file_type().is_symlink()); + assert_eq!(fs::read_dir(dir).unwrap().count(), 1); + } + + #[test] + fn local_dir_allows_concurrent_probes_for_the_same_identifier() { + let tmp = tempfile::tempdir().unwrap(); + let barrier = std::sync::Barrier::new(8); + std::thread::scope(|scope| { + for _ in 0..8 { + let barrier = &barrier; + let root = tmp.path(); + scope.spawn(move || { + barrier.wait(); + for _ in 0..16 { + assert_eq!(resolve_local(Some(root.into()), None, "id").unwrap(), root.join("id")); + } + }); + } + }); + assert_eq!(fs::read_dir(tmp.path().join("id")).unwrap().count(), 0); + } + #[test] fn local_dir_rejects_relative_and_unwritable_paths() { for relative in ["local", "./local", "../local"] { diff --git a/docs/data-directories.md b/docs/data-directories.md index 56ca14b50..67f6666dc 100644 --- a/docs/data-directories.md +++ b/docs/data-directories.md @@ -20,5 +20,8 @@ setx OPENIDEA_LOCAL_DATA_DIR "E:\OpenIDEA" Settings, plugins, scratches and other user data stay in the operating system's data and configuration directories. Logs stay in their usual location, so they are still written when the variable is wrong. Existing data is not moved: quit -OpenIDEA and move the old directory's contents to the new location, or let the -IDE download tools and rebuild indexes again. +OpenIDEA and move only `tools/`, `language-server-runtimes/`, `index/`, `cache/`, +`lsp/`, `notebook-runtime/`, `server/`, `logs/`, `agents/tools/`, +`agents/registry.json` and top-level `java-usages-*.jar` / `java-usages-*.tmp` to +the new location, preserving their relative paths, or let the IDE download and +rebuild them. Leave everything else in place.