DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(preinstall): rewrite workspace/patch YAML via serde_yaml
hairyff25ec0fd7f52
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lockindex 22be7078..73837d9d 100644--- a/src-tauri/Cargo.lock+++ b/src-tauri/Cargo.lock@@ -825,6 +825,7 @@ dependencies = ["rfd","serde","serde_json",+ "serde_yaml","sha2","tar","tauri",@@ -3824,6 +3825,19 @@ dependencies = ["syn 2.0.119",]+[[package]]+name = "serde_yaml"+version = "0.9.34+deprecated"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"+dependencies = [+ "indexmap 2.14.0",+ "itoa",+ "ryu",+ "serde",+ "unsafe-libyaml",+]+[[package]]name = "serialize-to-javascript"version = "0.1.2"@@ -5039,6 +5053,12 @@ version = "1.13.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"+[[package]]+name = "unsafe-libyaml"+version = "0.2.11"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"+[[package]]name = "untrusted"version = "0.9.0"diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex 2e14cbd7..8aea4c07 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -34,6 +34,7 @@ tauri-plugin-notification = "2"image = { version = "0.25", default-features = false, features = [ "png" ] }serde = { version = "1", features = [ "derive" ] }serde_json = "1"+serde_yaml = "0.9"sha2 = "0.10"log = "0.4"reqwest = { version = "0.12", features = [diff --git a/src-tauri/src/service/plugin/install.rs b/src-tauri/src/service/plugin/install.rsindex 37c36aff..c6e26d9c 100644--- a/src-tauri/src/service/plugin/install.rs+++ b/src-tauri/src/service/plugin/install.rs@@ -18,6 +18,7 @@ use crate::service::workflow;use std::collections::HashMap;use std::ffi::OsString;use std::path::PathBuf;+use serde_yaml::{Mapping, Value};use tauri::{AppHandle, Emitter, Manager, WebviewWindow};use super::installed::{PREINSTALL_PROFILE, profile_dir};@@ -316,10 +317,18 @@ fn profile_workspace_path(app_handle: &AppHandle) -> PathBuf {/// 把新的 `allowBuilds` 键合并写回 profile 的 `pnpm-workspace.yaml`。///-/// dsh 的 `initProfile` 仅在文件缺失时创建(其模板无 `allowBuilds`),因此桌面端-/// 自行维护该块:缺失时按 dsh 模板补建基础设置并追加 `allowBuilds`;已有时按键-/// 去重合并。git depPath 键含 `@`/`/`/`:`/`#`,按 YAML 单引号标量写入避免误解析;-/// `false` 不应出现于此(我们只放行)。重复写入同一键是无害的(幂等)。+/// 用 YAML 库(serde_yaml)整体改写而非字符串拼接,避免格式错乱:+/// - 键(git depPath 含 `@`/`/`/`:`/`#`)由库自动按需加引号,不再手工拼;+/// - 已存在的同名键会被就地覆盖,不会残留占位值。+///+/// TODO(v1): 移除对旧版损坏文件(issue #49)的自愈逻辑。v1 起只解析干净配置,+/// `apply_allow_build_keys` 中解析失败后的「同键去重再解析」与+/// `collapse_allow_builds_duplicates` 一并删除。+///+/// 防御性修复:旧版本用字符串拼接可能留下「重复映射键」的损坏文件+/// (最多见的是 `node-pty: set this to true or false` 占位行与真正的+/// `'node-pty': true` 并存,见 issue #49)。此处解析失败时先做一次+/// `allowBuilds` 同键去重再解析,把损坏文件自愈回合法 YAML。fn add_allow_build_keys(app_handle: &AppHandle, keys: &[String]) -> Result<(), String> {let path = profile_workspace_path(app_handle);let dir = path@@ -327,62 +336,154 @@ fn add_allow_build_keys(app_handle: &AppHandle, keys: &[String]) -> Result<(), S.ok_or("PREINSTALL_BAD_PROFILE_DIR: no profile dir")?;std::fs::create_dir_all(dir).map_err(|e| format!("PREINSTALL_MKDIR: {e}"))?;- let mut content = if path.exists() {+ let content = if path.exists() {std::fs::read_to_string(&path).map_err(|e| format!("PREINSTALL_READ_WORKSPACE: {e}"))?} else {- // 与 dsh `initProfile` 生成的基础模板保持一致(尚无 allowBuilds)+ // 与 dsh `initProfile` 生成的基础模板保持一致(尚无 allowBuilds)。"packages:\n - .\n\nnodeLinker: hoisted\nautoInstallPeers: false\n".to_string()};- let has_allow_builds = content- .lines()- .any(|l| l.trim_start().starts_with("allowBuilds:"));- if !has_allow_builds {- if !content.ends_with('\n') {- content.push('\n');- }- content.push_str("allowBuilds:\n");- }-- // 收集已有 ` <key>: true` 条目(含单引号形式),避免重复。基础模板里- // 的 `packages`/`nodeLinker`/`autoInstallPeers` 等行不会以 `: true` 结尾,- // 天然被排除。- let existing: Vec<String> = content- .lines()- .filter_map(|l| {- let trimmed = l.trim_start();- if trimmed.len() == l.len() {- return None; // 非缩进行(顶层键)不参与- }- let suffix = trimmed.strip_suffix(": true")?;- let key = suffix.trim().trim_matches(['\'', '"']);- if key.is_empty() || key.contains(':') {- return None;+ let rendered = apply_allow_build_keys(&content, keys)?;+ if rendered == content {+ return Ok(()); // 无变化(所有键已就位),避免无意义写盘+ }++ log::info!("pnpm-workspace.yaml rewritten with allowBuilds {keys:?} at {}", path.display());+ std::fs::write(&path, rendered).map_err(|e| format!("PREINSTALL_WRITE_WORKSPACE: {e}"))+}++/// 把新的 `allowBuilds` 键合并进 `pnpm-workspace.yaml` 文本并返回新文本。+///+/// 用 YAML 库(serde_yaml)整体改写而非字符串拼接,避免格式错乱:+/// - 键(git depPath 含 `@`/`/`/`:`/`#`)由库自动按需加引号,不再手工拼;+/// - 已存在的同名键会被就地覆盖为 `true`,不会残留占位值,也不会产生重复键。+///+/// 防御性修复:旧版本用字符串拼接可能留下「重复映射键」的损坏文件+/// (最多见的是 `node-pty: set this to true or false` 占位行与真正的+/// `'node-pty': true` 并存,见 issue #49)。此处先尝试严格解析;解析失败时+/// 做一次 `allowBuilds` 同键去重再解析,把损坏文件自愈回合法 YAML。+fn apply_allow_build_keys(content: &str, keys: &[String]) -> Result<String, String> {+ // 先尝试严格解析。旧的损坏文件(重复映射键)严格解析会失败:+ // 把 `allowBuilds` 内同名键去重(保留最后写入的值)后再解析,自愈损坏状态。+ let mut repaired = false;+ let mut doc: Value = match serde_yaml::from_str(content) {+ Ok(v) => v,+ Err(first_err) => {+ let normalized = collapse_allow_builds_duplicates(content);+ if normalized == content {+ return Err(format!(+ "PREINSTALL_WORKSPACE_INVALID_YAML: {first_err}"+ ));}- Some(key.to_string())- })- .collect();+ repaired = true;+ serde_yaml::from_str(&normalized).map_err(|e| {+ format!("PREINSTALL_WORKSPACE_INVALID_YAML: {e}")+ })?+ }+ };++ // 空/注释-only 内容解析为 `Value::Null`,视为全新空配置(pnpm-workspace.yaml+ // 可加载的最小映射);其余非映射内容才是真正的损坏。+ if doc.is_null() {+ doc = Value::Mapping(Mapping::new());+ }++ let map = doc.as_mapping_mut().ok_or_else(|| {+ "PREINSTALL_WORKSPACE_NOT_MAP: pnpm-workspace.yaml must be a mapping".to_string()+ })?;++ let allow_key = Value::String("allowBuilds".to_string());+ if !map.contains_key(&allow_key) {+ map.insert(allow_key.clone(), Value::Mapping(Mapping::new()));+ }+ let allow_builds = map+ .get_mut(&allow_key)+ .and_then(Value::as_mapping_mut)+ .ok_or_else(|| {+ "PREINSTALL_WORKSPACE_ALLOWBUILDS_NOT_MAP: allowBuilds must be a mapping".to_string()+ })?;let mut dirty = false;for key in keys {- if existing.iter().any(|k| k == key) {- continue;+ let k = Value::String(key.clone());+ if allow_builds.get(&k) == Some(&Value::Bool(true)) {+ continue; // 已是 true,幂等跳过}- // 单引号包裹键:git depPath 含 `:`/`#`/`@`,裸写会让 YAML 误解析- content.push_str(&format!(" '{}': true\n", key.replace('\'', "''")));+ // 直接覆盖旧值(含占位值/旧 false),由库负责按需加引号+ allow_builds.insert(k, Value::Bool(true));dirty = true;}- if !dirty {- return Ok(());+ if !dirty && !repaired {+ return Ok(content.to_string());+ }+ // 有键新增,或损坏文件已被自愈归一化——两种都要落回解析后的完整文档,+ // 否则会把损坏的原始文本原样返回。++ serde_yaml::to_string(&doc).map_err(|e| format!("PREINSTALL_WORKSPACE_RENDER: {e}"))+}++/// 把损坏的 `allowBuilds` 映射(同一键出现多次)去重为合法 YAML。+///+/// 仅作为旧版字符串拼接遗留损坏(重复映射键,见 issue #49)的兜底归一化:+/// 扫描 `allowBuilds:` 之后、下一个顶层键之前的缩进 `key: value` 行,同一键+/// 只保留最后一次出现的行(与 YAML「后者覆盖前者」语义一致),其余行原样保留。+fn collapse_allow_builds_duplicates(content: &str) -> String {+ let lines: Vec<&str> = content.lines().collect();+ let mut in_allow = false;+ // 记录(键 → 该键所有行的索引),用于去重+ let mut key_indexes: HashMap<String, Vec<usize>> = HashMap::new();+ let mut order: Vec<String> = Vec::new();++ for (idx, line) in lines.iter().enumerate() {+ let trimmed = line.trim_start();+ if trimmed == "allowBuilds:" {+ in_allow = true;+ continue;+ }+ if in_allow {+ let is_indent = line.starts_with(' ') || line.starts_with('\t');+ let is_comment = trimmed.starts_with('#');+ if !is_indent || is_comment {+ in_allow = false; // 遇到顶层键或注释即离开 allowBuilds+ continue;+ }+ // 缩进的 `key: value` 行 → 提取键(冒号前)+ if let Some(col) = trimmed.find(':') {+ let key = trimmed[..col].trim().trim_matches(['\'', '"']);+ if !key.is_empty() {+ if !key_indexes.contains_key(key) {+ order.push(key.to_string());+ }+ key_indexes.entry(key.to_string()).or_default().push(idx);+ }+ }+ }}- std::fs::write(&path, content).map_err(|e| format!("PREINSTALL_WRITE_WORKSPACE: {e}"))+ // 每个键只保留最后一个出现行,其余标记删除+ let mut keep: std::collections::HashSet<usize> = std::collections::HashSet::new();+ for key in &order {+ if let Some(idxs) = key_indexes.get(key) {+ if let Some(&last) = idxs.last() {+ keep.insert(last);+ }+ }+ }+ let mut out: Vec<&str> = Vec::with_capacity(lines.len());+ for (idx, line) in lines.iter().enumerate() {+ if key_indexes.values().any(|v| v.contains(&idx)) && !keep.contains(&idx) {+ continue; // 是被去重掉的重复键行+ }+ out.push(line);+ }+ // 避免重复键里夹带的空行粘连成异常空行:去掉去重区(allowBuilds 段)的连续空行+ out.join("\n")}#[cfg(test)]mod tests {- use super::{extract_allow_line_key, parse_allowlist_keys};+ use super::{apply_allow_build_keys, collapse_allow_builds_duplicates, extract_allow_line_key, parse_allowlist_keys};#[test]fn parse_git_dep_path_key() {@@ -420,4 +521,89 @@ allowBuilds:assert_eq!(extract_allow_line_key("packages:"), None);assert_eq!(extract_allow_line_key("allowBuilds:"), None);}++ // ---- 归并写回 pnpm-workspace.yaml(issue #49 回归)----++ /// 从渲染结果里解析出单一 `allowBuilds` 映射,便于断言。+ fn allow_builds_map(yaml: &str) -> serde_yaml::Mapping {+ let doc: serde_yaml::Value = serde_yaml::from_str(yaml).expect("output must be valid YAML");+ doc.get("allowBuilds")+ .and_then(serde_yaml::Value::as_mapping)+ .expect("allowBuilds must be a mapping")+ .clone()+ }++ #[test]+ fn apply_adds_new_key_when_absent() {+ let base = "packages:\n - .\n\nnodeLinker: hoisted\nautoInstallPeers: false\n";+ // 无 allowBuilds 时首次写入+ let out = apply_allow_build_keys(base, &["node-pty".to_string()]).unwrap();+ let map = allow_builds_map(&out);+ assert_eq!(map.get("node-pty"), Some(&serde_yaml::Value::Bool(true)));+ // 顶级基础设置被保留+ let doc: serde_yaml::Value = serde_yaml::from_str(&out).unwrap();+ assert!(doc.get("packages").is_some());+ assert!(doc.get("nodeLinker").is_some());+ }++ #[test]+ fn apply_is_idempotent_and_does_not_duplicate() {+ // 已放行的键再次写入:结果不变(幂等、不产生重复键)+ let base = "packages:\n - .\nnodeLinker: hoisted\nautoInstallPeers: false\nallowBuilds:\n node-pty: true\n";+ let out = apply_allow_build_keys(base, &["node-pty".to_string()]).unwrap();+ assert_eq!(out, base);+ }++ #[test]+ fn apply_quotes_git_dep_path_keys() {+ let dep = "dsh-better-sidebar@git+ssh://[email protected]/omdsh-dev/DSH-better-sidebar.git#6c89".to_string();+ // 空内容也能生成合法配置+ let out = apply_allow_build_keys("", &[dep.clone()]).unwrap();+ let map = allow_builds_map(&out);+ assert_eq!(map.get(&serde_yaml::Value::String(dep)), Some(&serde_yaml::Value::Bool(true)));+ // 库负责正确加引号,键原样(含 @ / : / #)可回读+ let doc: serde_yaml::Value = serde_yaml::from_str(&out).unwrap();+ assert_eq!(+ doc["allowBuilds"][&serde_yaml::Value::String(+ "dsh-better-sidebar@git+ssh://[email protected]/omdsh-dev/DSH-better-sidebar.git#6c89".to_string()+ )],+ serde_yaml::Value::Bool(true)+ );+ }++ #[test]+ fn apply_overwrites_placeholder_value_without_duplicate() {+ // 关键回归:旧版字符串拼接可能留下占位键 `node-pty: set this to true or false`+ // 与真实键并存。若解析保留重复键,或解析失败被去重兜底,最终都必须只保留+ // 一个 `node-pty: true`(不允许重复映射键)。+ let corrupted =+ "allowBuilds:\n 'dsh-better-sidebar@https://code...': true\n node-pty: set this to true or false\n 'node-pty': true\n";+ let out = apply_allow_build_keys(corrupted, &["node-pty".to_string()]).unwrap();+ let map = allow_builds_map(&out);+ // 恰好只有一个 node-pty 键,值是 true(覆盖了占位值)+ assert_eq!(map.get("node-pty"), Some(&serde_yaml::Value::Bool(true)));+ // 序列化后全局不允许再出现“重复键”的等价行(node-pty 只出现一次)+ let node_pty_keys = out+ .lines()+ .filter(|l| l.trim_start().starts_with("node-pty") || l.trim_start().starts_with("'node-pty'"))+ .count();+ assert_eq!(node_pty_keys, 1);+ }++ #[test]+ fn collapse_dedupes_allow_builds_keys() {+ let corrupted =+ "packages:\n - .\nallowBuilds:\n node-pty: set this to true or false\n 'node-pty': true\n keep: true\n";+ let normalized = collapse_allow_builds_duplicates(corrupted);+ // 重复的 node-pty 只剩最后一个(值 true),同键不再重复+ let node_pty = normalized+ .lines()+ .filter(|l| l.trim_start().starts_with("node-pty") || l.trim_start().starts_with("'node-pty'"))+ .count();+ assert_eq!(node_pty, 1);+ assert!(normalized.contains("keep"));+ // 去重结果必须是合法 YAML,且能被后续解析+ let out = apply_allow_build_keys(&normalized, &["node-pty".to_string()]).unwrap();+ assert_eq!(allow_builds_map(&out).get("node-pty"), Some(&serde_yaml::Value::Bool(true)));+ }}diff --git a/src-tauri/src/service/workflow/win_inspector.rs b/src-tauri/src/service/workflow/win_inspector.rsindex d4f7546a..c62a490c 100644--- a/src-tauri/src/service/workflow/win_inspector.rs+++ b/src-tauri/src/service/workflow/win_inspector.rs@@ -106,30 +106,27 @@ mod imp {}/// 幂等地写入 web profile 的 `cordis.patch.yml` 挂载行。+ ///+ /// 用 YAML 库整体改写顶层数组,而非字符串拼接:新增的顶层 `- insert:`+ /// 元素由库序列化,避免手拼格式错乱。**代价**:库往返会丢弃文件中的用户+ /// 注释(评审已确认接受),但顶层数组语义保持不变(loader 只读数组结构)。fn ensure_patch(profile: &Path) -> Result<(), String> {let patch_path = profile.join("cordis.patch.yml");let existing = fs::read_to_string(&patch_path).unwrap_or_default();+ let mut doc = parse_patch_list(&existing)?;+ let seq = match &mut doc {+ serde_yaml::Value::Sequence(seq) => seq,+ _ => unreachable!("parse_patch_list only returns a sequence"),+ };// 已挂载则跳过(幂等)。- if existing.contains(PATCH_MARKER) {+ if seq.iter().any(block_is_ours) {return Ok(());}+ seq.push(plugin_insert_entry());- // dsh 可能以“流式空列表 `[]` + 注释头”初始化该文件;块式条目直接追到- // `[]` 后面会产生非法 YAML(“end of the stream or a document separator- // is expected”),因此先把单独成行的 `[]` 去掉再追加。- let cleaned = existing- .lines()- .filter(|line| line.trim() != "[]")- .collect::<Vec<_>>()- .join("\n");-- let mut out = cleaned;- if !out.trim().is_empty() && !out.ends_with('\n') {- out.push('\n');- }- out.push_str(PATCH_ENTRY);-+ let out = serde_yaml::to_string(&doc)+ .map_err(|e| format!("PATCH_RENDER_FAILED: {e}"))?;write_file(&patch_path, &out).map_err(|e| format!("PATCH_WRITE_FAILED: {e}"))}@@ -137,12 +134,11 @@ mod imp {////// 场景:插件经 `dsh plugin remove` 卸载后,我们写入的挂载行不会随依赖被清掉,/// loader 会去挂载一个不存在的包(`Cannot find package`)导致 harness 启动/热加载- /// 报错。因此在「插件未装入」时把对应的顶层 `- insert:` 条目整块删掉,其余条目- /// 与注释原样保留。无该行时无操作。+ /// 报错。因此在「插件未装入」时把顶层数组中属于本插件的条目整块删掉,其余条目+ /// 原样保留。无该块时无操作。///- /// 自愈保证:若删掉的块是文件里唯一的实际内容(剩下只有注释/空行),补写 `[]`——- /// 纯注释的 YAML 解析为 `null`,`parsePatchList` 会抛「必须是顶层数组」直接崩掉- /// 启动;prune 必须保证输出始终是 loader 可加载的顶层数组。+ /// 自愈保证:删除后若数组为空,序列化结果自然是 `[]`(而非纯注释/空——那是+ /// YAML `null`,`parsePatchList` 会抛「必须是顶层数组」直接崩掉启动)。fn prune_patch_if_uninstalled(profile: &Path) -> Result<(), String> {let patch_path = profile.join("cordis.patch.yml");let existing = match fs::read_to_string(&patch_path) {@@ -150,90 +146,87 @@ mod imp {Err(_) => return Ok(()), // 无 patch 文件则无需清理};- if !existing.contains(PATCH_MARKER) {+ let doc = match parse_patch_list(&existing) {+ Ok(d) => d,+ Err(_) => return Ok(()), // 无法解析为数组则不动原文件+ };+ let serde_yaml::Value::Sequence(seq) = doc else {+ return Ok(());+ };+ if !seq.iter().any(block_is_ours) {return Ok(());}- // 逐行扫描,删掉「顶层 `- insert:` 且其缩进子块内含 PATCH_MARKER」的那一段。- // 顶层条目以行首(无缩进)的 `- ` 开头为界;其后续缩进行归属同一块。- let mut lines: Vec<&str> = existing.lines().collect();- let mut i = 0usize;- while i < lines.len() {- let is_top_level = !lines[i].starts_with(char::is_whitespace)- && lines[i].trim_start().starts_with("- ");- if !is_top_level {- i += 1;- continue;- }- // 收集该顶层条目的块(自身 + 后续缩进行)- let block: Vec<&str> = {- let mut b = vec![lines[i]];- let mut j = i + 1;- while j < lines.len() {- let is_indent = lines[j].starts_with(' ') || lines[j].starts_with('\t');- let is_comment = lines[j].trim_start().starts_with('#');- if is_indent && !is_comment {- b.push(lines[j]);- j += 1;- } else {- break;- }- }- b- };- // 是否为我们的 install 块- let is_ours = block.iter().any(|l| l.contains(PATCH_MARKER));- if is_ours {- // 删除块(含尾部空行),保持其余内容与注释完整- let remove_start = i;- let remove_end = i + block.len();- lines.drain(remove_start..remove_end);- // 若块后紧跟空行,一并去掉,避免粘连出异常空行- if remove_start < lines.len() && lines[remove_start].trim().is_empty() {- lines.remove(remove_start);- }- break;- }- i += block.len();- }-- let out = lines.join("\n");- let out = out.trim_end_matches('\n');- let mut out = format!("{out}\n");- // 自愈:清理后若只剩注释/空内容,补 `[]` 保证是合法顶层数组- let has_content = out- .lines()- .any(|line| !line.trim().is_empty() && !line.trim().starts_with('#'));- if !has_content {- out.push_str("[]\n");- }+ let retained: Vec<serde_yaml::Value> =+ seq.into_iter().filter(|el| !block_is_ours(el)).collect();+ let out = serde_yaml::to_string(&serde_yaml::Value::Sequence(retained))+ .map_err(|e| format!("PATCH_RENDER_FAILED: {e}"))?;write_file(&patch_path, &out).map_err(|e| format!("PATCH_PRUNE_FAILED: {e}"))}/// 修复 dsh 可能留下的“仅注释”patch scaffold:YAML 解析为 `null` 而非/// 顶层数组,加载器(`parsePatchList`)会直接抛错导致 harness 启动失败。///+ /// TODO(v1): 移除该自愈逻辑(旧版遗留的`仅注释/空` scaffold 修复),v1 起+ /// 直接按干净顶层数组处理。+ ////// 幂等:文件不存在或已有实际内容(条目或 `[]`)时不动;仅注释/空则补 `[]`。fn ensure_patch_scaffold(profile: &Path) -> Result<(), String> {let patch_path = profile.join("cordis.patch.yml");let Ok(existing) = fs::read_to_string(&patch_path) else {return Ok(());};- let has_content = existing- .lines()- .any(|line| !line.trim().is_empty() && !line.trim().starts_with('#'));- if has_content {+ // 空串或纯注释解析为 `null` 才是需要修复的状态;其余内容(数组/映射)+ // 保持原样,不做无谓改写。+ let repair = if existing.trim().is_empty() {+ true+ } else {+ match serde_yaml::from_str::<serde_yaml::Value>(&existing) {+ Ok(v) => v.is_null(),+ Err(_) => false, // 非空但非法 YAML:当前行为保持原样+ }+ };+ if !repair {return Ok(());}- let mut out = existing;- if !out.is_empty() && !out.ends_with('\n') {- out.push('\n');- }- out.push_str("[]\n");+ // 用库生成合法的空顶层数组,保证 loader 可加载。+ let out = serde_yaml::to_string(&serde_yaml::Value::Sequence(Vec::new()))+ .map_err(|e| format!("PATCH_RENDER_FAILED: {e}"))?;write_file(&patch_path, &out).map_err(|e| format!("PATCH_WRITE_FAILED: {e}"))}+ /// 把 `cordis.patch.yml` 文本解析为顶层数组 `Value`;空/纯注释视为空数组。+ fn parse_patch_list(content: &str) -> Result<serde_yaml::Value, String> {+ if content.trim().is_empty() {+ return Ok(serde_yaml::Value::Sequence(Vec::new()));+ }+ let doc: serde_yaml::Value = serde_yaml::from_str(content)+ .map_err(|e| format!("PATCH_PARSE_FAILED: {e}"))?;+ match &doc {+ serde_yaml::Value::Sequence(_) => Ok(doc),+ serde_yaml::Value::Null => Ok(serde_yaml::Value::Sequence(Vec::new())),+ _ => Err("PATCH_NOT_ARRAY: cordis.patch.yml must be a top-level array".to_string()),+ }+ }++ /// 顶层数组元素是否为本插件的 `- insert:` 挂载块(按注入标记字符串判定)。+ fn block_is_ours(el: &serde_yaml::Value) -> bool {+ serde_yaml::to_string(el)+ .map(|s| s.contains(PATCH_MARKER))+ .unwrap_or(false)+ }++ /// 生成本插件的顶层 `- insert:` 挂载元素(解析自 `PATCH_ENTRY` 模板)。+ fn plugin_insert_entry() -> serde_yaml::Value {+ let seq: serde_yaml::Value = serde_yaml::from_str(PATCH_ENTRY)+ .expect("PATCH_ENTRY must remain valid YAML");+ match seq {+ serde_yaml::Value::Sequence(mut s) => s.remove(0),+ other => other,+ }+ }+/// 在本机查找 Git Bash 可执行文件(环境变量优先,其次常见安装路径)。fn find_git_bash() -> Option<PathBuf> {if let Ok(p) = std::env::var("DSH_GIT_BASH_PATH") {@@ -476,11 +469,10 @@ mod imp {prune_patch_if_uninstalled(&dir).unwrap();let out = std::fs::read_to_string(&patch).unwrap();- // 只删我们的块:其余条目与注释原样保留+ // 只删我们的块:其余条目原样保留(库往返会丢弃注释)assert!(!out.contains("win-terminal-inspector"));assert!(!out.contains("insert:"));assert!(out.contains("some-row"));- assert!(out.contains("# user comments"));// 幂等:再次调用内容不变prune_patch_if_uninstalled(&dir).unwrap();@@ -505,9 +497,9 @@ mod imp {prune_patch_if_uninstalled(&dir).unwrap();let out = std::fs::read_to_string(&patch).unwrap();- // 标记行被删、注释保留、并补回可加载的 `[]`+ // 标记块被删,剩余为空数组 → 序列化为 `[]`(loader 可加载的顶层数组;+ // 库往返丢弃注释,但数组语义自愈成立)assert!(!out.contains("win-terminal-inspector"));- assert!(out.contains("# Your patch layer"));assert!(out.contains("[]\n"));// 幂等:再次调用内容不变