Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(config): refuse to write a config rewrite that is not valid TOML (#4140)
Maximilian Roosda03501c9952
diff --git a/src/commands/config/update.rs b/src/commands/config/update.rsindex 07e3e93b1..de5ad0fdb 100644--- a/src/commands/config/update.rs+++ b/src/commands/config/update.rs@@ -12,7 +12,7 @@ use anyhow::{Context, bail};use color_print::cformat;use worktrunk::config::{ConfigFileKind, DeprecationInfo, DeprecationKind, compute_migrated_content, config_path,- copy_approved_commands_to_approvals_file, format_deprecation_warnings,+ copy_approved_commands_to_approvals_file, ensure_config_parses, format_deprecation_warnings,format_migration_diff_block,};use worktrunk::git::{Repository, resolve_input_path};@@ -36,6 +36,25 @@ struct UpdateCandidate {info: DeprecationInfo,}+impl UpdateCandidate {+ /// Compute the migration, refusing one whose content wt could not load.+ ///+ /// Checking here rather than at the write covers both destinations — the+ /// in-place update and `--output` — and fails before the preview asks for+ /// confirmation.+ fn new(config_path: PathBuf, original: String, info: DeprecationInfo) -> anyhow::Result<Self> {+ let migrated = compute_migrated_content(&original);+ ensure_config_parses(&migrated)+ .with_context(|| format!("Failed to migrate {}", info.label().to_lowercase()))?;+ Ok(Self {+ config_path,+ original,+ migrated,+ info,+ })+ }+}+/// Handle the `wt config update` command.pub fn handle_config_update(yes: bool, output: Option<PathBuf>) -> anyhow::Result<()> {let mut candidates = Vec::new();@@ -277,13 +296,7 @@ fn check_user_config() -> anyhow::Result<Option<UpdateCandidate>> {return Ok(None);};- let migrated = compute_migrated_content(&original);- Ok(Some(UpdateCandidate {- config_path,- original,- migrated,- info,- }))+ UpdateCandidate::new(config_path, original, info).map(Some)}fn check_project_config(read_only: bool) -> anyhow::Result<Option<UpdateCandidate>> {@@ -326,11 +339,5 @@ fn check_project_config(read_only: bool) -> anyhow::Result<Option<UpdateCandidatreturn Ok(None);}- let migrated = compute_migrated_content(&original);- Ok(Some(UpdateCandidate {- config_path,- original,- migrated,- info,- }))+ UpdateCandidate::new(config_path, original, info).map(Some)}diff --git a/src/config/mod.rs b/src/config/mod.rsindex 3d2c034c7..cfdbc0925 100644--- a/src/config/mod.rs+++ b/src/config/mod.rs@@ -190,6 +190,22 @@ pub(crate) fn replace_inline_with_table(existing[key] = toml_edit::Item::Table(table);}+/// Refuse to write a config file that is not valid TOML.+///+/// wt can't load such a file: every later command skips user config with a+/// warning, and the commands that need project config fail, until the user+/// hand-edits it. Both writers that rewrite a config file the user owns+/// check the content they are about to write: `UserConfig::save_to` and+/// `wt config update`. Neither starts from invalid TOML, so this fires only when+/// the rewrite itself broke the syntax, and the file on disk stays as it was.+pub fn ensure_config_parses(content: &str) -> Result<(), ConfigError> {+ content.parse::<toml::Table>().map(|_| ()).map_err(|e| {+ ConfigError(format!(+ "Refusing to write a config file wt could not read back: {e}"+ ))+ })+}+// Re-export public typespub use approvals::{Approvals, approvals_path, require_approvals_path};pub use commands::{Command, CommandConfig, HookStep, append_aliases};@@ -250,6 +266,16 @@ mod tests {TestRepo::new()}+ #[test]+ fn test_ensure_config_parses_rejects_a_header_holding_the_key_decor() {+ // The shape an inline-to-table rewrite once wrote: the key's leading+ // comment rendered inside the brackets.+ let err =+ ensure_config_parses("[# why squash is off\nmerge ]\nsquash = true\n").unwrap_err();+ assert!(err.0.contains("could not read back"), "{}", err.0);+ ensure_config_parses("# why squash is off\n[merge]\nsquash = true\n").unwrap();+ }+#[test]fn test_config_serialization() {// Default config serializes to empty (no optional fields)diff --git a/src/config/user/persistence.rs b/src/config/user/persistence.rsindex 394c4153b..3239a3cec 100644--- a/src/config/user/persistence.rs+++ b/src/config/user/persistence.rs@@ -223,6 +223,7 @@ impl UserConfig {doc.to_string()};+ crate::config::ensure_config_parses(&toml_string)?;crate::utils::write_atomically(config_path, &toml_string).map_err(|e| ConfigError(format!("Failed to write config file: {}", e)))?;