Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: write cd directive only after pre-remove hooks succeed (#692)
Maximilian Roos6b6751ec0679
diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 13f632b6f..039e185e3 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -762,18 +762,13 @@ fn handle_removed_worktree_output(background: bool,verify: bool,) -> anyhow::Result<()> {- // 1. Emit cd directive if needed - shell will execute this immediately- if changed_directory {- super::change_directory(main_path)?;- super::flush()?; // Force flush to ensure shell processes the cd- }-// Use main_path for discovery - the worktree being removed might be cwd,// and git operations after removal need a valid working directory.let repo = worktrunk::git::Repository::at(main_path)?;- // Execute pre-remove hooks in the worktree being removed- // Non-zero exit aborts removal (FailFast strategy)+ // Execute pre-remove hooks in the worktree being removed BEFORE writing cd directive.+ // Non-zero exit aborts removal (FailFast strategy).+ // If hooks fail, we don't want the shell to cd to main_path.// For detached HEAD, {{ branch }} expands to "HEAD" in templatesif verify && let Ok(config) = WorktrunkConfig::load() {let ctx = CommandContext::new(@@ -793,6 +788,12 @@ fn handle_removed_worktree_output(execute_pre_remove_commands(&ctx, None, display_path, &[])?;}+ // Emit cd directive only after pre-remove hooks succeed+ if changed_directory {+ super::change_directory(main_path)?;+ super::flush()?; // Force flush to ensure shell processes the cd+ }+// Handle detached HEAD case (no branch known)let Some(branch_name) = branch_name else {// No branch associated - just remove the worktreediff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex b458b5671..76714ef78 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -1231,6 +1231,56 @@ approved-commands = ["exit 1"]);}+/// Pre-remove hook failure should NOT write cd directive.+/// Bug: cd directive was written before pre-remove hooks ran, so if hooks failed,+/// the shell would still cd to main_path even though the worktree wasn't removed.+#[rstest]+fn test_pre_remove_hook_failure_no_cd_directive(mut repo: TestRepo) {+ // Create project config with failing hook+ repo.write_project_config(r#"pre-remove = "exit 1""#);+ repo.commit("Add config");++ // Pre-approve the command+ repo.write_test_config(+ r#"[projects."../origin"]+approved-commands = ["exit 1"]+"#,+ );++ // Create a worktree to remove+ let worktree_path = repo.add_worktree("feature-cd-test");++ // Set up directive file+ let (directive_path, _guard) = directive_file();++ // Run remove from within the worktree (which would trigger cd to main if it worked)+ let mut cmd = repo.wt_command();+ cmd.args(["remove", "--foreground"]);+ cmd.current_dir(&worktree_path);+ configure_directive_file(&mut cmd, &directive_path);+ let output = cmd.output().unwrap();++ // Command should have failed (hook failure)+ assert!(+ !output.status.success(),+ "Remove should fail when pre-remove hook fails"+ );++ // Directive file should be empty (no cd written)+ let directives = std::fs::read_to_string(&directive_path).unwrap_or_default();+ assert!(+ !directives.contains("cd "),+ "Directive file should NOT contain cd when hook fails, got: {}",+ directives+ );++ // Worktree should still exist+ assert!(+ worktree_path.exists(),+ "Worktree should NOT be removed when hook fails"+ );+}+#[rstest]fn test_pre_remove_hook_not_for_branch_only(repo: TestRepo) {// Create a marker file that the hook would create