From f220ceb1174ee7cd6245569820bf39e95f93022b Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 30 Nov 2026 01:06:22 +0800 Subject: [PATCH] feat(data-dirs): portable installs with an openidea.properties file (#6869) An openidea.properties next to the executable (the AppImage file on Linux, the folder holding OpenIDEA.app on macOS) sets the OPENIDEA_CONFIG_DIR, OPENIDEA_DATA_DIR, OPENIDEA_LOCAL_DATA_DIR and OPENIDEA_LOG_DIR variables at start, so a portable install needs no environment. Relative paths are taken from the file's folder and the environment wins. Applied first thing in main so every directory and child process sees the same locations. Closes #6207 Closes #6437 --- crates/ide-host/src/data_dirs.rs | 76 ++++++++++++++++++++++++++ docs/en/reference/data-directories.mdx | 14 +++++ docs/zh/reference/data-directories.mdx | 14 +++++ src-tauri/src/main.rs | 2 + 4 files changed, 106 insertions(+) diff --git a/crates/ide-host/src/data_dirs.rs b/crates/ide-host/src/data_dirs.rs index 800c96940..4187a5e70 100644 --- a/crates/ide-host/src/data_dirs.rs +++ b/crates/ide-host/src/data_dirs.rs @@ -14,6 +14,65 @@ pub const DATA_ENV: &str = "OPENIDEA_DATA_DIR"; /// Moves the logs. pub const LOG_ENV: &str = "OPENIDEA_LOG_DIR"; +/// A portable install's settings file, next to the executable: `NAME=value` lines setting the +/// variables above (and only those), so the install needs no environment. A variable already set +/// in the environment wins; a relative value is taken from the file's folder. +pub const PROPERTIES_FILE: &str = "openidea.properties"; + +/// The folder `openidea.properties` is read from: the executable's, the AppImage file's when run +/// from one, the folder holding the `.app` bundle on macOS. +fn install_dir() -> Option { + let exe = std::env::current_exe().ok()?; + // `APPIMAGE` is inherited by everything started from inside one; only trust it for the + // executable it mounted. + let appdir = std::env::var_os("APPDIR").filter(|v| !v.is_empty()); + if let (Some(image), Some(appdir)) = (std::env::var_os("APPIMAGE").filter(|v| !v.is_empty()), appdir) + && exe.starts_with(appdir) + { + return PathBuf::from(image).parent().map(Path::to_path_buf); + } + let dir = exe.parent()?; + if cfg!(target_os = "macos") + && let Some(bundle) = dir.ancestors().nth(2).filter(|b| b.extension().is_some_and(|e| e == "app")) + { + return bundle.parent().map(Path::to_path_buf); + } + Some(dir.to_path_buf()) +} + +/// The variables an `openidea.properties` in `dir` sets, made absolute, leaving out those +/// `is_set` reports the environment already has. Unknown keys and comments (`#`, `!`) are ignored. +fn properties_in(dir: &Path, text: &str, is_set: impl Fn(&str) -> bool) -> Vec<(&'static str, PathBuf)> { + let mut out: Vec<(&'static str, PathBuf)> = Vec::new(); + for line in text.lines().map(str::trim) { + if line.starts_with(['#', '!']) { + continue; + } + let Some((key, value)) = line.split_once('=') else { continue }; + let (key, value) = (key.trim(), value.trim()); + let Some(name) = [CONFIG_ENV, DATA_ENV, LOCAL_DATA_ENV, LOG_ENV].into_iter().find(|n| *n == key) else { + continue; + }; + if value.is_empty() || is_set(name) || out.iter().any(|(n, _)| *n == name) { + continue; + } + // An absolute value replaces `dir`. + out.push((name, dir.join(value))); + } + out +} + +/// Applies the install's `openidea.properties` to the environment, so the directories below and +/// every process the IDE starts see the same locations. Call first thing in `main`. +pub fn load_properties_file() { + let Some(dir) = install_dir() else { return }; + let Ok(text) = fs::read_to_string(dir.join(PROPERTIES_FILE)) else { return }; + for (name, value) in properties_in(&dir, &text, |n| std::env::var_os(n).is_some_and(|v| !v.is_empty())) { + // SAFETY: called at the start of `main`, before any other thread exists. + unsafe { std::env::set_var(name, value) }; + } +} + /// `$OPENIDEA_CONFIG_DIR/` when set, else the OS configuration directory /// (Tauri's `app_config_dir`). Like the local data, the identifier keeps release and /// per-worktree debug builds apart under one root; a relative path is an error. @@ -664,6 +723,23 @@ mod tests { assert!(!tmp.path().join("copy/Default/Cache").exists()); } + #[test] + fn a_properties_file_sets_the_directory_variables_relative_to_its_folder() { + let dir = std::env::temp_dir().join("portable"); + let abs = std::env::temp_dir().join("elsewhere"); + let text = format!( + "# portable install\n! also a comment\nOPENIDEA_CONFIG_DIR = data\nOPENIDEA_DATA_DIR=data\n\ + OPENIDEA_LOCAL_DATA_DIR={}\nOPENIDEA_LOG_DIR=\nOPENIDEA_CONFIG_DIR=ignored\nPATH=/bin\nnot a pair\n", + abs.display() + ); + let set = properties_in(&dir, &text, |_| false); + assert_eq!(set, [(CONFIG_ENV, dir.join("data")), (DATA_ENV, dir.join("data")), (LOCAL_DATA_ENV, abs.clone())]); + // The environment wins over the file. + let set = properties_in(&dir, &text, |n| n == CONFIG_ENV); + assert_eq!(set.iter().map(|(n, _)| *n).collect::>(), [DATA_ENV, LOCAL_DATA_ENV]); + assert!(set.iter().all(|(_, v)| v.is_absolute())); + } + #[test] fn config_data_and_log_dirs_follow_the_os_unless_their_variable_is_set() { let os = Some(PathBuf::from("/os")); diff --git a/docs/en/reference/data-directories.mdx b/docs/en/reference/data-directories.mdx index 3b28bbdf7..a2f359ce4 100644 --- a/docs/en/reference/data-directories.mdx +++ b/docs/en/reference/data-directories.mdx @@ -83,6 +83,20 @@ Each of the other directories has its own variable, set the same way as `OPENIDE - If the configuration or user data path is relative or can't be used, OpenIDEA shows an error and exits instead of falling back to the default location. A wrong log path only turns off the log file. - Existing files are not moved. To keep your settings, plugins and history, quit OpenIDEA and copy the old folder's contents to the new location first. +## Portable install + +Instead of setting variables, put a file named `openidea.properties` next to the OpenIDEA executable (on Linux, next to the AppImage file; on macOS, in the folder that holds `OpenIDEA.app`). Each line sets one of the four variables above; `#` starts a comment, and a relative path is taken from the file's folder. For example, to keep everything in a `data` folder beside the executable on a USB drive: + +```properties +OPENIDEA_CONFIG_DIR=data +OPENIDEA_DATA_DIR=data +OPENIDEA_LOCAL_DATA_DIR=data +``` + +- A variable set in the environment wins over the file. +- The file is read once at start. The IDE passes the locations on to the processes it starts, such as terminals. +- Other lines are ignored. + ## Logs OpenIDEA writes one log file per day, `openidea..log`, and keeps the last 7. Panics are written to the current log too. diff --git a/docs/zh/reference/data-directories.mdx b/docs/zh/reference/data-directories.mdx index b0e0f9595..4e1ea2675 100644 --- a/docs/zh/reference/data-directories.mdx +++ b/docs/zh/reference/data-directories.mdx @@ -83,6 +83,20 @@ setx OPENIDEA_LOCAL_DATA_DIR "E:\OpenIDEA" - 如果配置或用户数据的路径是相对路径或不可用,OpenIDEA 会显示错误并退出,而不会退回默认位置;日志路径有误只会不写日志文件。 - 已有文件不会自动迁移。想保留设置、插件和历史记录,先退出 OpenIDEA,把原文件夹里的内容复制到新位置。 +## 便携安装 + +不想设置环境变量时,可以在 OpenIDEA 可执行文件旁边放一个 `openidea.properties` 文件(Linux 上放在 AppImage 文件旁边;macOS 上放在 `OpenIDEA.app` 所在的文件夹)。每行设置上面四个变量中的一个;`#` 开头是注释,相对路径按该文件所在的文件夹计算。例如把所有数据放在 U 盘上可执行文件旁的 `data` 文件夹里: + +```properties +OPENIDEA_CONFIG_DIR=data +OPENIDEA_DATA_DIR=data +OPENIDEA_LOCAL_DATA_DIR=data +``` + +- 环境变量里已设置的变量优先于文件。 +- 文件只在启动时读取一次;IDE 会把这些位置传给它启动的进程(如终端)。 +- 其他行会被忽略。 + ## 日志 OpenIDEA 每天写一个日志文件 `openidea.<日期>.log`,保留最近 7 个。程序崩溃(panic)的信息也会写入当前日志。 diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 429e84f27..2cba6531f 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,6 +9,8 @@ fn main() { if let Some(code) = ide_ssh::askpass::run_helper() { std::process::exit(code); } + // A portable install's `openidea.properties` moves the data directories. + ide_host::data_dirs::load_properties_file(); #[cfg(target_os = "macos")] macos_webview::ensure_modern_webkit(); openidea_lib::run()