Skip to content

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

fix(shell): preview legacy-file removal in the wt switch first-run offer (#3656)

Worktrunk Bot4485312c50f6
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rs
index b5b28bf9c..9f2af5e4f 100644
--- a/src/commands/configure_shell.rs
+++ b/src/commands/configure_shell.rs
@@ -300,7 +300,7 @@ fn cleanup_stranded_nushell(
/// removing them, so a `--dry-run` preview and the install confirmation can name
/// the deletions before they happen (issue #3644). With `dry_run` false, removes
/// them and returns what was removed.
-fn collect_legacy_cleanups(
+pub(crate) fn collect_legacy_cleanups(
configured: &[ConfigureResult],
cmd: &str,
dry_run: bool,
diff --git a/src/output/shell_integration.rs b/src/output/shell_integration.rs
index 3269993a5..1502ffef1 100644
--- a/src/output/shell_integration.rs
+++ b/src/output/shell_integration.rs
@@ -75,8 +75,8 @@ use worktrunk::styling::{
};
use crate::commands::configure_shell::{
- ConfigAction, UninstallScanResult, format_matched_lines, handle_configure_shell,
- prompt_for_install, scan_shell_configs,
+ ConfigAction, UninstallScanResult, collect_legacy_cleanups, format_matched_lines,
+ handle_configure_shell, prompt_for_install, scan_shell_configs,
};
/// Git config key tracking how many times the shell-integration install hint
@@ -482,13 +482,17 @@ pub fn prompt_shell_integration(
// TTY + first time: Show interactive prompt
// Accepting installs for all shells with config files (same as `wt config shell install`)
- // This first-run offer installs but resolves no legacy cleanups of its own;
- // the subsequent handle_configure_shell reports any deprecated-file removal
- // after the fact, as before.
+ // Detect (without removing) the legacy files the subsequent handle_configure_shell
+ // would delete, so this offer names them before the user consents — the removal is
+ // destructive and must not happen unpreviewed, exactly as `wt config shell install`
+ // now previews it (issue #3644). The list is computed from the same dry-run scan
+ // handle_configure_shell re-derives internally, so the prompt names precisely what
+ // the install removes.
+ let legacy_preview = collect_legacy_cleanups(&scan.configured, binary_name, true);
let confirmed = prompt_for_install(
&scan.configured,
&scan.completion_results,
- &[],
+ &legacy_preview,
binary_name,
"Install shell integration?",
)
diff --git a/tests/integration_tests/shell_integration_prompt.rs b/tests/integration_tests/shell_integration_prompt.rs
index f58130dc3..01c7c0ceb 100644
--- a/tests/integration_tests/shell_integration_prompt.rs
+++ b/tests/integration_tests/shell_integration_prompt.rs
@@ -548,6 +548,72 @@ mod pty_tests {
});
}
+ /// Test: the first-run offer names a legacy file it will delete before the
+ /// user consents.
+ ///
+ /// Regression for the `wt config shell install` preview (issue #3644) not
+ /// reaching the `wt switch` first-run offer. Setup: bash is unconfigured, so
+ /// the offer fires; fish is already configured at the canonical
+ /// `functions/wt.fish` while a deprecated `conf.d/wt.fish` lingers. Accepting
+ /// the offer would remove that legacy file, so its preview must name the
+ /// removal — otherwise `wt switch` deletes a file the prompt never mentioned.
+ #[rstest]
+ fn test_first_run_offer_previews_legacy_removal(repo: TestRepo) {
+ let temp_home = TempDir::new().unwrap();
+
+ // bash: unconfigured (empty rc) → current shell not installed → offer fires.
+ let bashrc = temp_home.path().join(".bashrc");
+ fs::write(&bashrc, "# empty bashrc\n").unwrap();
+
+ // fish: already configured at the canonical functions/wt.fish location.
+ let functions = temp_home.path().join(".config/fish/functions");
+ fs::create_dir_all(&functions).unwrap();
+ let wrapper = worktrunk::shell::ShellInit::with_prefix(
+ worktrunk::shell::Shell::Fish,
+ "wt".to_string(),
+ )
+ .generate_fish_wrapper()
+ .unwrap();
+ fs::write(functions.join("wt.fish"), format!("{wrapper}\n")).unwrap();
+
+ // fish: a deprecated conf.d/wt.fish that accepting the offer would delete.
+ let conf_d = temp_home.path().join(".config/fish/conf.d");
+ fs::create_dir_all(&conf_d).unwrap();
+ let legacy_file = conf_d.join("wt.fish");
+ fs::write(&legacy_file, "wt config shell init fish | source").unwrap();
+
+ let mut env_vars = repo.test_env_vars();
+ env_vars.retain(|(k, _)| k != "WORKTRUNK_DIRECTIVE_FILE");
+ env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));
+
+ let cmd = build_pty_command(
+ wt_bin().to_str().unwrap(),
+ &["switch", "--create", "feature"],
+ repo.root_path(),
+ &env_vars,
+ Some(temp_home.path()),
+ );
+ // Request the preview, then decline so nothing is actually removed.
+ let (output, exit_code) = exec_cmd_in_pty_prompted(cmd, &["?\n", "n\n"], "[y/N");
+
+ assert_eq!(exit_code, 0);
+ assert!(
+ output.contains("Install shell integration"),
+ "Should show the first-run offer: {output}"
+ );
+ // The preview must name the deprecated fish file the install would remove.
+ assert!(
+ output.contains("Will remove") && output.contains("conf.d/wt.fish"),
+ "First-run offer preview must name the legacy fish removal: {output}"
+ );
+
+ // Declining leaves the legacy file in place — the preview did not delete it.
+ assert!(
+ legacy_file.exists(),
+ "Declining should preserve the legacy file: {legacy_file:?}"
+ );
+ }
+
/// Test: Second switch after first prompt → no prompt
#[rstest]
fn test_no_prompt_after_first_prompt(repo: TestRepo) {