Repository navigation
fix(config): keep default values written out in the file when a save doesn't change them - #4156
Conversation
…doesn't change them A config save diffed the in-memory config against the file and removed every key the config didn't serialize. A field at its default serializes to nothing, so `skip-shell-integration-prompt = false # note`, `columns = []`, or an empty `[merge]` section was deleted, comment and all, by any unrelated save such as setting the commit-generation command. The merge now also serializes the file's own config and removes a key only when that has it and the saved config doesn't — the keys the change reset. Keys neither serializes (defaults written out, schema-unknown keys) stay, which replaces the unknown-key preserve tree the save used. An existing file that no longer parses as a config now fails the save instead of being rewritten. Two things the old merge got right by accident, or wrong, now hold by construction: - The file's `pre-create`/`post-create` hook aliases are renamed to their canonical keys before merging, with the same rename the load path applies. Left under the alias, the hook was written again beside it and the next load failed on the duplicate; the old merge avoided that only at the top level. - A save no longer appends a bare `[projects]` header to a file that had none. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
# Conflicts: # src/config/user/persistence.rs
…heir comments The `pre-create`/`post-create` → `pre-start`/`post-start` rename skipped a `[projects]` entry written inline, and dropped the comment line above the key wherever it renamed one. On load, that left the inline entry's alias unrenamed, so the unknown-key check warned that a working hook would be ignored. On save, the file kept the alias while the saved config wrote the canonical key beside it, and the next load failed on the duplicate. The rename now walks every config scope through `for_each_config_table_mut`, as the other scoped migrations do, and moves the key with its leaf decor. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Anchoring the merge on the file's own config is the right shape, and it's provably safer than what it replaces: a key absent from base is by construction one serde didn't round-trip, so it was never a stale-key candidate under the old preserve filter either — the new removal set is a subset of the old one. pre-create/post-create are also the only #[serde(alias)] pair in the config schema, so canonicalize_hook_keys covers the whole duplicate-key surface rather than one instance of it.
One gap in the same class as the fix, and one note on the overlapping PR.
An empty [commit] section is still deleted
make_commit_table_implicit_if_only_subtables in src/config/user/persistence.rs decides with commit.iter().all(|(_, v)| v.is_table()), and all() over an empty table is true. So a [commit] header the user wrote is forced implicit and vanishes, taking the comment above it.
[commit] is exactly the section the new merge should have rescued and doesn't: it carries skip_serializing_if = "super::is_default", so an empty one is absent from both base and desired, merge_tables correctly leaves it alone — and then the implicit pass takes it anyway. It's the one "written out at its default" section that still loses its line.
Same shape as the new test_saving_config_mutation_keeps_explicit_defaults, run against this head:
# commit settings, fill in later
[commit]
[merge]
[list]
columns = [] # pick laterafter set_skip_shell_integration_prompt:
skip-shell-integration-prompt = true
[merge]
[list]
columns = [] # pick later[merge] and columns = [] survive; [commit] and its comment are gone. With a [commit.generation] subtable present the header and comment go the same way, leaving only [commit.generation].
Moving the call into to_expanded_document fixes both: a commit table inserted from desired arrives implicit through the same clone that already gives projects its implicit flag, and existing_doc's own header is never touched.
if let Some(projects) = doc.get_mut("projects").and_then(|p| p.as_table_mut()) {
projects.set_implicit(true);
}
+ Self::make_commit_table_implicit_if_only_subtables(&mut doc);
Ok(doc)
}with both calls in save_to dropped. That's green on cargo test --lib config (606) and cargo test --test integration approval_save (24), and both cases above then round-trip untouched. Happy to push it here if you'd rather not carry it as a follow-up.
#4123 touches the same five files
Its persistence.rs half looks subsumed by this one. I ran its test_save_to_existing_file_keeps_section_holding_only_unknown_keys unmodified against this head and it passes — a [list] holding only future-knob = 1 survives because base has no list — so there's nothing to port from its !preserve.nested.contains_key(k) guard, which has no preserve to hang off any more. Its unknown_tree.rs half is still live work, since per-project seeding is the warning path this PR narrows compute_unknown_tree down to.
The save marked the file's `commit` table implicit whenever it held only subtables, which also matched an empty `[commit]` or one above `[commit.generation]`, so the header and the comment above it disappeared. The implicit flag now goes on the serialized document, where it only keeps a save from adding a bare `[commit]` header, and never on the file's own table. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Moving the implicit flag onto the serialized document is the right place for it — an explicit [commit] in the file now survives, and the inserted-from-desired path keeps the flag through Item::clone, so a file with no commit key still gets only [commit.generation].
One path doesn't get the flag, and it's a regression against main.
An inline commit table gains a bare [commit] header when its value changes
merge_tables' inline-table branch builds merged from inline.clone().into_table() and hands it to replace_inline_with_table. That table's implicit flag comes from into_table(), not from desired, so the header is emitted. The old post-merge make_commit_table_implicit_if_only_subtables(&mut existing_doc) ran after that replacement and suppressed it; nothing does now.
Starting from commit = { generation = { command = "old" } } and saving a config whose commit.generation.command is "new":
# this head
[commit]
[commit.generation]
command = "new"# a8ed730a5^ (and main)
[commit.generation]
command = "new"Reachable from wt commit's generation-command prompt against a user config that writes commit inline — the same shape test_save_to_existing_file_preserves_nested_inline_table_formatting covers for the unchanged case, which passes either way because the inline table is never replaced there.
Carrying desired's flag onto merged fixes it and stays general — projects written inline would hit the same branch. Green on cargo test --lib config (606) and cargo test --test integration approval_save (24) with the inline suggestion applied.
…changes A `commit` table written inline is rebuilt as a standard table when a value in it changes, and that table took its implicit flag from the inline value rather than from the serialized config. Since the previous commit stopped marking the file's `commit` implicit after the merge, the rewrite gained an empty `[commit]` header above `[commit.generation]`. The rebuilt table now takes the serialized table's flag. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
The inline-table rewrite now takes desired's implicit flag, and that flag also decides whether the comment above the rewritten line survives — for an implicit table it doesn't.
The comment above an inline commit or projects line is dropped
replace_inline_with_table moves the key's leaf decor — the comment lines above commit = { … } — onto the table's header decor, since that is the only place a standard table renders it. An implicit table writes no header, so that decor renders nowhere. Same for the trailing comment after the closing brace, which lands on the header's ].
Starting from
# why we generate
commit = { generation = { command = "old" } } # trailingand saving a config whose commit.generation.command is "new" — set_commit_generation_command, i.e. the wt commit generation prompt:
# this head
[commit.generation]
command = "new"For commit that matches main: the old post-merge make_commit_table_implicit_if_only_subtables suppressed the same header after the same replacement, so both comments were already going. projects is new — the flag reaches it through desired now, where main kept the comment above the bare [projects] header it emitted instead. That one is reachable through set_project_worktree_path (the bare-repo worktree-path prompt) against a hand-written projects = { "…" = { … } }.
The decor has a place to go: the first header the implicit table does write. That keeps both comments and the empty-header fix.
- if let Some(prefix) = prefix {
- table.decor_mut().set_prefix(prefix);
- }
let suffix = existing
.get(key)
.and_then(|item| item.as_inline_table())
.and_then(|inline| inline.decor().suffix())
.filter(|suffix| suffix.as_str().is_some_and(|s| s.contains('#')))
.cloned();
+ // An implicit table writes no header, so decor placed on it renders
+ // nowhere and the comment is lost. It belongs on the first header the
+ // table does write.
+ let decor = match table
+ .is_implicit()
+ .then(|| table.iter_mut().next())
+ .flatten()
+ .and_then(|(_, item)| item.as_table_mut())
+ {
+ Some(first) => first.decor_mut(),
+ None => table.decor_mut(),
+ };
+ if let Some(prefix) = prefix {
+ decor.set_prefix(prefix);
+ }
if let Some(suffix) = suffix {
- table.decor_mut().set_suffix(suffix);
+ decor.set_suffix(suffix);
}That's in replace_inline_with_table in src/config/mod.rs, outside this diff. With it applied the two cases above round-trip as # why we generate / [commit.generation] # trailing and # per-repo overrides / [projects."github.com/o/r"], and it's green on cargo test --lib config (607), cargo test --test integration config_ (460), and cargo test --test integration approval_save (24). The docstring's "Move the prefix to the header" needs the same amendment either way, since for an implicit table there is no header to move it to. Happy to push this here if you'd rather not carry it.
Everything else in the increment checks out: an explicit [commit] survives, the inserted-from-desired path keeps the flag through Item::clone, and the snapshot that lost its bare [projects] is the existing-file path picking up the implicit flag the new-file path always had.
…cit table Rewriting an inline table as a standard one moves the comments on its line onto the table's header. A `commit` or `projects` holding only subtables is implicit and writes no header, so those comments were dropped: always for `commit`, and for `projects` since the save stopped writing a bare `[projects]` header. They now go on the first header such a table writes, `[commit.generation]` or `[projects."…"]`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… whole config (#4160) The four user-config mutations — the two skip-prompt flags, the commit-generation command, a project's `worktree-path` — each change one value, but saving serialized the whole `UserConfig` and diff-merged it into `config.toml`. That merge had to reconstruct everything it didn't mean to change, and #4151 and #4156 spent several review rounds on its cases: trailing comments, values written at their defaults, inline vs standard tables and their headers, pipeline spellings, hook aliases, deprecated spellings. No crate does this merge (the closest, patchloom, fails three of those cases; toml-rs/toml#231 has asked for one since 2021), and tools that edit user-owned TOML — cargo add, uv, poetry, jj, mise — set values by key path instead. Each mutation now returns a `ConfigEdit` (tables, key, value), applied to the file parsed as a `toml_edit` document: - **Only that value changes.** An existing value keeps its decor; a missing table is created implicit, or inline inside an inline table; an existing table is edited in whatever form the file writes it. So a save no longer writes a deprecated key's canonical spelling beside the original (`[select]` gaining `[switch.picker]`, after which `select` warned as unknown on every command), and retired template variables, `pre-create` aliases, and deprecated sections stay as written until `wt config update`. - **An edit the load wouldn't read goes into the migrated file.** A deprecated `[commit-generation]` migrates to `[commit.generation]` only while the canonical table is absent, so writing the command there directly would strand the section's template. `ConfigFile::edited` checks that the edited file loads as the mutated config and otherwise applies the edit after the load-time migrations. That file carries every load-path migration, so it can also move an unrelated deprecated section and drop keys their destinations have no field for — the one place a mutation materializes migrations rather than `wt config update`. It is reported once the write lands, naming each change in the tense of a write that happened (`format_applied_lines`, the past-tense sibling of the load path's renderer). - **The in-memory config is no longer replaced by the file's.** The mutator runs on the file's config to decide what to write and on `self` to keep it current, so system config, `WORKTRUNK_*` variables, and `--config-set` still hold for the rest of the command. Previously a first-run prompt dropped them — `--config-set 'commit.stage="none"'` staged everything once `wt step commit` had asked about commit generation. Removed: the merge engine and whole-struct serialization in `src/config/user/persistence.rs`, the save-side hook-alias rename, and the implicit-header branch of `replace_inline_with_table`, which moves into `deprecation.rs` beside its only caller. The unit tests that pinned the merge go; `test_config_edit_changes_only_its_value` covers the edit's table shapes, and one integration test runs two mutations over a file holding comments, defaults, aliases, a deprecated section, and inline and array-of-tables entries. > _This was written by Claude Code on behalf of max-sixty_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
A config save (declining the shell-integration prompt, setting the commit-generation command, …) deleted any value written out at its default —
skip-shell-integration-prompt = false # note,[list] columns = [], an empty[merge]— comment and all.save_todiffed the serialized config against the file and removed every key the config didn't serialize, and a field at its default serializes to nothing. v0.77.0 behaves the same.The save now also serializes the file's own config, parsed the way the mutation reloads it, and removes a key only when that has it and the config being saved doesn't: the keys the change reset. Defaults written out and schema-unknown keys serialize in neither, so they stay. That replaces the unknown-key preserve tree the save used, so
compute_unknown_treenow serves only the warning path and returnsOption<UnknownTree>.Three related changes the new merge needed:
pre-create/post-createis renamed topre-start/post-startbefore merging, with the rename the load path already applies. Left under the alias, the save wrote the canonical key beside it and the next load failed on the duplicate field. The rename now also covers[projects]entries written inline, which load used to report as an unknown field, and keeps the comment above the key.[projects]or[commit]header, and no longer deletes a[commit]header the file has when that section is empty or holds only[commit.generation]. When an inlinecommitorprojectsholding only subtables is rewritten, the comments on its line move to the first header it writes ([commit.generation],[projects."…"]) rather than being dropped.Still open: a save writes a deprecated key's new spelling beside the old one (
[select]gains[switch.picker], after whichselectwarns as unknown on every command). Fixing it means deciding whether saves apply load-time migrations, which is left for a separate change.🤖 Generated with Claude Code