Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: spawn background pipelines per hook-type (#1904)
Maximilian Roosc1815cf258d2
diff --git a/src/commands/handle_switch.rs b/src/commands/handle_switch.rsindex d905bb588..619751c1a 100644--- a/src/commands/handle_switch.rs+++ b/src/commands/handle_switch.rs@@ -184,8 +184,12 @@ pub(crate) fn spawn_switch_background_hooks(let ctx = CommandContext::new(repo, config, branch, result.path(), yes);let mut flat_hooks = Vec::new();- let mut pipeline_hooks = Vec::new();+ // Spawn each hook type's pipeline independently — pipelines carry a single+ // hook_type/source/context, so mixing types in one runner would give later+ // steps the wrong template variables (e.g., {{ hook_type }}).+ // Flat hooks are spawned as independent processes, so accumulating them+ // across hook types for a combined display message is fine.match super::hooks::prepare_background_hooks(&ctx,HookType::PostSwitch,@@ -193,7 +197,9 @@ pub(crate) fn spawn_switch_background_hooks(hooks_display_path,)? {super::hooks::PreparedHooks::Flat(cmds) => flat_hooks.extend(cmds),- super::hooks::PreparedHooks::Pipeline(steps) => pipeline_hooks.extend(steps),+ super::hooks::PreparedHooks::Pipeline(steps) => {+ super::hooks::spawn_hook_pipeline(&ctx, steps)?;+ }}if matches!(result, SwitchResult::Created { .. }) {@@ -204,14 +210,12 @@ pub(crate) fn spawn_switch_background_hooks(hooks_display_path,)? {super::hooks::PreparedHooks::Flat(cmds) => flat_hooks.extend(cmds),- super::hooks::PreparedHooks::Pipeline(steps) => pipeline_hooks.extend(steps),+ super::hooks::PreparedHooks::Pipeline(steps) => {+ super::hooks::spawn_hook_pipeline(&ctx, steps)?;+ }}}- // Spawn pipeline hooks as compound commands, flat hooks as independent processes- if !pipeline_hooks.is_empty() {- super::hooks::spawn_hook_pipeline(&ctx, pipeline_hooks)?;- }super::hooks::spawn_background_hooks(&ctx, flat_hooks)}diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex 113f51e26..bd802b5f5 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -2557,3 +2557,33 @@ fn test_user_post_start_pipeline_shell_escaping(repo: TestRepo) {"Quotes should survive escaping, got: {content}");}++#[rstest]+fn test_user_post_switch_pipeline_via_switch_create(repo: TestRepo) {+ // Post-switch with pipeline config, triggered by `wt switch --create`.+ // This exercises the pipeline branch in `spawn_switch_background_hooks`,+ // which spawns each hook type's pipeline independently.+ repo.write_test_config(+ r#"post-switch = [+ "echo SWITCH_STEP_1 > switch_step1.txt",+ { check = "cat switch_step1.txt > switch_step2.txt" }+]+"#,+ );++ snapshot_switch(+ "user_post_switch_pipeline_via_create",+ &repo,+ &["--create", "feature"],+ );++ let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");+ let step2_file = worktree_path.join("switch_step2.txt");+ wait_for_file_content(&step2_file);++ let content = fs::read_to_string(&step2_file).unwrap();+ assert!(+ content.contains("SWITCH_STEP_1"),+ "Pipeline serial ordering should be preserved for post-switch, got: {content}"+ );+}diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_post_switch_pipeline_via_create.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_switch_pipeline_via_create.snapnew file mode 100644index 000000000..f68acd762--- /dev/null+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_switch_pipeline_via_create.snap@@ -0,0 +1,50 @@+---+source: tests/integration_tests/user_hooks.rs+info:+ program: wt+ args:+ - switch+ - "--create"+ - feature+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""+ PATH: "[PATH]"+ PSModulePath: ""+ RUST_LOG: warn+ SHELL: ""+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"+ WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[32m✓[39m [32mCreated branch [1mfeature[22m from [1mmain[22m and worktree @ [1m_REPO_.feature[22m[39m+[2m↳[22m [2mTo customize worktree locations, run [4mwt config create[24m[22m+[33m▲[39m [33mCannot change directory — shell integration not installed[39m+[2m↳[22m [2mTo enable automatic cd, run [4mwt config shell install[24m[22m+[36m◎[39m [36mRunning post-switch: [1mcmd-0[22m → [1mcheck[22m @ [1m_REPO_.feature[22m[39m