DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(path): fix path matching and dependency issues caused by Windows verbatim paths
hairyf60e67be679e5
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lockindex fa0cfdde..7a4e762e 100644--- a/src-tauri/Cargo.lock+++ b/src-tauri/Cargo.lock@@ -819,6 +819,7 @@ dependencies = ["arboard","async-trait","base64 0.22.1",+ "dunce","flate2","futures-util","image",diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex 9934502c..08aa3a4a 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -60,6 +60,7 @@ tar = "0.4"async-trait = "0.1"tokio = { version = "1", features = [ "full" ] }semver = "1"+dunce = "1.0.5"[target.'cfg(windows)'.dependencies]rfd = "0.16"diff --git a/src-tauri/src/bridge/guard.rs b/src-tauri/src/bridge/guard.rsindex 4d691935..bb4605a6 100644--- a/src-tauri/src/bridge/guard.rs+++ b/src-tauri/src/bridge/guard.rs@@ -43,16 +43,20 @@ fn existing_roots(app_handle: &AppHandle) -> Vec<PathBuf> {}/// `path` canonicalize 后是否位于任一允许根目录内。+///+/// 用 `dunce::canonicalize`(内部是 std `fs::canonicalize`)把路径与根统一归一到+/// 常规形式(Windows 上 `fs::canonicalize` 会带 `\\?\` verbatim 前缀,dunce 剥掉),+/// 避免某侧带前缀、另一侧不带导致 `starts_with` 边界失配。pub fn is_allowed_path(app_handle: &AppHandle, path: &Path) -> bool {if !path.exists() {return false;}- let Ok(real) = path.canonicalize() else {+ let Ok(real) = dunce::canonicalize(path) else {return false;};existing_roots(app_handle).iter()- .filter_map(|root| root.canonicalize().ok())+ .filter_map(|root| dunce::canonicalize(root).ok()).any(|root| real.starts_with(&root))}diff --git a/src-tauri/src/service/fs_guard.rs b/src-tauri/src/service/fs_guard.rsindex b3947cd5..7cfacc2d 100644--- a/src-tauri/src/service/fs_guard.rs+++ b/src-tauri/src/service/fs_guard.rs@@ -43,15 +43,14 @@ pub fn validate_id(id: &str) -> Result<(), String> {/// 校验 `child` canonicalize 后仍位于 `root` canonicalize 的目录内。///-/// 使用 `canonicalize` 后再比较,避免字符串前缀误判(`/data/foobar` 不作为-/// `/data/foo` 的子路径)以及 `..`、符号链接改写。`root` 不存在时(尚未初始化)-/// 直接拒绝——删除操作的前提是父目录存在。+/// 使用 `dunce::canonicalize`(std `fs::canonicalize` 在 Windows 上返回 `\\?\`+/// verbatim 前缀路径,dunce 把它归一化成常规形式)后再比较,避免字符串前缀误判+/// (`/data/foobar` 不作为 `/data/foo` 的子路径)以及 `..`、符号链接改写。+/// `root` 不存在时(尚未初始化)直接拒绝——删除操作的前提是父目录存在。pub fn ensure_within(child: &Path, root: &Path) -> Result<PathBuf, String> {- let root_real = root- .canonicalize()+ let root_real = dunce::canonicalize(root).map_err(|e| format!("ROOT_RESOLVE_FAILED: {e}"))?;- let child_real = child- .canonicalize()+ let child_real = dunce::canonicalize(child).map_err(|e| format!("PATH_RESOLVE_FAILED: {e}"))?;if !child_real.starts_with(&root_real) {return Err(format!(diff --git a/src-tauri/src/service/plugin/internal.rs b/src-tauri/src/service/plugin/internal.rsindex dd4c8ea9..5a7266c3 100644--- a/src-tauri/src/service/plugin/internal.rs+++ b/src-tauri/src/service/plugin/internal.rs@@ -11,12 +11,11 @@//! 要求——用户怎么卸载、何时卸载都不影响下次启动自动恢复,无需任何用户操作。use std::collections::HashMap;+use std::path::Path;use tauri::{AppHandle, Emitter};use super::installed::{installed_name, profile_dir, ProfilePackageJson};-use super::preset::{- bundled_dep_spec, bundled_plugin_dir, load_presets, strip_verbatim_path, PreinstallPluginInfo,-};+use super::preset::{bundled_dep_spec, bundled_plugin_dir, load_presets, PreinstallPluginInfo};/// 核对并强制安装缺失/路径不正确/被卸载的内置插件,在服务进程启动前调用。///@@ -127,11 +126,16 @@ fn dep_matches_spec(actual: &str, expected: &str) -> bool {.strip_prefix("link:").or_else(|| spec.strip_prefix("file:")).unwrap_or(spec);- // 剥离 Windows 扩展长度路径前缀(`\\?\`,见 preset::strip_verbatim_path):- // 期望值已经由 bundled_dep_spec 归一化掉前缀,若历史命中的实值仍带- // `//?/` 前缀(旧版 or 含 `\\?\` 的联接目标),先归一化再比对,保证幂等- // (避免旧值一次次触发不必要的重装)。- strip_verbatim_path(stripped)+ // 统一用 dunce 归一化 Windows 扩展长度路径前缀(`\\?\`):+ // 期望值已经由 bundled_dep_spec 归一化掉前缀;若历史命中的实值仍带+ // `//?/` / `\\?\` 前缀,先归一再比对,保证幂等(避免旧值一次次触发+ // 不必要的重装)。先把手写正斜杠的 verbatim 形式(`//?/`)换算成反斜杠+ // (dunce 依赖 `\\?\` 识别 verbatim),再交给 dunce::simplified,最后+ // 统一回正斜杠,与 bundled_dep_spec 的产出可比。+ let backslash = stripped.replace('/', "\\");+ dunce::simplified(Path::new(&backslash))+ .to_string_lossy()+ .replace('\\', "/").trim_end_matches('/').to_string()};@@ -173,12 +177,6 @@ mod tests {"file:C:/Apps/dsh/resources/preset-plugins/dsh-tauri",expected));- // 实值仍带 Windows 扩展长度前缀(`\\?\`,见 preset::strip_verbatim_path)- // 与归一化掉前缀的期望值仍视为同一路径(幂等,避免不必要的重装)- assert!(dep_matches_spec(- "link://?/C:/Apps/dsh/resources/preset-plugins/dsh-tauri",- expected- ));}#[test]@@ -201,5 +199,11 @@ mod tests {"link:c:/apps/DSH/resources/preset-plugins/Dsh-Tauri",expected));+ // 实值仍带 Windows 扩展长度前缀(`\\?\`,dunce::simplified 归一化)时,+ // 与归一化掉前缀的期望值仍视为同一路径(幂等,避免不必要的重装)+ assert!(dep_matches_spec(+ "link://?/C:/Apps/dsh/resources/preset-plugins/dsh-tauri",+ expected+ ));}-}\ No newline at end of file+}diff --git a/src-tauri/src/service/plugin/preset.rs b/src-tauri/src/service/plugin/preset.rsindex 476f71d2..2a427dfe 100644--- a/src-tauri/src/service/plugin/preset.rs+++ b/src-tauri/src/service/plugin/preset.rs@@ -163,37 +163,16 @@ fn parse_dev_internal_dir(content: &str) -> Option<PathBuf> {/// 形式把 `link:` 依赖写入 profile 的 package.json;安装(`install.rs`)与启动/// 自愈(`internal.rs`)共用这一规范形比对路径是否正确。///-/// 生成前先剥离 Windows 扩展长度路径前缀(`\\?\` verbatim):`resource_dir()`-/// 在部分 Windows 环境返回 verbatim 形式,直接拼进 `link:` 会得到 `link://?/G:/...`,-/// pnpm 会把 `//?/G:/...` 当作相对路径解析并生成 `..\?\G:\...` 的坏联接-/// (内置插件重装死循环的根因),因此此处统一转成常规绝对路径。+/// 生成前用 `dunce::simplified` 归一化 Windows 扩展长度路径前缀(`\\?\`+/// verbatim):`resource_dir()` 在部分 Windows 环境返回 verbatim 形式,直接+/// 拼进 `link:` 会得到 `link://?/G:/...`,pnpm 会把 `//?/G:/...` 当作相对路径+/// 解析并生成 `..\?\G:\...` 的坏联接(内置插件重装死循环的根因),因此此处+/// 统一转成常规绝对路径。dunce 在非 Windows 平台是 no-op,跨平台安全。pub(crate) fn bundled_dep_spec(dir: &std::path::Path) -> String {- let normalized = strip_verbatim_path(&dir.to_string_lossy());+ let normalized = dunce::simplified(dir).to_string_lossy().replace('\\', "/");format!("link:{}", normalized.trim_end_matches('/'))}-/// 剥离 Windows 扩展长度路径前缀(`\\?\` verbatim)并统一分隔符为正斜杠。-///-/// Windows 文件的 verbatim 路径形如 `\\?\C:\...`(`canonicalize` 或部分环境下-/// `resource_dir()` 会返回该形式)。文件系统 API 对两者等价,但把它写进依赖-/// spec(`link:`)时 pnpm 会误把 `//?/` 当相对路径,必须剥掉,保证 pnpm 按常规-/// 绝对路径解析并建立正确联接。-///-/// - 常规路径:分隔符统一为正斜杠,原样返回;-/// - 盘符 verbatim(`\\?\G:\...`):剥离 `\\?\`;-/// - UNC verbatim(`\\?\UNC\server\share`):转成常规 UNC(`\\server\share`,-/// 即在 `//server/share` 形式下保留双斜杠)。-pub(crate) fn strip_verbatim_path(path: &str) -> String {- let s = path.replace('\\', "/");- if let Some(rest) = s.strip_prefix("//?/UNC/") {- format!("//{rest}")- } else if let Some(rest) = s.strip_prefix("//?/") {- rest.to_string()- } else {- s- }-}-/// 解析预设清单 JSONfn parse_presets(json: &str) -> Result<Vec<PreinstallPluginInfo>, String> {serde_json::from_str(json).map_err(|e| format!("PRESET_PLUGINS_INVALID_JSON: {e}"))@@ -398,11 +377,13 @@ mod tests {);}+ #[cfg(windows)]#[test]fn bundled_dep_spec_strips_verbatim_prefix() {// 回归:Windows 扩展长度路径前缀(`\\?\`)不该进入 link: spec。// `resource_dir()` 返回 verbatim 路径时,未剥离会导致 pnpm 把// `//?/G:/...` 当相对路径解析、生成 `..\?\G:\...` 的坏联接而安装失败。+ // (dunce::simplified 在 Windows 上把 `\\?\` 归一回常规绝对路径)let dir = std::path::Path::new(r"\\?\G:\Deepseek Harness Desktop\resources\preset-plugins\dsh-tauri",);@@ -412,27 +393,6 @@ mod tests {);}- #[test]- fn strip_verbatim_path_variants() {- // 常规路径:仅统一切换分隔符- assert_eq!(- strip_verbatim_path("C:\\Apps\\dsh\\plugins\\x"),- "C:/Apps/dsh/plugins/x"- );- // 盘符 verbatim:剥离 `\\?\`- assert_eq!(- strip_verbatim_path(r"\\?\G:\Apps\dsh\plugins\x"),- "G:/Apps/dsh/plugins/x"- );- // UNC verbatim:转成常规 UNC(保留 `//`)- assert_eq!(- strip_verbatim_path(r"\\?\UNC\server\share\plugins\x"),- "//server/share/plugins/x"- );- // 已是无 verbatim 前缀的(Unix 或正斜杠)原样保留- assert_eq!(strip_verbatim_path("/opt/dsh/plugins/x"), "/opt/dsh/plugins/x");- }-#[cfg(debug_assertions)]#[test]fn dev_internal_dir_parses_plain_and_quoted_values() {diff --git a/src-tauri/src/service/update/install.rs b/src-tauri/src/service/update/install.rsindex b8d62289..0b3068a3 100644--- a/src-tauri/src/service/update/install.rs+++ b/src-tauri/src/service/update/install.rs@@ -309,10 +309,11 @@ pub async fn open_installer(app_handle: &AppHandle, path: String) -> Result<(),if !p.exists() || !p.is_file() {return Err(format!("UPDATE_NOT_FOUND: {path}"));}- // 规范化后必须仍在 updates 目录内(防 `..`、符号链接、路径穿越)- let canonical = p.canonicalize().map_err(|e| format!("UPDATE_OPEN: {e}"))?;- let updates_real = updates_dir- .canonicalize()+ // 规范化后必须仍在 updates 目录内(防 `..`、符号链接、路径穿越)。+ // 用 `dunce::canonicalize`(std `fs::canonicalize`)——它返回的路径不带+ // Windows `\\?\` verbatim 前缀,`starts_with` 与日志展示更一致。+ let canonical = dunce::canonicalize(p).map_err(|e| format!("UPDATE_OPEN: {e}"))?;+ let updates_real = dunce::canonicalize(&updates_dir).map_err(|e| format!("UPDATE_DIR: {e}"))?;if !canonical.starts_with(&updates_real) {log::error!(