Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: warn when deprecated config key appears in wrong config file (#1899)

Maximilian Roos2b83c551313d
diff --git a/CLAUDE.md b/CLAUDE.md
index 280a3b471..4d52b7939 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -388,7 +388,7 @@ Separately, `check_and_migrate()` detects deprecated patterns, emits warnings, a
3. Call the migration function from `migrate_content()`
4. Add a warning message in `format_deprecation_warnings()`
5. Update `Deprecations::is_empty()` to include the new field
-6. If it's a top-level section being removed from the struct, add the key to `DEPRECATED_SECTION_KEYS` (prevents `warn_unknown_fields` from also flagging it)
+6. If it's a top-level section being removed from the struct, add a `DeprecatedSection` entry to `DEPRECATED_SECTION_KEYS` with the canonical top-level key and display form. This tells `warn_unknown_fields` to skip the key when it appears in the correct config type (the deprecation system provides better messaging) and to suggest the correct config when it appears in the wrong file.
### Renaming a field within a section
diff --git a/src/commands/config/mod.rs b/src/commands/config/mod.rs
index a24275ebd..073df51ec 100644
--- a/src/commands/config/mod.rs
+++ b/src/commands/config/mod.rs
@@ -166,6 +166,52 @@ mod tests {
assert_snapshot!(warn_unknown_keys::<UserConfig>(&unknown), @"▲ Key forge belongs in project config (will be ignored)");
}
+ #[test]
+ fn test_warn_unknown_keys_deprecated_in_wrong_config() {
+ // commit-generation in project config should suggest user config with canonical form
+ let mut unknown = HashMap::new();
+ let mut inner = toml::map::Map::new();
+ inner.insert(
+ "command".to_string(),
+ toml::Value::String("llm".to_string()),
+ );
+ unknown.insert("commit-generation".to_string(), toml::Value::Table(inner));
+ assert_snapshot!(warn_unknown_keys::<ProjectConfig>(&unknown));
+
+ // ci in user config should suggest project config with canonical form
+ let mut unknown = HashMap::new();
+ let mut inner = toml::map::Map::new();
+ inner.insert(
+ "platform".to_string(),
+ toml::Value::String("github".to_string()),
+ );
+ unknown.insert("ci".to_string(), toml::Value::Table(inner));
+ assert_snapshot!(warn_unknown_keys::<UserConfig>(&unknown));
+ }
+
+ #[test]
+ fn test_warn_unknown_keys_deprecated_in_right_config_is_skipped() {
+ // commit-generation in user config should be skipped (deprecation system handles it)
+ let mut unknown = HashMap::new();
+ let mut inner = toml::map::Map::new();
+ inner.insert(
+ "command".to_string(),
+ toml::Value::String("llm".to_string()),
+ );
+ unknown.insert("commit-generation".to_string(), toml::Value::Table(inner));
+ assert!(warn_unknown_keys::<UserConfig>(&unknown).is_empty());
+
+ // ci in project config should be skipped (deprecation system handles it)
+ let mut unknown = HashMap::new();
+ let mut inner = toml::map::Map::new();
+ inner.insert(
+ "platform".to_string(),
+ toml::Value::String("github".to_string()),
+ );
+ unknown.insert("ci".to_string(), toml::Value::Table(inner));
+ assert!(warn_unknown_keys::<ProjectConfig>(&unknown).is_empty());
+ }
+
// ==================== render_ci_tool_status tests ====================
#[test]
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rs
index 1479a4b80..965e5a87c 100644
--- a/src/commands/config/show.rs
+++ b/src/commands/config/show.rs
@@ -10,8 +10,8 @@ use std::path::{Path, PathBuf};
use anyhow::Context;
use color_print::cformat;
use worktrunk::config::{
- ProjectConfig, UserConfig, default_system_config_path, find_unknown_project_keys,
- find_unknown_user_keys, system_config_path,
+ ProjectConfig, UserConfig, WorktrunkConfig as _, default_system_config_path,
+ find_unknown_project_keys, find_unknown_user_keys, system_config_path,
};
use worktrunk::git::Repository;
use worktrunk::path::format_path_for_display;
@@ -490,15 +490,29 @@ pub(super) fn warn_unknown_keys<C: worktrunk::config::WorktrunkConfig>(
) -> String {
let mut out = String::new();
- // Sort keys for deterministic output order, skipping deprecated keys
- // (the deprecation system handles those with better messaging)
- let mut keys: Vec<_> = unknown_keys
- .keys()
- .filter(|k| !worktrunk::config::DEPRECATED_SECTION_KEYS.contains(&k.as_str()))
- .collect();
+ let mut keys: Vec<_> = unknown_keys.keys().collect();
keys.sort();
for key in keys {
+ // Check if this is a deprecated section key
+ if let Some(dep) = worktrunk::config::DEPRECATED_SECTION_KEYS
+ .iter()
+ .find(|d| d.key == key.as_str())
+ {
+ if C::is_valid_key(dep.canonical_top_key) {
+ // Canonical form belongs to this config type — deprecation system handles it
+ continue;
+ }
+ // Deprecated key in wrong config file — point to the correct config
+ let msg = cformat!(
+ "Key <bold>{key}</> belongs in {} as {}",
+ C::Other::description(),
+ dep.canonical_display,
+ );
+ let _ = writeln!(out, "{}", warning_message(msg));
+ continue;
+ }
+
let msg = match worktrunk::config::key_belongs_in::<C>(key) {
Some(location) => {
cformat!("Key <bold>{key}</> belongs in {location} (will be ignored)")
diff --git a/src/commands/config/snapshots/wt__commands__config__tests__warn_unknown_keys_deprecated_in_wrong_config-2.snap b/src/commands/config/snapshots/wt__commands__config__tests__warn_unknown_keys_deprecated_in_wrong_config-2.snap
new file mode 100644
index 000000000..85a04f1f2
--- /dev/null
+++ b/src/commands/config/snapshots/wt__commands__config__tests__warn_unknown_keys_deprecated_in_wrong_config-2.snap
@@ -0,0 +1,5 @@
+---
+source: src/commands/config/mod.rs
+expression: "warn_unknown_keys::<UserConfig>(&unknown)"
+---
+▲ Key ci belongs in project config as [forge]
diff --git a/src/commands/config/snapshots/wt__commands__config__tests__warn_unknown_keys_deprecated_in_wrong_config.snap b/src/commands/config/snapshots/wt__commands__config__tests__warn_unknown_keys_deprecated_in_wrong_config.snap
new file mode 100644
index 000000000..25487f6ec
--- /dev/null
+++ b/src/commands/config/snapshots/wt__commands__config__tests__warn_unknown_keys_deprecated_in_wrong_config.snap
@@ -0,0 +1,5 @@
+---
+source: src/commands/config/mod.rs
+expression: "warn_unknown_keys::<ProjectConfig>(&unknown)"
+---
+▲ Key commit-generation belongs in user config as [commit.generation]
diff --git a/src/config/deprecation.rs b/src/config/deprecation.rs
index 89ab3a70e..300a2b6d6 100644
--- a/src/config/deprecation.rs
+++ b/src/config/deprecation.rs
@@ -67,14 +67,41 @@ const DEPRECATED_VARS: &[(&str, &str)] = &[
("main_worktree_path", "primary_worktree_path"),
];
+/// Metadata for a deprecated top-level section key.
+#[derive(Debug)]
+pub struct DeprecatedSection {
+ /// The deprecated key name (e.g., "commit-generation")
+ pub key: &'static str,
+ /// The canonical top-level key that replaces this, for determining which config type
+ /// it belongs to via `WorktrunkConfig::is_valid_key()` (e.g., "commit")
+ pub canonical_top_key: &'static str,
+ /// Human-readable canonical form for display (e.g., "[commit.generation]")
+ pub canonical_display: &'static str,
+}
+
/// Top-level section keys that are deprecated and handled by the deprecation system.
-/// `warn_unknown_fields` skips these automatically.
///
-/// TODO: When a deprecated key appears in the wrong config file (e.g., `[commit-generation]`
-/// in project config), the deprecation system suggests renaming it — but the renamed key
-/// isn't valid in that config either. Ideally we'd detect "deprecated + wrong file" and
-/// suggest moving to the correct config instead of just renaming.
-pub const DEPRECATED_SECTION_KEYS: &[&str] = &["commit-generation", "select", "ci"];
+/// When a deprecated key appears in the config type where its canonical replacement
+/// is valid, `warn_unknown_fields` skips it (the deprecation system provides better
+/// messaging). When it appears in the wrong config type, `warn_unknown_fields`
+/// warns that it belongs in the other config with the canonical form.
+pub const DEPRECATED_SECTION_KEYS: &[DeprecatedSection] = &[
+ DeprecatedSection {
+ key: "commit-generation",
+ canonical_top_key: "commit",
+ canonical_display: "[commit.generation]",
+ },
+ DeprecatedSection {
+ key: "select",
+ canonical_top_key: "switch",
+ canonical_display: "[switch.picker]",
+ },
+ DeprecatedSection {
+ key: "ci",
+ canonical_top_key: "forge",
+ canonical_display: "[forge]",
+ },
+];
/// Normalize a template string by replacing deprecated variables with their canonical names.
///
@@ -1425,15 +1452,31 @@ pub fn warn_unknown_fields<C: WorktrunkConfig>(
guard.insert(canonical_path);
}
- // Sort keys for deterministic output order, skipping deprecated keys
- // (the deprecation system handles those with better messaging)
- let mut keys: Vec<_> = unknown_keys
- .keys()
- .filter(|k| !DEPRECATED_SECTION_KEYS.contains(&k.as_str()))
- .collect();
+ let mut keys: Vec<_> = unknown_keys.keys().collect();
keys.sort();
for key in keys {
+ // Check if this is a deprecated section key
+ if let Some(dep) = DEPRECATED_SECTION_KEYS
+ .iter()
+ .find(|d| d.key == key.as_str())
+ {
+ if C::is_valid_key(dep.canonical_top_key) {
+ // Canonical form belongs to this config type — deprecation system handles it
+ continue;
+ }
+ // Deprecated key in wrong config file — point to the correct config
+ eprintln!(
+ "{}",
+ warning_message(cformat!(
+ "{label} has key <bold>{key}</> which belongs in {} as {}",
+ C::Other::description(),
+ dep.canonical_display,
+ ))
+ );
+ continue;
+ }
+
if let Some(other_location) = key_belongs_in::<C>(key) {
eprintln!(
"{}",
@@ -3365,4 +3408,24 @@ ff = true
let result = migrate_content(content);
assert_eq!(result, content);
}
+
+ #[test]
+ fn test_warn_unknown_fields_deprecated_key_in_wrong_config() {
+ use crate::config::{ProjectConfig, UserConfig};
+
+ // commit-generation in project config → should warn "belongs in user config"
+ let mut unknown = HashMap::new();
+ unknown.insert(
+ "commit-generation".to_string(),
+ toml::Value::Table(toml::map::Map::new()),
+ );
+ let path = std::env::temp_dir().join("test-deprecated-wrong-config-project.toml");
+ warn_unknown_fields::<ProjectConfig>(&path, &unknown, "Project config");
+
+ // ci in user config → should warn "belongs in project config"
+ let mut unknown = HashMap::new();
+ unknown.insert("ci".to_string(), toml::Value::Table(toml::map::Map::new()));
+ let path = std::env::temp_dir().join("test-deprecated-wrong-config-user.toml");
+ warn_unknown_fields::<UserConfig>(&path, &unknown, "User config");
+ }
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 7d4d4b031..37ad261ce 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -85,7 +85,9 @@ pub use deprecation::format_migration_diff;
pub use deprecation::migrate_content;
pub use deprecation::normalize_template_vars;
pub use deprecation::write_migration_file;
-pub use deprecation::{DEPRECATED_SECTION_KEYS, key_belongs_in, warn_unknown_fields};
+pub use deprecation::{
+ DEPRECATED_SECTION_KEYS, DeprecatedSection, key_belongs_in, warn_unknown_fields,
+};
pub use expansion::{
DEPRECATED_TEMPLATE_VARS, TEMPLATE_VARS, TemplateExpandError, expand_template,
redact_credentials, sanitize_branch_name, sanitize_db, short_hash, template_references_var,
diff --git a/tests/integration_tests/readme_sync.rs b/tests/integration_tests/readme_sync.rs
index 2c6f7e5f3..f93d28906 100644
--- a/tests/integration_tests/readme_sync.rs
+++ b/tests/integration_tests/readme_sync.rs
@@ -1176,7 +1176,10 @@ fn test_config_docs_include_all_sections() {
// Check non-deprecated sections appear as TOML headers ([key] or [key.something])
for key in &section_keys {
- if DEPRECATED_SECTION_KEYS.contains(&key.as_str()) {
+ if DEPRECATED_SECTION_KEYS
+ .iter()
+ .any(|d| d.key == key.as_str())
+ {
let header = format!("[{key}]");
assert!(
!user_config_content.contains(&header),
@@ -1229,7 +1232,10 @@ fn test_project_config_docs_include_all_sections() {
// Check non-deprecated sections appear as TOML headers ([key] or [key.something])
for key in &section_keys {
- if DEPRECATED_SECTION_KEYS.contains(&key.as_str()) {
+ if DEPRECATED_SECTION_KEYS
+ .iter()
+ .any(|d| d.key == key.as_str())
+ {
let header = format!("[{key}]");
assert!(
!project_config_content.contains(&header),
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_project_config_for_ci.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_project_config_for_ci.snap
index 5f2299098..b2d218707 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_project_config_for_ci.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_project_config_for_ci.snap
@@ -58,6 +58,7 @@ exit_code: 0
  / No newline at end of file
  +[forge]
  +platform = "github"
+▲ Key ci belongs in project config as [forge]
○ Current config:
  worktree-path = "../{{ repo }}.{{ branch }}"
 
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_user_config_for_commit_generation.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_user_config_for_commit_generation.snap
index 3f1a308e7..fb15c37ac 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_user_config_for_commit_generation.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_suggests_user_config_for_commit_generation.snap
@@ -60,6 +60,7 @@ exit_code: 0
  / No newline at end of file
  +[commit.generation]
  +command = "claude"
+▲ Key commit-generation belongs in user config as [commit.generation]
○ Current config:
  [commit-generation]
  command = "claude"