Skip to content

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

fix(config): refuse to publish a config migration the preview no longer describes (#4127)

Worktrunk Bot370ac833919a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 80c0df8db..ea0ebb500 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@
### Fixed
+- **`wt config update` no longer overwrites an edit made while its confirmation prompt is open**: the migration was computed before the preview and written after the answer, so a config saved from another terminal during that wait was replaced by the pre-prompt snapshot — silently, with `✓ Updated user config` reported. The command now re-reads each config immediately before writing and fails with the path when the contents have moved on, leaving the newer file in place; re-running previews the current contents.
+
- **`wt config shell install fish` writes the wrapper where fish reads it under a custom `$XDG_CONFIG_HOME`**: the wrapper path was hardcoded to `~/.config/fish/functions/`, while the completion path resolved the XDG config directory — same tool, same command, two answers. With `$XDG_CONFIG_HOME` pointing anywhere but `~/.config`, install reported success for both files, fish autoloaded the completion and never saw the wrapper, so `wt switch` changed no directory and `wt config show` reported the integration as installed. The wrapper, the completion, and the directories `wt config shell uninstall` and `wt config show` scan now all come from one resolution of fish's `$__fish_config_dir`. On Unix an unset or default `$XDG_CONFIG_HOME` is unaffected; on Windows the fish completion moves off `%APPDATA%\fish\completions` onto the same path as the wrapper. The deprecated `conf.d` file install cleans up stays pinned to `~/.config/fish/conf.d`, the only place worktrunk ever wrote it.
- **`wt remove`, `wt merge`, and `wt step prune` no longer discard untracked files hidden by Git configuration**: `status.showUntrackedFiles = no` made the clean-worktree guard inherit a display preference, so a worktree containing only untracked files appeared clean and could be removed without `--force` — including by `wt step prune`, which removes unattended. Safety checks now always include untracked files and name the path that blocks removal. Auto-staging warnings for `wt merge`, `wt step commit`, and `wt step squash` likewise override the setting and enumerate every file that will be staged.
diff --git a/src/commands/config/update.rs b/src/commands/config/update.rs
index 5faeb54a7..0dc575156 100644
--- a/src/commands/config/update.rs
+++ b/src/commands/config/update.rs
@@ -74,6 +74,29 @@ pub fn handle_config_update(yes: bool, output: Option<PathBuf>) -> anyhow::Resul
}
}
+ // A preview authorizes migrating the content it was computed from. An edit
+ // that completes while the prompt is open would be replaced by that earlier
+ // snapshot, so re-read each config and fail rather than publish a stale
+ // migration. Every candidate is checked before any is written, so one
+ // superseded config can't leave the other half-applied. The window between
+ // this check and the rename stays open to a non-cooperating editor, the same
+ // boundary the shell-config writes accept.
+ for candidate in &candidates {
+ let current = std::fs::read_to_string(&candidate.config_path).with_context(|| {
+ format!(
+ "Failed to re-read {}",
+ candidate.info.label().to_lowercase()
+ )
+ })?;
+ if current != candidate.original {
+ bail!(cformat!(
+ "{} changed @ <bold>{}</> since the preview; re-run <bold>wt config update</> to migrate the current contents",
+ candidate.info.label(),
+ format_path_for_display(&candidate.config_path)
+ ));
+ }
+ }
+
for candidate in &candidates {
// Preserve approved-commands before rewriting config (migrated content
// drops them; approvals.toml becomes the authoritative source). Abort
diff --git a/tests/integration_tests/config_update_pty.rs b/tests/integration_tests/config_update_pty.rs
index f4f1a9847..e9cde58b2 100644
--- a/tests/integration_tests/config_update_pty.rs
+++ b/tests/integration_tests/config_update_pty.rs
@@ -3,7 +3,9 @@
//!
//! Tests the accept/decline flow in a real TTY environment.
-use crate::common::pty::{build_pty_command, exec_cmd_in_pty_prompted};
+use crate::common::pty::{
+ build_pty_command, exec_cmd_in_pty_prompted, exec_cmd_in_pty_prompted_with,
+};
use crate::common::{TestRepo, add_pty_filters, repo, setup_snapshot_settings, wt_bin};
use insta::assert_snapshot;
use rstest::rstest;
@@ -82,3 +84,80 @@ json-schema = 1
let content = fs::read_to_string(config_path).unwrap();
assert_eq!(content, original_content, "Config should be unchanged");
}
+
+/// The preview authorizes the migration of the content it was computed from.
+/// An edit that completes while the prompt is open must not be replaced by
+/// that earlier snapshot.
+#[rstest]
+fn test_config_update_rejects_preview_stale_at_apply(repo: TestRepo) {
+ let config_path = repo.test_config_path().to_path_buf();
+ let original = r#"worktree-path = "../{{ main_worktree }}.{{ branch }}"
+"#;
+ fs::write(&config_path, original).unwrap();
+
+ let edited = format!("{original}\n# preserve this edit\n");
+ let callback_path = config_path.clone();
+ let callback_content = edited.clone();
+ let cmd = build_pty_command(
+ wt_bin().to_str().unwrap(),
+ &["config", "update"],
+ repo.root_path(),
+ &repo.test_env_vars(),
+ None,
+ );
+ let (output, exit_code) = exec_cmd_in_pty_prompted_with(cmd, &["y\n"], "[y/N", move |_| {
+ fs::write(&callback_path, &callback_content).unwrap();
+ });
+
+ assert_ne!(
+ exit_code, 0,
+ "applying a preview of superseded content should fail:\n{output}"
+ );
+ assert!(
+ output.contains("since the preview"),
+ "failure should name the superseded preview:\n{output}"
+ );
+ assert_eq!(
+ fs::read_to_string(&config_path).unwrap(),
+ edited,
+ "the edit made while the prompt was open should survive"
+ );
+}
+
+/// Removing the config while the prompt is open is the same staleness, at its
+/// limit: the command fails rather than recreating the file from the preview.
+#[rstest]
+fn test_config_update_rejects_config_removed_at_apply(repo: TestRepo) {
+ let config_path = repo.test_config_path().to_path_buf();
+ fs::write(
+ &config_path,
+ r#"worktree-path = "../{{ main_worktree }}.{{ branch }}"
+"#,
+ )
+ .unwrap();
+
+ let callback_path = config_path.clone();
+ let cmd = build_pty_command(
+ wt_bin().to_str().unwrap(),
+ &["config", "update"],
+ repo.root_path(),
+ &repo.test_env_vars(),
+ None,
+ );
+ let (output, exit_code) = exec_cmd_in_pty_prompted_with(cmd, &["y\n"], "[y/N", move |_| {
+ fs::remove_file(&callback_path).unwrap();
+ });
+
+ assert_ne!(
+ exit_code, 0,
+ "applying a preview of a removed config should fail:\n{output}"
+ );
+ assert!(
+ output.contains("Failed to re-read user config"),
+ "failure should name the unreadable config:\n{output}"
+ );
+ assert!(
+ !config_path.exists(),
+ "the removed config should not be recreated from the preview"
+ );
+}