Skip to content

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

fix(config): stop reporting valid empty per-project sections as unknown (#4123)

Worktrunk Bota6e2bed4b382
diff --git a/src/config/deprecation.rs b/src/config/deprecation.rs
index 0911e67fd..b8e480620 100644
--- a/src/config/deprecation.rs
+++ b/src/config/deprecation.rs
@@ -1515,14 +1515,11 @@ fn rename_hook_key(table: &mut toml_edit::Table, old_key: &str, new_key: &str) -
/// `[projects."<id>"]`-scoped section that the removal empties.
///
/// A default section serializes away, so an empty one is absent from the
-/// round-trip `unknown_tree` compares against. `seed_schema_skeleton` puts
-/// every valid *top-level* key back, which is why an emptied top-level
-/// `[list]` reads as known and an emptied `[projects."<id>".list]` reads as
-/// `unknown field projects.<id>.list`. So only the project scope needs the
-/// section removed; a top-level one keeps its position and the comments above
-/// its header. (An empty nested section the user wrote by hand warns the same
-/// way — that false positive belongs to `unknown_tree`, and this rule only
-/// avoids adding to it.)
+/// round-trip `unknown_tree` compares against; `seed_schema_skeleton` puts the
+/// valid keys back at both levels, so an emptied section reads as known in
+/// either scope. The project scope still drops the section it empties, leaving
+/// nothing behind at a path the user's remaining config never mentions, while a
+/// top-level one keeps its position and the comments above its header.
///
/// A section can be written as a section table (`[list]`) or inline
/// (`list = { … }`); `toml_edit` surfaces these as different node types, so
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 76479f949..8216be93f 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -46,6 +46,19 @@ pub trait WorktrunkConfig:
/// All valid top-level keys for this config type, derived from JsonSchema.
fn valid_top_level_keys() -> &'static [String];
+ /// A top-level key whose entries are themselves schema-typed tables,
+ /// paired with the valid keys for one entry — `projects` in user config,
+ /// nothing in project config.
+ ///
+ /// A section inside such an entry serializes away when it equals its
+ /// default, exactly as a top-level section does, so the skeleton
+ /// `seed_schema_skeleton` builds has to reach one level further down for
+ /// it. Without that, `[projects."<id>".list]` — valid, and accepted on
+ /// load — reads back as an unknown field.
+ fn valid_scoped_keys() -> Option<(&'static str, &'static [String])> {
+ None
+ }
+
/// Check if a key would be valid in this config type.
fn is_valid_key(key: &str) -> bool {
Self::valid_top_level_keys().iter().any(|k| k == key)
@@ -64,6 +77,10 @@ impl WorktrunkConfig for UserConfig {
static VALID_KEYS: OnceLock<Vec<String>> = OnceLock::new();
VALID_KEYS.get_or_init(user::valid_user_config_keys)
}
+
+ fn valid_scoped_keys() -> Option<(&'static str, &'static [String])> {
+ Some(("projects", user_project_override_keys()))
+ }
}
impl WorktrunkConfig for ProjectConfig {
@@ -142,17 +159,26 @@ pub(crate) fn schema_property_names<T: schemars::JsonSchema>() -> Vec<String> {
.unwrap_or_default()
}
+/// The top-level fields of a `[projects."<id>"]` table (the
+/// [`UserProjectOverrides`] schema), including the serde aliases
+/// [`schema_top_level_keys`] adds.
+///
+/// Two callers share it: [`is_user_project_override_key`] for the "scope it to
+/// this repo" note, and `UserConfig::valid_scoped_keys` for the round-trip
+/// skeleton in `unknown_tree`.
+fn user_project_override_keys() -> &'static [String] {
+ use std::sync::OnceLock;
+ static KEYS: OnceLock<Vec<String>> = OnceLock::new();
+ KEYS.get_or_init(schema_top_level_keys::<UserProjectOverrides>)
+}
+
/// Whether `key` is a top-level field of a `[projects."<id>"]` table (the
/// [`UserProjectOverrides`] schema). Used to gate the "scope it to this repo"
/// note: a misplaced user-config key only earns that advice when it can
/// actually be placed under `[projects."<id>"]`. Root-only user settings such
/// as `skip-shell-integration-prompt` are absent here and get no note.
pub fn is_user_project_override_key(key: &str) -> bool {
- use std::sync::OnceLock;
- static KEYS: OnceLock<Vec<String>> = OnceLock::new();
- KEYS.get_or_init(schema_top_level_keys::<UserProjectOverrides>)
- .iter()
- .any(|k| k == key)
+ user_project_override_keys().iter().any(|k| k == key)
}
/// Refuse to write a config file that is not valid TOML.
diff --git a/src/config/unknown_tree.rs b/src/config/unknown_tree.rs
index 411670744..cde2ddcb6 100644
--- a/src/config/unknown_tree.rs
+++ b/src/config/unknown_tree.rs
@@ -59,10 +59,33 @@ where
}
/// Seed `reserialized` with every schema-valid top-level key as an empty
-/// table so `diff_tables` treats valid-but-omitted sections as known.
+/// table so `diff_tables` treats valid-but-omitted sections as known, then do
+/// the same one level down inside each entry of the scoped map
+/// ([`WorktrunkConfig::valid_scoped_keys`] — `projects` in user config).
+///
+/// Both levels need it for the same reason: a section equal to its default is
+/// skipped during serialization, so it is missing from the round-tripped view
+/// `diff_tables` compares against and would be reported as unknown.
fn seed_schema_skeleton<C: WorktrunkConfig>(reserialized: &mut toml::Table) {
- for key in C::valid_top_level_keys() {
- reserialized
+ seed_keys(reserialized, C::valid_top_level_keys());
+
+ // The top-level pass above already put `scope_key` in place as an empty
+ // table where the round-trip omitted it, so this lookup finds the scoped
+ // map whether or not any entry survived serialization.
+ if let Some((scope_key, entry_keys)) = C::valid_scoped_keys()
+ && let Some(toml::Value::Table(entries)) = reserialized.get_mut(scope_key)
+ {
+ for (_, entry) in entries.iter_mut() {
+ if let toml::Value::Table(entry_table) = entry {
+ seed_keys(entry_table, entry_keys);
+ }
+ }
+ }
+}
+
+fn seed_keys(table: &mut toml::Table, keys: &[String]) {
+ for key in keys {
+ table
.entry(key.clone())
.or_insert_with(|| toml::Value::Table(toml::Table::new()));
}
@@ -381,6 +404,49 @@ b = 2
assert!(tree.keys.contains("skip-shell-integration-prompt"));
}
+ #[test]
+ fn empty_per_project_section_is_known() {
+ // `[projects."<id>".list]` is valid and accepted on load. It equals
+ // its default, so it serializes away and is missing from the
+ // round-trip — the skeleton has to put it back one level down too.
+ let tree = parsed::<UserConfig>("[projects.\"example.com/org/repo\".list]\n");
+ assert!(tree.is_empty(), "unexpected unknown keys: {tree:?}");
+ assert!(
+ collect_unknown_warnings::<UserConfig>("[projects.\"example.com/org/repo\".list]\n")
+ .is_empty()
+ );
+ }
+
+ #[test]
+ fn unknown_key_in_per_project_scope_still_warns() {
+ // The skeleton seeds only schema-valid keys, so a genuine unknown in
+ // the same scope keeps its warning — as does one nested inside a
+ // known section, and a user key that has no per-project form.
+ for (content, expected) in [
+ (
+ "[projects.\"example.com/org/repo\".nonsense]\n",
+ "projects.example.com/org/repo.nonsense",
+ ),
+ (
+ "[projects.\"example.com/org/repo\".list]\nbogus = 1\n",
+ "projects.example.com/org/repo.list.bogus",
+ ),
+ (
+ "[projects.\"example.com/org/repo\"]\nskip-shell-integration-prompt = true\n",
+ "projects.example.com/org/repo.skip-shell-integration-prompt",
+ ),
+ ] {
+ let warnings = collect_unknown_warnings::<UserConfig>(content);
+ assert!(
+ warnings.iter().any(|w| matches!(
+ w,
+ UnknownWarning::NestedUnknown { path } if path == expected
+ )),
+ "expected {expected} to warn, got {warnings:?} for:\n{content}"
+ );
+ }
+ }
+
#[test]
fn syntax_error_yields_no_tree() {
assert!(compute_unknown_tree::<UserConfig>("not valid {{{").is_none());