From 403bb28ca4b3875b56c8a24d8b989d1148c901d8 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 18 Nov 2026 19:21:04 +0800 Subject: [PATCH] fix(local-history): keep snapshots as document text (no BOM, LF) (#4272) Saves record the document's text but the watcher snapshotted the file with its BOM and CRLF, adding an External change revision per save; reverting that revision doubled the BOM. Disk snapshots and snapshot content are now normalized to document text. Fixes #4195. --- crates/ide-workspace/src/history.rs | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/ide-workspace/src/history.rs b/crates/ide-workspace/src/history.rs index 9fe0135bb..f99e5c35c 100644 --- a/crates/ide-workspace/src/history.rs +++ b/crates/ide-workspace/src/history.rs @@ -121,7 +121,14 @@ fn read_snapshot_text(path: &Path) -> Option { if bytes.iter().take(8192).any(|b| *b == 0) { return None; } - String::from_utf8(bytes).ok() + String::from_utf8(bytes).ok().map(|text| document_text(&text).into_owned()) +} + +/// `text` as a document holds it: without the UTF-8 BOM and with `\n` line endings. Saves +/// record the document's text, so a file read from disk must match it to dedupe, and +/// Revert writes it back through the document, which adds the file's BOM and line endings. +fn document_text(text: &str) -> std::borrow::Cow<'_, str> { + ide_text::normalize_line_endings(text.strip_prefix('\u{feff}').unwrap_or(text)) } impl LocalHistory { @@ -338,7 +345,10 @@ impl LocalHistory { return Err(IdeError::invalid(format!("invalid history id {id:?}"))); } match fs::read(self.blob_path(id)) { - Ok(bytes) => Ok(String::from_utf8(bytes).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into())), + Ok(bytes) => { + let text = String::from_utf8(bytes).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into()); + Ok(document_text(&text).into_owned()) + } Err(e) if e.kind() == std::io::ErrorKind::NotFound => { Err(IdeError::not_found(format!("history snapshot {id} not found"))) } @@ -532,6 +542,25 @@ mod tests { } } + #[test] + fn a_file_read_from_disk_matches_the_saved_document_text() { + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path().join("project"); + fs::create_dir_all(&root).unwrap(); + let h = LocalHistory::open(&tmp.path().join("history"), &root).unwrap(); + let a = root.join("a.txt"); + // A save records the document's text; the file on disk has a BOM and CRLF. + fs::write(&a, b"\xEF\xBB\xBFheader\r\nbody\r\n").unwrap(); + h.record(&a, "header\nbody\n", "Saved").unwrap(); + assert!(h.record_file(&a, "External change", false).unwrap()); + let list = h.list(&a).unwrap(); + assert_eq!(list.iter().map(|e| e.label.as_str()).collect::>(), ["Saved"], "no second revision"); + // A snapshot stored with its BOM before reads back as document text. + h.record(&a, "\u{feff}old\r\n", "External change").unwrap(); + let old = &h.list(&a).unwrap()[0]; + assert_eq!(h.content(&old.id).unwrap(), "old\n"); + } + #[test] fn events_labels_and_deletions() { let tmp = tempfile::tempdir().unwrap();