DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(plugins): 升级后彻底卸载已废弃的内部插件
hairyf01cfeb8ce80e
diff --git a/src-tauri/resources/deprecated-plugins.json b/src-tauri/resources/deprecated-plugins.jsonindex faacf8ee..0d815d08 100644--- a/src-tauri/resources/deprecated-plugins.json+++ b/src-tauri/resources/deprecated-plugins.json@@ -1,5 +1,6 @@["dsh-win-terminal-inspector","dsh-session-context-menu",- "dsh-notification"+ "dsh-notification",+ "dsh-tauri-panel"]diff --git a/src-tauri/src/service/plugin/install/single.rs b/src-tauri/src/service/plugin/install/single.rsindex 3b2ab6c6..afc155b7 100644--- a/src-tauri/src/service/plugin/install/single.rs+++ b/src-tauri/src/service/plugin/install/single.rs@@ -145,29 +145,58 @@ pub async fn remove(app_handle: &AppHandle, id: &str) -> Result<(), String> {/// 计算需要自动卸载的弃用插件已安装包名(纯函数,便于单测)。////// 命中条件:id 登记在弃用清单(`resources/deprecated-plugins.json`,见-/// [`super::super::preset::load_deprecated_ids`])、非内部插件(内部插件由启动-/// 自愈强制安装,不适用弃用语义),且当前已安装(以实际 npm 包名 `installed_name`-/// 为准)。返回实际安装包名——离线卸载(`uninstall_recovery`)以它为键从 profile-/// 清单与 `node_modules` 精准移除(scoped 包名与预设 id 不一致时也能正确卸载)。+/// [`super::super::preset::load_deprecated_ids`])且当前已安装。返回实际安装包名+/// ——离线卸载(`uninstall_recovery`)以它为键从 profile 清单与 `node_modules`+/// 精准移除(scoped 包名与预设 id 不一致时也能正确卸载)。+///+/// 两条来源缺一不可:+/// 1. 预设仍声明该 id:以 `installed_name`(实际 npm 包名)为准;内部插件由启动+/// 自愈强制安装,不适用弃用语义,跳过。+/// 2. 兜底——预设清单已不存在该 id(被整体删除,如 `dsh-tauri-panel` 并入核心后+/// 从 `internal-plugins.json` 移除):此时按弃用清单登记的 id 本身核对。少了+/// 这一步,弃用名单对「连预设条目一起删掉」的插件永远无效,其 `link:` 目标还会+/// 因安装目录残留而躲过失效链接清理,残留 bundle 直接拖垮启动。fn deprecated_installed_names(presets: &[PreinstallPluginInfo],deprecated_ids: &HashSet<String>,installed: impl Fn(&str) -> bool,) -> Vec<String> {- presets+ let mut names: Vec<String> = presets.iter()- .filter(|p| deprecated_ids.contains(&p.id) && !p.internal)- .filter(|p| installed(installed_name(p)))+ .filter(|p| deprecated_ids.contains(&p.id) && !p.internal && installed(installed_name(p))).map(|p| installed_name(p).to_string())- .collect()+ .collect();+ for id in deprecated_ids {+ if presets.iter().any(|p| p.id == *id) {+ continue;+ }+ if installed(id) {+ names.push(id.clone());+ }+ }+ names.sort();+ names.dedup();+ names+}++/// 弃用插件在 profile 中的残留判定:清单引用或 `node_modules` 入口。+///+/// 入口用 `symlink_metadata` 判定:junction / 符号链接的目标目录消失时 `exists()`+/// 返回 false,但入口本身仍是 profile 解析路径上的残留,必须能被识别并清掉+/// (`uninstall_recovery` 对已清空清单的 id 幂等,只清入口与 patch 层)。+fn deprecated_residue_present(app_handle: &AppHandle, name: &str) -> bool {+ is_installed(app_handle, name)+ || std::fs::symlink_metadata(profile_dir(app_handle).join("node_modules").join(name))+ .is_ok()}/// 启动时自动卸载弃用清单(`resources/deprecated-plugins.json`)登记的插件。///-/// 弃用是发布侧决策:某个社区插件下架/被替换后,把它的 id 追加进弃用清单,桌面端+/// 弃用是发布侧决策:某个插件下架/被替换后,把它的 id 追加进弃用清单,桌面端/// 每次启动核对「已安装 → 自动卸载」,无需用户手动处理,也避免残留插件继续在-/// profile 里加载破坏启动。仅处理社区预设;未被引用的条目跳过。已卸载/未安装的-/// 插件跳过,绝不误伤其它插件。+/// profile 里加载破坏启动。清单是唯一依据:条目已从预设清单整体删除时(被并入+/// 核心、从随包清单移除)同样按 id 卸载,这是升级残留的兜底。未安装(清单未引用+/// 且 `node_modules` 无入口)的条目跳过,绝不误伤其它插件。////// 启动阶段走离线精准卸载(`uninstall_recovery`):不依赖 node/pnpm/窗口,即使/// 插件产物已损坏也能移除,也不会触发服务停止或 pnpm 下载。最佳努力:任何失败@@ -176,7 +205,7 @@ pub(crate) async fn uninstall_deprecated_plugins(app_handle: &AppHandle) -> Resulet presets = load_presets(app_handle);let deprecated_ids = load_deprecated_ids(app_handle);let names = deprecated_installed_names(&presets, &deprecated_ids, |name| {- is_installed(app_handle, name)+ deprecated_residue_present(app_handle, name)});if names.is_empty() {return Ok(());@@ -377,39 +406,42 @@ mod tests {#[test]fn deprecated_installed_only_picks_marked_and_installed() {- // 弃用清单只登记 dsh-ok / dsh-scoped / dsh-not-installed 三个 id;- // 只把 `dsh-ok` 视为已安装,其余一律未安装(便于区分「登记但未安装」)。+ // 三个弃用条目都在预设清单里声明(预设驱动的常规路径),逐个区分命中。let deprecated: HashSet<String> = ["dsh-ok", "dsh-scoped", "dsh-not-installed"].into_iter().map(String::from).collect();- let installed = |name: &str| matches!(name, "dsh-ok" | "@scope/deprecated");+ let mut scoped = preset("dsh-scoped", "github:x/y", false);+ scoped.package = Some("@scope/deprecated".into());+ let declared = vec![+ preset("dsh-ok", "dshmarket", false),+ scoped,+ preset("dsh-not-installed", "dshmarket", false),+ ];// 命中:登记且已安装 → 返回实际安装包名(dsh-ok 未声明 package,回落 id)+ let only_ok = |name: &str| name == "dsh-ok";assert_eq!(- deprecated_installed_names(- &[preset("dsh-ok", "dshmarket", false)],- &deprecated,- installed- ),+ deprecated_installed_names(&declared, &deprecated, only_ok),vec!["dsh-ok".to_string()]);// scoped 包:返回真实安装包名(与预设 id 不一致)- let mut scoped = preset("dsh-scoped", "github:x/y", false);- scoped.package = Some("@scope/deprecated".into());+ let only_scoped = |name: &str| name == "@scope/deprecated";assert_eq!(- deprecated_installed_names(&[scoped], &deprecated, installed),+ deprecated_installed_names(&declared, &deprecated, only_scoped),vec!["@scope/deprecated".to_string()]);+ // 登记了但未安装:不命中+ assert!(deprecated_installed_names(&declared, &deprecated, |_| false).is_empty());+// 未登记弃用:即使已安装也不命中let plain = preset("dsh-plain", "dsh-plain", false);- assert!(deprecated_installed_names(&[plain], &deprecated, installed).is_empty());-- // 登记了但未安装:不命中- let absent = preset("dsh-not-installed", "dshmarket", false);- assert!(deprecated_installed_names(&[absent], &deprecated, installed).is_empty());+ assert!(+ deprecated_installed_names(&[plain], &deprecated, |name| name == "dsh-plain")+ .is_empty()+ );// 内部插件即使登记弃用也不命中(内部插件由启动自愈强制安装)let internal = preset("dsh-internal", "[email protected]", true);@@ -422,6 +454,34 @@ mod tests {);}+ #[test]+ fn deprecated_installed_falls_back_when_preset_entry_removed() {+ // 弃用条目已从预设清单整体删除(dsh-tauri-panel 并入核心后从+ // internal-plugins.json 移除):预设驱动的循环命中不到,兜底按 id 卸载。+ let removed: HashSet<String> = ["dsh-tauri-panel"].into_iter().map(String::from).collect();++ assert_eq!(+ deprecated_installed_names(&[], &removed, |name| name == "dsh-tauri-panel"),+ vec!["dsh-tauri-panel".to_string()]+ );++ // 清单里根本没有弃用条目:即使同名包已安装也不命中+ assert!(deprecated_installed_names(+ &[],+ &HashSet::new(),+ |name| name == "dsh-tauri-panel"+ )+ .is_empty());++ // 仍被预设声明(内部插件由自愈强制安装):不走兜底,避免与自愈互相拉扯+ assert!(deprecated_installed_names(+ &[preset("dsh-tauri-panel", "[email protected]", true)],+ &removed,+ |name| name == "dsh-tauri-panel"+ )+ .is_empty());+ }+// ---- 升级参数与「假成功」核验 ----#[test]diff --git a/src-tauri/src/service/plugin/preset.rs b/src-tauri/src/service/plugin/preset.rsindex b04454b6..e86262cd 100644--- a/src-tauri/src/service/plugin/preset.rs+++ b/src-tauri/src/service/plugin/preset.rs@@ -669,6 +669,18 @@ mod tests {assert!(ids.contains("dsh-session-context-menu"));}+ #[test]+ fn deprecated_manifest_lists_absorbed_panel() {+ // 0.15.0 把 dsh-tauri-panel 并入核心,并从 internal-plugins.json 移除了条目,+ // 升级用户只能靠弃用清单兜底卸载(残留 bundle 会导致无法进入软件页面)。+ let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))+ .join("resources")+ .join(DEPRECATED_PLUGINS_FILE);+ let raw = std::fs::read_to_string(path).expect("deprecated manifest should exist");+ let ids = parse_deprecated_ids(&raw).expect("deprecated manifest should be valid JSON");+ assert!(ids.contains("dsh-tauri-panel"));+ }+#[test]fn manifest_source_overrides_internal_field() {let raw =diff --git a/src-tauri/src/service/plugin/recovery/uninstall.rs b/src-tauri/src/service/plugin/recovery/uninstall.rsindex dbdaff0e..812bb921 100644--- a/src-tauri/src/service/plugin/recovery/uninstall.rs+++ b/src-tauri/src/service/plugin/recovery/uninstall.rs@@ -53,6 +53,11 @@ fn remove_plugin_entry(entry: &Path) -> std::io::Result<()> {}/// 删除 `node_modules/<id>`;scoped 目录删除后若 scope 空则一并清理。+///+/// 链接入口(符号链接 / Windows junction)只校验父目录、只解链本身:内置插件以+/// `link:` 指向应用资源目录(在 profile 之外),`canonicalize` 会解析到 profile+/// 外并被误判为路径逃逸,导致入口永远删不掉(弃用/孤儿/失效链接卸载的残留根因)。+/// 链接的删除动作不触碰目标,父目录校验已足以保证删除范围仍在 `node_modules` 内。pub(super) fn remove_plugin_dir(profile: &Path, id: &str) {let node_modules = profile.join("node_modules");let Ok(node_modules_root) = fs_guard::ensure_within(&node_modules, profile) else {@@ -66,15 +71,20 @@ pub(super) fn remove_plugin_dir(profile: &Path, id: &str) {let Ok(entry_metadata) = fs::symlink_metadata(&entry) else {return;};- let entry_is_dangling_symlink = entry_metadata.file_type().is_symlink() && !entry.exists();- if entry_is_dangling_symlink {- // 悬空链接无法规范化目标,只验证父目录,确保删除的仍是 node_modules 内入口。+ let file_type = entry_metadata.file_type();+ // junction 在 Rust 中同样满足 `is_symlink()`;两个判定都保留,避免行为差异+ // 下把重解析点当成普通目录而递归删进应用资源目录。+ #[cfg(windows)]+ let entry_is_link = file_type.is_symlink() || file_type.is_symlink_dir();+ #[cfg(not(windows))]+ let entry_is_link = file_type.is_symlink();+ if entry_is_link {let Some(parent) = entry.parent() else {return;};if let Err(e) = fs_guard::ensure_within(parent, &node_modules_root) {log::warn!(- "refusing to remove dangling plugin symlink outside node_modules: {} ({e})",+ "refusing to remove plugin link outside node_modules: {} ({e})",entry.display());return;@@ -229,6 +239,46 @@ mod tests {let _ = std::fs::remove_dir_all(root);}+ #[cfg(windows)]+ #[test]+ fn remove_plugin_dir_unlinks_junction_pointing_outside_profile() {+ // 内置插件以 link: 指向应用资源目录(在 profile 之外,如 dsh-tauri-panel):+ // junction 的 canonicalize 落在 profile 外,仍必须只解链入口、绝不动目标。+ let root =+ std::env::temp_dir().join(format!("dsh-plugin-recovery-junction-{}", std::process::id()));+ let profile = root.join("profile");+ let target = root.join("app-resources/dsh-tauri-panel");+ let entry = profile.join("node_modules/dsh-tauri-panel");+ let _ = std::fs::remove_dir_all(&root);+ std::fs::create_dir_all(&target).unwrap();+ std::fs::create_dir_all(entry.parent().unwrap()).unwrap();+ std::fs::write(target.join("package.json"), "{}").unwrap();++ use std::os::windows::process::CommandExt;+ let created = std::process::Command::new("cmd")+ .arg("/C")+ .raw_arg(format!(+ "mklink /J \"{}\" \"{}\"",+ entry.display(),+ target.display()+ ))+ .status()+ .expect("mklink should run");+ assert!(created.success(), "junction creation should succeed");++ remove_plugin_dir(&profile, "dsh-tauri-panel");++ assert!(+ std::fs::symlink_metadata(&entry).is_err(),+ "junction entry should be unlinked"+ );+ assert!(+ target.join("package.json").is_file(),+ "junction target must remain"+ );+ let _ = std::fs::remove_dir_all(root);+ }+#[cfg(unix)]#[test]fn remove_plugin_dir_rejects_node_modules_symlink_escape() {