Skip to content

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

fix: `wt hook post-start` spawns in background instead of blocking (#335)

Maximilian Roos2edd19f9252a
diff --git a/src/commands/hooks.rs b/src/commands/hooks.rs
index a77d65918..40581928e 100644
--- a/src/commands/hooks.rs
+++ b/src/commands/hooks.rs
@@ -215,17 +215,9 @@ pub fn spawn_hook_commands_background(
Ok(())
}
-/// A single hook command failure (for concurrent execution).
-#[derive(Debug, Clone)]
-struct HookFailure {
- name: Option<String>,
- error: String,
- exit_code: Option<i32>,
-}
-
/// Check if a name filter was provided but no commands matched.
/// Returns an error listing available command names if so.
-fn check_name_filter_matched(
+pub(crate) fn check_name_filter_matched(
name_filter: Option<&str>,
total_commands_run: usize,
user_config: Option<&CommandConfig>,
@@ -364,140 +356,6 @@ pub fn run_hook_with_filter(
Ok(())
}
-/// Run user and project hooks concurrently (for hook types that normally run in background).
-///
-/// All commands from both sources run in parallel together. Collects all failures and returns
-/// a combined error at the end. Handles name filtering and returns an error if a name
-/// filter was provided but no matching command found.
-pub fn run_hook_concurrent_with_filter(
- ctx: &CommandContext,
- user_config: Option<&CommandConfig>,
- project_config: Option<&CommandConfig>,
- hook_type: HookType,
- extra_vars: &[(&str, &str)],
- name_filter: Option<&str>,
-) -> anyhow::Result<()> {
- use rayon::prelude::*;
- use std::process::Stdio;
- use worktrunk::shell_exec::ShellConfig;
-
- let commands = prepare_hook_commands(
- ctx,
- user_config,
- project_config,
- hook_type,
- extra_vars,
- name_filter,
- )?;
-
- check_name_filter_matched(name_filter, commands.len(), user_config, project_config)?;
-
- if commands.is_empty() {
- return Ok(());
- }
-
- // Announce all commands upfront
- for cmd in &commands {
- cmd.announce()?;
- }
- crate::output::flush()?;
-
- // Reset ANSI codes to prevent color bleeding from our output into command output
- use std::io::Write;
- use worktrunk::styling::{eprint, stderr};
- eprint!("{}", anstyle::Reset);
- stderr().flush().ok();
-
- // Run commands concurrently using Rayon's thread pool.
- //
- // Note: Unlike sequential execution (execute_streaming), we don't use process_group(0)
- // or sophisticated signal forwarding here. Children inherit the foreground process group,
- // so Ctrl+C sends SIGINT to all of them together. This simpler behavior is acceptable for
- // "best effort" concurrent hooks - if the user interrupts, everything stops.
- let shell = ShellConfig::get();
- let worktree_path = ctx.worktree_path;
-
- let all_failures: Vec<HookFailure> = commands
- .par_iter()
- .filter_map(|cmd| {
- use std::io::Write;
-
- let prepared = &cmd.prepared;
- let mut child_cmd = shell.command(&prepared.expanded);
- child_cmd
- .current_dir(worktree_path)
- .stdin(Stdio::piped())
- .stdout(Stdio::from(std::io::stderr()))
- .stderr(Stdio::inherit())
- .env_remove(worktrunk::shell_exec::DIRECTIVE_FILE_ENV_VAR);
-
- let mut child = match child_cmd.spawn() {
- Ok(child) => child,
- Err(e) => {
- return Some(HookFailure {
- name: prepared.name.clone(),
- error: e.to_string(),
- exit_code: None,
- });
- }
- };
-
- // Pipe context JSON to stdin (same as sequential execution)
- if let Some(mut stdin) = child.stdin.take() {
- // Ignore write errors - command may not read stdin
- let _ = stdin.write_all(prepared.context_json.as_bytes());
- }
-
- match child.wait() {
- Ok(status) if status.success() => None,
- Ok(status) => Some(HookFailure {
- name: prepared.name.clone(),
- error: format!("exit status: {}", status.code().unwrap_or(-1)),
- exit_code: status.code(),
- }),
- Err(e) => Some(HookFailure {
- name: prepared.name.clone(),
- error: e.to_string(),
- exit_code: None,
- }),
- }
- })
- .collect();
-
- // Report all failures at the end
- if !all_failures.is_empty() {
- let first = &all_failures[0];
- // For single failure, use command_name field (no duplication in error).
- // For multiple failures, list all names in the error message.
- let (command_name, error_msg) = if all_failures.len() == 1 {
- (first.name.clone(), first.error.clone())
- } else {
- let names: Vec<_> = all_failures
- .iter()
- .map(|f| f.name.as_deref().unwrap_or("(unnamed)"))
- .collect();
- (
- None, // Multiple failures - no single command to blame
- format!(
- "{} commands failed: {}",
- all_failures.len(),
- names.join(", ")
- ),
- )
- };
-
- return Err(WorktrunkError::HookCommandFailed {
- hook_type,
- command_name,
- error: error_msg,
- exit_code: first.exit_code,
- }
- .into());
- }
-
- Ok(())
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index 86c38d9f7..e8f7a2e1d 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -13,7 +13,10 @@ use worktrunk::styling::{
use super::command_executor::CommandContext;
use super::commit::{CommitGenerator, CommitOptions};
use super::context::CommandEnv;
-use super::hooks::{HookFailureStrategy, run_hook_concurrent_with_filter, run_hook_with_filter};
+use super::hooks::{
+ HookFailureStrategy, check_name_filter_matched, prepare_hook_commands, run_hook_with_filter,
+ spawn_hook_commands_background,
+};
use super::merge::{
execute_post_merge_commands, execute_pre_remove_commands, run_pre_merge_commands,
};
@@ -90,36 +93,40 @@ pub fn run_hook(hook_type: HookType, yes: bool, name_filter: Option<&str>) -> an
)
}
HookType::PostStart => {
- // post-start hooks run concurrently (matching their normal background behavior)
+ // post-start hooks spawn in background (matching their normal behavior during switch)
let user_config = user_hook!(post_start);
let project_config = project_config
.as_ref()
.and_then(|c| c.hooks.post_start.as_ref());
require_hooks(user_config, project_config, hook_type)?;
- run_hook_concurrent_with_filter(
+ let commands = prepare_hook_commands(
&ctx,
user_config,
project_config,
hook_type,
&[],
name_filter,
- )
+ )?;
+ check_name_filter_matched(name_filter, commands.len(), user_config, project_config)?;
+ spawn_hook_commands_background(&ctx, commands, hook_type)
}
HookType::PostSwitch => {
- // post-switch hooks run concurrently (matching their normal background behavior)
+ // post-switch hooks spawn in background (matching their normal behavior during switch)
let user_config = user_hook!(post_switch);
let project_config = project_config
.as_ref()
.and_then(|c| c.hooks.post_switch.as_ref());
require_hooks(user_config, project_config, hook_type)?;
- run_hook_concurrent_with_filter(
+ let commands = prepare_hook_commands(
&ctx,
user_config,
project_config,
hook_type,
&[],
name_filter,
- )
+ )?;
+ check_name_filter_matched(name_filter, commands.len(), user_config, project_config)?;
+ spawn_hook_commands_background(&ctx, commands, hook_type)
}
HookType::PreCommit => {
let user_config = user_hook!(pre_commit);
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index ffc2b987f..38355412b 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -7,8 +7,8 @@
//! - Skipped together with project hooks via --no-verify
use crate::common::{
- TestRepo, make_snapshot_cmd, repo, setup_snapshot_settings, wait_for_file,
- wait_for_file_content,
+ TestRepo, make_snapshot_cmd, repo, resolve_git_common_dir, setup_snapshot_settings,
+ wait_for_file, wait_for_file_content, wait_for_file_count,
};
use insta_cmd::assert_cmd_snapshot;
use rstest::rstest;
@@ -784,9 +784,9 @@ fn test_standalone_hook_post_start(repo: TestRepo) {
let output = cmd.output().unwrap();
assert!(output.status.success(), "wt hook post-start should succeed");
- // Hook should have run
+ // Hook spawns in background - wait for marker file
let marker = repo.root_path().join("hook_ran.txt");
- assert!(marker.exists(), "post-start hook should have run");
+ wait_for_file_content(&marker, Duration::from_secs(5));
let content = fs::read_to_string(&marker).unwrap();
assert!(content.contains("STANDALONE_POST_START"));
}
@@ -835,14 +835,14 @@ fn test_standalone_hook_no_hooks_configured(repo: TestRepo) {
}
// ============================================================================
-// Concurrent Hook Execution Tests (post-start, post-switch)
+// Background Hook Execution Tests (post-start, post-switch)
// ============================================================================
-/// Test that a single failing concurrent hook reports the failure
+/// Test that a single failing background hook logs its output
#[rstest]
fn test_concurrent_hook_single_failure(repo: TestRepo) {
- // Write project config with a failing post-start hook
- repo.write_project_config(r#"post-start = "exit 1""#);
+ // Write project config with a hook that writes output before failing
+ repo.write_project_config(r#"post-start = "echo HOOK_OUTPUT_MARKER; exit 1""#);
let mut cmd = crate::common::wt_command();
cmd.current_dir(repo.root_path());
@@ -850,26 +850,42 @@ fn test_concurrent_hook_single_failure(repo: TestRepo) {
cmd.args(["hook", "post-start", "--yes"]);
let output = cmd.output().unwrap();
+ // Background spawning always succeeds (spawn succeeded, failure is logged)
assert!(
- !output.status.success(),
- "wt hook post-start should fail when hook exits with error"
+ output.status.success(),
+ "wt hook post-start should succeed (spawns in background)"
);
- let stderr = String::from_utf8_lossy(&output.stderr);
+ // Wait for log file to be created and contain output
+ let log_dir = resolve_git_common_dir(repo.root_path()).join("wt-logs");
+ wait_for_file_count(&log_dir, "log", 1, Duration::from_secs(5));
+
+ // Find and read the log file
+ let log_file = fs::read_dir(&log_dir)
+ .unwrap()
+ .filter_map(|e| e.ok())
+ .find(|e| e.path().extension().is_some_and(|ext| ext == "log"))
+ .expect("Should have a log file");
+
+ // Wait for content to be written (command runs async)
+ wait_for_file_content(&log_file.path(), Duration::from_secs(5));
+ let log_content = fs::read_to_string(log_file.path()).unwrap();
+
+ // Verify the hook actually ran and wrote output (not just that file was created)
assert!(
- stderr.contains("exit status: 1"),
- "Error should mention exit status, got: {stderr}"
+ log_content.contains("HOOK_OUTPUT_MARKER"),
+ "Log should contain hook output, got: {log_content}"
);
}
-/// Test that multiple failing concurrent hooks report all failures
+/// Test that multiple background hooks each get their own log file with correct content
#[rstest]
fn test_concurrent_hook_multiple_failures(repo: TestRepo) {
- // Write project config with multiple named failing hooks (table format)
+ // Write project config with multiple named hooks (table format)
repo.write_project_config(
r#"[post-start]
-first = "exit 1"
-second = "exit 2"
+first = "echo FIRST_OUTPUT"
+second = "echo SECOND_OUTPUT"
"#,
);
@@ -879,24 +895,55 @@ second = "exit 2"
cmd.args(["hook", "post-start", "--yes"]);
let output = cmd.output().unwrap();
+ // Background spawning always succeeds (spawn succeeded)
assert!(
- !output.status.success(),
- "wt hook post-start should fail when hooks exit with error"
+ output.status.success(),
+ "wt hook post-start should succeed (spawns in background)"
);
- let stderr = String::from_utf8_lossy(&output.stderr);
- // Should report multiple failures
- assert!(
- stderr.contains("2 commands failed"),
- "Error should mention 2 commands failed, got: {stderr}"
- );
- assert!(
- stderr.contains("first") && stderr.contains("second"),
- "Error should list both failed command names, got: {stderr}"
- );
+ // Wait for both log files to be created
+ let log_dir = resolve_git_common_dir(repo.root_path()).join("wt-logs");
+ wait_for_file_count(&log_dir, "log", 2, Duration::from_secs(5));
+
+ // Collect log files and their contents
+ let log_files: Vec<_> = fs::read_dir(&log_dir)
+ .unwrap()
+ .filter_map(|e| e.ok())
+ .filter(|e| e.path().extension().is_some_and(|ext| ext == "log"))
+ .collect();
+ assert_eq!(log_files.len(), 2, "Should have 2 log files");
+
+ // Wait for content in both log files
+ for log_file in &log_files {
+ wait_for_file_content(&log_file.path(), Duration::from_secs(5));
+ }
+
+ // Collect all log contents
+ let mut found_first = false;
+ let mut found_second = false;
+ for log_file in &log_files {
+ let name = log_file.file_name().to_string_lossy().to_string();
+ let content = fs::read_to_string(log_file.path()).unwrap();
+ if name.contains("first") {
+ assert!(
+ content.contains("FIRST_OUTPUT"),
+ "first log should contain FIRST_OUTPUT, got: {content}"
+ );
+ found_first = true;
+ }
+ if name.contains("second") {
+ assert!(
+ content.contains("SECOND_OUTPUT"),
+ "second log should contain SECOND_OUTPUT, got: {content}"
+ );
+ found_second = true;
+ }
+ }
+ assert!(found_first, "Should have log for 'first' hook");
+ assert!(found_second, "Should have log for 'second' hook");
}
-/// Test that user and project post-start hooks both run concurrently
+/// Test that user and project post-start hooks both run in background
#[rstest]
fn test_concurrent_hook_user_and_project(repo: TestRepo) {
// Write user config with post-start hook (using table format for named hook)
@@ -921,15 +968,12 @@ user = "echo 'USER_HOOK' > user_hook_ran.txt"
String::from_utf8_lossy(&output.stderr)
);
- // Both hooks should have run
+ // Both hooks spawn in background - wait for marker files
let user_marker = repo.root_path().join("user_hook_ran.txt");
let project_marker = repo.root_path().join("project_hook_ran.txt");
- assert!(user_marker.exists(), "user post-start hook should have run");
- assert!(
- project_marker.exists(),
- "project post-start hook should have run"
- );
+ wait_for_file_content(&user_marker, Duration::from_secs(5));
+ wait_for_file_content(&project_marker, Duration::from_secs(5));
let user_content = fs::read_to_string(&user_marker).unwrap();
let project_content = fs::read_to_string(&project_marker).unwrap();
@@ -937,7 +981,7 @@ user = "echo 'USER_HOOK' > user_hook_ran.txt"
assert!(project_content.contains("PROJECT_HOOK"));
}
-/// Test that post-switch hooks also run concurrently
+/// Test that post-switch hooks also run in background
#[rstest]
fn test_concurrent_hook_post_switch(repo: TestRepo) {
// Write project config with post-switch hook
@@ -954,14 +998,14 @@ fn test_concurrent_hook_post_switch(repo: TestRepo) {
"wt hook post-switch should succeed"
);
- // Hook should have run
+ // Hook spawns in background - wait for marker file
let marker = repo.root_path().join("hook_ran.txt");
- assert!(marker.exists(), "post-switch hook should have run");
+ wait_for_file_content(&marker, Duration::from_secs(5));
let content = fs::read_to_string(&marker).unwrap();
assert!(content.contains("POST_SWITCH"));
}
-/// Test that concurrent hooks work with name filter
+/// Test that background hooks work with name filter
#[rstest]
fn test_concurrent_hook_with_name_filter(repo: TestRepo) {
// Write project config with multiple named hooks
@@ -985,11 +1029,14 @@ second = "echo 'SECOND' > second.txt"
String::from_utf8_lossy(&output.stderr)
);
- // Only the first hook should have run
+ // First hook spawns in background - wait for marker file
let first_marker = repo.root_path().join("first.txt");
let second_marker = repo.root_path().join("second.txt");
- assert!(first_marker.exists(), "first hook should have run");
+ wait_for_file_content(&first_marker, Duration::from_secs(5));
+
+ // Fixed sleep for absence check - second hook should NOT have run
+ thread::sleep(SLEEP_FOR_ABSENCE_CHECK);
assert!(!second_marker.exists(), "second hook should NOT have run");
}