Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: warn about unknown fields in config files at load time (#758)
Maximilian Roosda73464fb2e0
diff --git a/src/config/deprecation.rs b/src/config/deprecation.rsindex 2862cb19f..eb8168fe7 100644--- a/src/config/deprecation.rs+++ b/src/config/deprecation.rs@@ -23,7 +23,12 @@ use std::sync::{LazyLock, Mutex};/// Tracks which config paths have already shown deprecation warnings this process./// Prevents repeated warnings when config is loaded multiple times.-static WARNED_PATHS: LazyLock<Mutex<HashSet<PathBuf>>> =+static WARNED_DEPRECATED_PATHS: LazyLock<Mutex<HashSet<PathBuf>>> =+ LazyLock::new(|| Mutex::new(HashSet::new()));++/// Tracks which config paths have already shown unknown field warnings this process.+/// Prevents repeated warnings when config is loaded multiple times.+static WARNED_UNKNOWN_PATHS: LazyLock<Mutex<HashSet<PathBuf>>> =LazyLock::new(|| Mutex::new(HashSet::new()));/// Hint name for project config deprecation warnings@@ -179,7 +184,7 @@ pub fn check_and_migrate(// Deduplicate warnings per path per processlet canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());{- let mut guard = WARNED_PATHS.lock().unwrap();+ let mut guard = WARNED_DEPRECATED_PATHS.lock().unwrap();if guard.contains(&canonical_path) {return Ok(true); // Already warned, skip}@@ -273,6 +278,50 @@ pub fn check_and_migrate(Ok(true)}+/// Warn about unknown fields in config file+///+/// Emits a warning for each unknown field, deduplicated per path per process.+/// Unlike deprecated vars, there's no migration file — the warning just repeats+/// until the user removes or fixes the unknown field.+///+/// The `label` is used in the warning message (e.g., "User config" or "Project config").+pub fn warn_unknown_fields(path: &Path, unknown_keys: &[String], label: &str) {+ if unknown_keys.is_empty() {+ return;+ }++ // Deduplicate warnings per path per process+ let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());+ {+ let mut guard = WARNED_UNKNOWN_PATHS.lock().unwrap();+ if guard.contains(&canonical_path) {+ return; // Already warned, skip+ }+ guard.insert(canonical_path);+ }++ // Build inline list of unknown keys+ let keys_display: Vec<String> = unknown_keys+ .iter()+ .map(|k| cformat!("<bold>{}</>", k))+ .collect();++ let warning = format!(+ "{} has unknown {}: {} (will be ignored)",+ label,+ if unknown_keys.len() == 1 {+ "field"+ } else {+ "fields"+ },+ keys_display.join(", ")+ );+ eprintln!("{}", warning_message(warning));++ // Flush stderr to ensure output appears before any subsequent messages+ std::io::stderr().flush().ok();+}+#[cfg(test)]mod tests {use super::*;diff --git a/src/config/mod.rs b/src/config/mod.rsindex f92e73544..9dbe92ec0 100644--- a/src/config/mod.rs+++ b/src/config/mod.rs@@ -28,6 +28,7 @@ mod user;pub use commands::{Command, CommandConfig};pub use deprecation::check_and_migrate as check_deprecated_vars;pub use deprecation::normalize_template_vars;+pub use deprecation::warn_unknown_fields;pub use expansion::{DEPRECATED_TEMPLATE_VARS, TEMPLATE_VARS, expand_template, redact_credentials,sanitize_branch_name, sanitize_db,diff --git a/src/config/project.rs b/src/config/project.rsindex 7b16c0ef8..29aef62c2 100644--- a/src/config/project.rs+++ b/src/config/project.rs@@ -149,6 +149,12 @@ impl ProjectConfig {repo_for_hints,);+ // Warn about unknown fields (only in main worktree where it's actionable)+ if is_main_worktree {+ let unknown_keys = find_unknown_keys(&contents);+ super::deprecation::warn_unknown_fields(&config_path, &unknown_keys, "Project config");+ }+let config: ProjectConfig = toml::from_str(&contents).map_err(|e| ConfigError::Message(format!("Failed to parse TOML: {}", e)))?;diff --git a/src/config/user.rs b/src/config/user.rsindex 6d9c4176b..62acd8fcd 100644--- a/src/config/user.rs+++ b/src/config/user.rs@@ -589,20 +589,27 @@ impl UserConfig {.set_default("commit-generation.args", defaults.commit_generation.args)?;// Add config file if it exists- if let Some(config_path) = get_config_path()+ let config_path = get_config_path();+ if let Some(config_path) = config_path.as_ref()&& config_path.exists(){// Check for deprecated template variables and create migration file if needed// User config always gets migration file (it's global, not worktree-specific)// Pass None for repo since user config is global and not tied to any repository- if let Ok(content) = std::fs::read_to_string(&config_path) {+ if let Ok(content) = std::fs::read_to_string(config_path) {let _ = super::deprecation::check_and_migrate(- &config_path,+ config_path,&content,true,"User config",None,);++ // Warn about unknown fields in the config file+ // (must check file content directly, not config.unknown, because+ // config.unknown includes env vars which shouldn't trigger warnings)+ let unknown_keys = find_unknown_keys(&content);+ super::deprecation::warn_unknown_fields(config_path, &unknown_keys, "User config");}builder = builder.add_source(File::from(config_path.clone()));@@ -625,6 +632,7 @@ impl UserConfig {// See: https://github.com/max-sixty/worktrunk/issues/737let config: Self = builder.build()?.try_deserialize()?;config.validate()?;+Ok(config)}diff --git a/tests/integration_tests/approvals.rs b/tests/integration_tests/approvals.rsindex ab878a643..8e57eba24 100644--- a/tests/integration_tests/approvals.rs+++ b/tests/integration_tests/approvals.rs@@ -215,7 +215,8 @@ fn test_add_approvals_project_config_no_commands(repo: TestRepo) {// Create project config with only non-hook settingsrepo.write_project_config(r#"# Project config without any hook sections-worktree-path = "../project.{{ branch }}"+[list]+url = "http://localhost:8080""#,);repo.commit("Add config without hooks");diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex d32ba9fa2..088c76197 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -700,9 +700,8 @@ fn test_deprecated_template_variables_show_warning(repo: TestRepo, temp_home: Tefs::write(config_path,// Use all deprecated variables: repo_root, worktree, main_worktree+ // Note: hooks are at top-level in user config, not in a [hooks] sectionr#"worktree-path = "../{{ main_worktree }}.{{ branch }}"--[hooks]post-create = "ln -sf {{ repo_root }}/node_modules {{ worktree }}/node_modules""#,)@@ -814,12 +813,13 @@ fn test_deprecated_template_variables_hint_deduplication(repo: TestRepo, temp_ho#[rstest]fn test_deprecated_config_deleted_shows_regenerate_hint(repo: TestRepo, temp_home: TempDir) {// Write project config with deprecated variables+ // Use deprecated variable main_worktree (should be repo) in a valid project config fieldlet project_config_dir = repo.root_path().join(".config");fs::create_dir_all(&project_config_dir).unwrap();let project_config_path = project_config_dir.join("wt.toml");fs::write(&project_config_path,- r#"worktree-path = "../{{ main_worktree }}.{{ branch }}"+ r#"post-create = "ln -sf {{ main_worktree }}/node_modules""#,).unwrap();diff --git a/tests/integration_tests/hook_show.rs b/tests/integration_tests/hook_show.rsindex 520502ff5..c2561e545 100644--- a/tests/integration_tests/hook_show.rs+++ b/tests/integration_tests/hook_show.rs@@ -187,7 +187,8 @@ fn test_hook_show_project_config_no_hooks(repo: TestRepo, temp_home: TempDir) {// Create project config without any hook sectionsrepo.write_project_config(r#"# Project config with no hooks-worktree-path = "../project.{{ branch }}"+[list]+url = "http://localhost:8080""#,);repo.commit("Add project config without hooks");