Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(data-dir): 修复 Windows 迁移后重启回到旧目录 (#1216)

Hairy1615099054c5
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 88e341f2..7e22ffbc 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -63,7 +63,7 @@ pub fn run() -> Result<(), String> {
"STARTUP_BOOT: name=logger_ready elapsed_ms={}",
started.elapsed().as_millis()
);
- // Unix:把持久化的 DSH_HOME 补回进程环境(issue #871)。必须在桌面端装配之前,
+ // 把持久化的 DSH_HOME 补回进程环境(issue #871、#1195)。必须在桌面端装配之前,
// 否则首次 get_dsh_data_path 会落到默认目录,日志、store 与 shim 全部写错位置。
service::data_dir::restore_process_env();
diff --git a/src-tauri/src/service/cli/mod.rs b/src-tauri/src/service/cli/mod.rs
index 0556890c..54f2337a 100644
--- a/src-tauri/src/service/cli/mod.rs
+++ b/src-tauri/src/service/cli/mod.rs
@@ -31,7 +31,9 @@ pub use core::{ensure, ensure_shims, get_status, remove, CliLinkStatus};
#[cfg(windows)]
pub(crate) use path::find_user_pnpm_executable;
#[cfg(windows)]
-pub(crate) use path::{delete_user_env, notify_environment_change, read_user_env, write_user_env};
+pub(crate) use path::{
+ delete_user_env, expand_env, notify_environment_change, read_user_env, write_user_env,
+};
pub use path::{find_user_pnpm, get_bin_dir, pnpm_env_value};
pub(crate) use path::{find_user_pnpm_from, get_bin_dir_from};
#[cfg(unix)]
diff --git a/src-tauri/src/service/cli/path/mod.rs b/src-tauri/src/service/cli/path/mod.rs
index b0041464..c936a188 100644
--- a/src-tauri/src/service/cli/path/mod.rs
+++ b/src-tauri/src/service/cli/path/mod.rs
@@ -33,7 +33,7 @@ mod registry;
pub(crate) use pnpm::find_user_pnpm_executable;
#[cfg(windows)]
pub(crate) use registry::{
- delete_user_env, notify_environment_change, read_user_env, write_user_env,
+ delete_user_env, expand_env, notify_environment_change, read_user_env, write_user_env,
};
// 数据目录(issue #871)在 Unix 上复用 rc 的「标记块 + 备份写回」原语,
// 免得再抄一份 rc 文件读写实现。rc 模块本身是私有的,跨模块用要在这里再导出。
diff --git a/src-tauri/src/service/cli/path/registry.rs b/src-tauri/src/service/cli/path/registry.rs
index 601d1b43..ec918e23 100644
--- a/src-tauri/src/service/cli/path/registry.rs
+++ b/src-tauri/src/service/cli/path/registry.rs
@@ -221,7 +221,7 @@ pub(crate) fn notify_environment_change() {
}
/// 展开字符串中的 %VAR%(Windows)
-fn expand_env(value: &str) -> String {
+pub(crate) fn expand_env(value: &str) -> String {
use windows_sys::Win32::System::Environment::ExpandEnvironmentStringsW;
let wide = to_wide_null(value);
let mut buf = vec![0u16; 32768];
diff --git a/src-tauri/src/service/data_dir/env.rs b/src-tauri/src/service/data_dir/env.rs
index ab8a4c98..1302ee35 100644
--- a/src-tauri/src/service/data_dir/env.rs
+++ b/src-tauri/src/service/data_dir/env.rs
@@ -38,6 +38,15 @@ pub(super) fn read_user_home() -> Result<Option<String>, String> {
Ok(normalize(crate::service::cli::read_user_env(DATA_DIR_ENV)?))
}
+#[cfg(windows)]
+pub(super) fn restore_process_env(persisted: Result<Option<String>, String>) {
+ match persisted {
+ Ok(Some(value)) => std::env::set_var(DATA_DIR_ENV, crate::service::cli::expand_env(&value)),
+ Ok(None) => {}
+ Err(error) => log::warn!("DATA_DIR_ENV_READ: {error}"),
+ }
+}
+
/// Unix:值可能来自进程环境、~/.profile 的标记块或平台自己的会话文件
/// (macOS LaunchAgent / Linux environment.d),三者由 unix_env 统一处理。
#[cfg(not(windows))]
@@ -200,6 +209,72 @@ fn normalize(value: Option<String>) -> Option<String> {
mod tests {
use super::*;
+ #[cfg(windows)]
+ #[test]
+ fn persisted_home_restores_in_an_isolated_process() {
+ use std::os::windows::process::CommandExt;
+ for mode in [
+ "missing", "stale", "expanded", "unset", "empty", "blank", "default", "error",
+ ] {
+ let mut child = std::process::Command::new(std::env::current_exe().unwrap())
+ .args([
+ "--exact",
+ "service::data_dir::env::tests::persisted_home_restore_child",
+ "--nocapture",
+ ])
+ .env("DSH_TEST_HOME_RESTORE", mode)
+ .env_remove(DATA_DIR_ENV)
+ .creation_flags(0x08000000)
+ .spawn()
+ .unwrap();
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ loop {
+ if let Some(status) = child.try_wait().unwrap() {
+ assert!(status.success(), "home restoration failed: {mode}");
+ break;
+ }
+ if std::time::Instant::now() >= deadline {
+ let _ = child.kill();
+ let _ = child.wait();
+ panic!("home restoration timed out: {mode}");
+ }
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ }
+ }
+
+ #[cfg(windows)]
+ #[test]
+ fn persisted_home_restore_child() {
+ let Ok(mode) = std::env::var("DSH_TEST_HOME_RESTORE") else {
+ return;
+ };
+ let migrated = "E:\\DSH 数据\\DSHHome";
+ let inherited = "D:\\Old Home";
+ std::env::set_var("DSH_TEST_EXPAND_ROOT", "E:\\DSH 数据");
+ let persisted = match mode.as_str() {
+ "missing" | "stale" => Ok(Some(migrated.to_string())),
+ "expanded" => Ok(Some("%DSH_TEST_EXPAND_ROOT%\\DSHHome".to_string())),
+ "empty" => Ok(normalize(Some(String::new()))),
+ "blank" => Ok(normalize(Some(" ".to_string()))),
+ "error" => Err("REG_QUERY_FAILED: isolated failure".to_string()),
+ "unset" | "default" => Ok(None),
+ _ => panic!("unknown home restoration mode: {mode}"),
+ };
+ if mode == "expanded" {
+ std::env::set_var(DATA_DIR_ENV, migrated);
+ } else if mode != "missing" && mode != "default" {
+ std::env::set_var(DATA_DIR_ENV, inherited);
+ }
+ restore_process_env(persisted);
+ let expected = match mode.as_str() {
+ "missing" | "stale" | "expanded" => Some(migrated.into()),
+ "default" => None,
+ _ => Some(inherited.into()),
+ };
+ assert_eq!(std::env::var_os(DATA_DIR_ENV), expected, "mode: {mode}");
+ }
+
#[test]
fn normalize_treats_blank_as_unset() {
assert_eq!(normalize(Some(" ".to_string())), None);
diff --git a/src-tauri/src/service/data_dir/mod.rs b/src-tauri/src/service/data_dir/mod.rs
index 44579ccb..06d62eaa 100644
--- a/src-tauri/src/service/data_dir/mod.rs
+++ b/src-tauri/src/service/data_dir/mod.rs
@@ -29,13 +29,10 @@ mod state;
#[cfg(unix)]
mod unix_env;
-/// 应用启动时把持久化的 `DSH_HOME` 注入本进程(Unix 的实现见 [`unix_env`];
-/// Windows 走注册表,进程启动时已经拿到值,这里是空操作)。
-///
-/// 必须在任何 `config::get_dsh_data_path` 调用之前执行:macOS / Linux 上由
-/// launchd 或显示管理器启动的 GUI 应用拿不到登录 shell 的环境变量,迁移过的
-/// 数据目录只能靠这一步回到进程里,否则会「迁移成功但重启后又读旧目录」。
+/// issue #1195:Windows 启动器可能继承旧环境,须在解析数据目录前恢复持久化值。
pub fn restore_process_env() {
+ #[cfg(windows)]
+ env::restore_process_env(env::read_user_home());
#[cfg(unix)]
unix_env::restore_process_env();
}
diff --git a/test/data-dir.test.ts b/test/data-dir.test.ts
index 920b996c..ffe723ec 100644
--- a/test/data-dir.test.ts
+++ b/test/data-dir.test.ts
@@ -75,6 +75,15 @@ describe('data directory backend contract', () => {
}
})
+ it('restores persisted Windows home before any startup path resolution', () => {
+ const source = readSource('src-tauri/src/lib.rs')
+ const restore = source.indexOf('service::data_dir::restore_process_env();')
+ expect(restore).toBeGreaterThan(-1)
+ expect(restore).toBeLessThan(source.indexOf('service::workflow::early::Startup::begin('))
+ expect(restore).toBeLessThan(source.indexOf('desktop::builder()'))
+ expect(readSource(MODULE)).toMatch(/#\[cfg\(windows\)\]\s*env::restore_process_env\(env::read_user_home\(\)\);/)
+ })
+
it('keeps the user-level DSH_HOME as the single source of truth', () => {
expect(readSource(ENV)).toContain('pub(super) const DATA_DIR_ENV: &str = "DSH_HOME";')
expect(readSource(ENV)).toContain('read_user_env(DATA_DIR_ENV)')