From 7eead65cf8ff2b3bf4ebe40f9ff94cfb8d2987fc Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Mon, 9 Nov 2026 05:36:17 -0500 Subject: [PATCH] Edit a YAML config that starts with a byte-order mark in place (R2.2) (#1304) A byte-order mark that some Windows editors put at the start of a YAML config was read as part of its first key, so setup added a second table of the same name that hid the first. The mark is now set aside while the file is read and edited and put back as it was, so our entry goes into the existing table and removing it gives the file back byte for byte. --- crates/setup/src/yaml.rs | 21 ++++++++++-- crates/setup/tests/yaml_config_bom.rs | 49 +++++++++++++++++++++++++++ docs/ROADMAP.md | 3 ++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 crates/setup/tests/yaml_config_bom.rs diff --git a/crates/setup/src/yaml.rs b/crates/setup/src/yaml.rs index e25bdd93..2b25646c 100644 --- a/crates/setup/src/yaml.rs +++ b/crates/setup/src/yaml.rs @@ -7,9 +7,21 @@ use serde_json::{Map, Value}; use yaml_rust2::{Yaml, YamlLoader}; +/// A byte-order mark some Windows editors put in front of the file. YAML allows one there; it isn't +/// part of the first key. +const BOM: &str = "\u{feff}"; + +/// The file's byte-order mark, if it has one, and the text after it. +fn split_bom(text: &str) -> (&'static str, &str) { + match text.strip_prefix(BOM) { + Some(rest) => (BOM, rest), + None => ("", text), + } +} + /// Parse to JSON-like data (first document; an empty file is an empty mapping). pub fn parse(text: &str) -> Result { - let docs = YamlLoader::load_from_str(text).map_err(|e| e.to_string())?; + let docs = YamlLoader::load_from_str(split_bom(text).1).map_err(|e| e.to_string())?; match docs.into_iter().next() { None | Some(Yaml::Null) => Ok(Value::Object(Map::new())), Some(y @ Yaml::Hash(_)) => Ok(to_json(&y, 0)), @@ -228,6 +240,8 @@ fn join(lines: &[Line]) -> String { /// Set `table.name` to `value` (a mapping). Returns the new text. pub fn set(text: &str, table: &str, name: &str, value: &Value) -> Result { parse(text)?; + // Lines are edited after the byte-order mark, which goes back in front as it was. + let (bom, text) = split_bom(text); let (mut out, eol) = split(text); let lines: Vec<&str> = out.iter().map(|(t, _)| t.as_str()).collect(); let new_lines = |rendered: String| -> Vec { rendered.lines().map(|l| (l.to_string(), eol)).collect() }; @@ -258,19 +272,20 @@ pub fn set(text: &str, table: &str, name: &str, value: &Value) -> Result Option { + let (bom, text) = split_bom(text); let (mut out, _) = split(text); let lines: Vec<&str> = out.iter().map(|(t, _)| t.as_str()).collect(); let block = find_table(&lines, table).ok()??; let (s, e) = find_child(&lines, &block, name)?; out.drain(s..e); - let new = join(&out); + let new = format!("{bom}{}", join(&out)); parse(&new).ok()?; Some(new) } diff --git a/crates/setup/tests/yaml_config_bom.rs b/crates/setup/tests/yaml_config_bom.rs new file mode 100644 index 00000000..85baa920 --- /dev/null +++ b/crates/setup/tests/yaml_config_bom.rs @@ -0,0 +1,49 @@ +use craft_anything_setup::yaml::{parse, remove, set}; +use serde_json::json; + +const BOM: &str = "\u{feff}"; + +/// A byte-order mark in front of a YAML config (as some Windows editors save one) isn't part of +/// its first key. +#[test] +fn a_byte_order_mark_is_not_part_of_the_first_key() { + let v = parse(&format!("{BOM}mcp_servers:\n other:\n command: o\n")).unwrap(); + assert_eq!(v, json!({"mcp_servers": {"other": {"command": "o"}}})); +} + +/// Our entry goes into the table the file already has, not into a second table of the same name +/// that hides the first from the app, and removing it gives the file back byte for byte. +#[test] +fn a_config_with_a_byte_order_mark_gets_one_table() { + let entry = json!({"command": "/b/craft-anything-cli", "args": ["mcp"], "enabled": true}); + for eol in ["\n", "\r\n"] { + let original = format!("{BOM}mcp_servers:{eol} other:{eol} command: o{eol}model: x{eol}"); + let new = set(&original, "mcp_servers", "craft-anything", &entry).unwrap(); + assert!(new.starts_with(BOM), "{new:?}"); + assert_eq!(new.matches("mcp_servers:").count(), 1, "{new:?}"); + let v = parse(&new).unwrap(); + assert_eq!(v["mcp_servers"]["other"]["command"], "o"); + assert_eq!(v["mcp_servers"]["craft-anything"], entry); + assert_eq!(v["model"], "x"); + assert_eq!(remove(&new, "mcp_servers", "craft-anything").unwrap(), original); + } +} + +/// A comment right after the byte-order mark is still a comment. +#[test] +fn a_comment_after_the_byte_order_mark_stays() { + let entry = json!({"command": "c", "args": ["mcp"]}); + let original = format!("{BOM}# Hermes config\nmcp_servers:\n other:\n command: o\n"); + let new = set(&original, "mcp_servers", "craft-anything", &entry).unwrap_or_else(|e| panic!("{e}")); + assert!(new.starts_with(&format!("{BOM}# Hermes config\n")), "{new:?}"); + assert_eq!(parse(&new).unwrap()["mcp_servers"]["other"]["command"], "o"); + assert_eq!(remove(&new, "mcp_servers", "craft-anything").unwrap(), original); +} + +/// A file holding only a byte-order mark is treated as empty. +#[test] +fn a_file_with_only_a_byte_order_mark_gets_a_new_table() { + let entry = json!({"command": "c", "args": ["mcp"]}); + let new = set(BOM, "mcp_servers", "craft-anything", &entry).unwrap(); + assert_eq!(parse(&new).unwrap(), json!({"mcp_servers": {"craft-anything": entry}})); +} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 7f4cec9a..31cdd0af 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -833,6 +833,9 @@ Owner: open · Status: in progress started before the install, or by a launcher, finds it (#116) - [ ] Live-check #116 on Windows: install Hermes natively, start the app from a launcher, and see Settings → AI offer it; #116's "error after every message" has no text yet (asked) +- [x] A Hermes `config.yaml` that starts with a byte-order mark gets our entry in its own + `mcp_servers` table, not in a second one that hides the person's servers from Hermes. + — claimed: Plastic-time, branch bugfix/r2.2-yaml-config-bom - [ ] See whether Hermes still fails on 0.3.0 (#460: the fix its `fixed` label names was already in 0.3.0 when it was filed, and it gives no version)