diff --git a/crates/settings/src/store.rs b/crates/settings/src/store.rs index 76c10d1429..45917e2b6d 100644 --- a/crates/settings/src/store.rs +++ b/crates/settings/src/store.rs @@ -466,15 +466,16 @@ impl SettingsStore { // writing over the changes this save merges in. An early return releases it. let lock = self.files.lock(&path); let (on_disk, theirs) = self.check_disk(&path)?; - merge_into(&mut self.doc, &self.value).map_err(|e| SettingsError::Serialize(e.to_string()))?; - set_schema_version(&mut self.doc, T::SCHEMA_VERSION); - // The merge is staged and only kept once the file is written, so a save that fails leaves the + // The merge is staged on a copy and only kept once the file is written, so a save that fails leaves the // document, the value and the baseline as they were, and the next save merges the disk again. + let mut doc = self.doc.clone(); + merge_into(&mut doc, &self.value).map_err(|e| SettingsError::Serialize(e.to_string()))?; + set_schema_version(&mut doc, T::SCHEMA_VERSION); let theirs = theirs.map(|mut theirs| { - merge_changes::(&self.base, &self.doc, &mut theirs); + merge_changes::(&self.base, &doc, &mut theirs); theirs }); - let text = document::render(theirs.as_ref().unwrap_or(&self.doc)); + let text = document::render(theirs.as_ref().unwrap_or(&doc)); // A file over the limit would not be read back at the next start; keep the old file. if text.len() as u64 > MAX_CONFIG_BYTES { return Err(SettingsError::Io(too_large())); @@ -486,6 +487,7 @@ impl SettingsStore { } self.files.write(&path, &text)?; drop(lock); + self.doc = doc; if let Some(theirs) = theirs { let (ours, _) = document::deserialize_sections::(&self.doc); self.doc = theirs; diff --git a/crates/settings/tests/recovery.rs b/crates/settings/tests/recovery.rs index 70453290a3..9fbb8e2ef9 100644 --- a/crates/settings/tests/recovery.rs +++ b/crates/settings/tests/recovery.rs @@ -2,7 +2,7 @@ use super::common; -use common::{config_path, open_in, read_config, write_config}; +use common::{config_path, open_in, open_keyed, read_config, write_config}; use craft_settings::{ReadOnlyReason, SettingsError, Source}; #[test] @@ -117,6 +117,30 @@ fn rejected_save_keeps_the_adopted_file_as_the_baseline() { assert!(text.contains("language = \"fr\""), "{text}"); } +#[test] +fn rejected_save_keeps_the_comment_of_a_removed_key() { + let tmp = tempfile::tempdir().unwrap(); + write_config(tmp.path(), "schema_version = 1\n\n[shortcuts]\n# note on save\nsave = \"Ctrl+S\"\n"); + let (mut store, _) = open_keyed(tmp.path()); + + // The key is removed in a save too large to write, then put back. + let huge = "a".repeat(craft_settings::store::MAX_CONFIG_BYTES as usize); + store.update(|k| { + k.shortcuts.remove("save"); + k.general.language = huge; + }); + assert!(matches!(store.flush(), Err(SettingsError::Io(e)) if e.kind() == std::io::ErrorKind::FileTooLarge)); + store.update(|k| { + k.shortcuts.insert("save".into(), "Ctrl+S".into()); + k.general.language = "fr".into(); + }); + store.flush().unwrap(); + + let text = read_config(tmp.path()); + assert!(text.contains("# note on save\nsave = \"Ctrl+S\""), "{text}"); + assert!(text.contains("language = \"fr\""), "{text}"); +} + #[test] fn newer_schema_opens_read_only_and_never_overwrites() { let tmp = tempfile::tempdir().unwrap();