Repository navigation
fix: fire post-switch hooks when removing current worktree - #1452
Conversation
When removing the current worktree, spawn_hooks_after_remove creates a new Repository at main_path but load_project_config() uses the global current_worktree() (process cwd) to find .config/wt.toml. Since the cwd worktree has been removed, the config lookup fails silently and no project hooks fire. Fix: pre-warm the project config cache from main_path before preparing hooks, so all subsequent load_project_config() calls find the config. Adds ProjectConfig::load_at() and Repository::load_project_config_at() to support loading config from a specific worktree path. Closes #1450 Co-authored-by: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
|
thanks, though I'm not sure I understand the solution.
|
|
Good questions — let me clarify. Does No, it does — that logic already exists on Why the config loading dance? It's not about different configs between worktrees — it's the same
The That said — I can see how this feels indirect. An alternative would be to just pass |
|
can you comment out the solution and create a test which demonstrates the current behavior — i.e. that post-switch is not called? |
Comment out the solution per review request: the test now asserts the current (broken) behavior — that project-level post-switch hooks do NOT fire when removing the current worktree. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Done — reverted the fix and updated the test to assert the current broken behavior. The test ( The |
The test now writes and commits .config/wt.toml before creating the feature worktree, so the config exists in both worktrees. This confirms the bug is purely about the deleted cwd causing load_project_config() to fail, not about the config file being absent from the feature worktree. The test currently fails (expected) — it asserts the desired behavior. Co-Authored-By: Claude <[email protected]>
`spawn_hooks_after_remove` was creating a fresh `Repository::at(main_path)` with an empty cache. When it called `load_project_config()`, the method resolved via `current_worktree()` — the process cwd, which points to the now-deleted feature worktree. The config lookup failed silently, so no project hooks fired. Fix: pass the existing `&Repository` from `handle_removed_worktree_output` into `spawn_hooks_after_remove`. Pre-remove hooks already called `load_project_config()` on this repo (while the cwd still existed), warming the `OnceCell` cache. Post-remove and post-switch hooks now find the cached config. Closes #1450 Co-Authored-By: Claude <[email protected]>
Problem
When removing the current worktree (
wt removefrom within the worktree being removed), post-switch hooks don't fire even though the user is implicitly switched back to the primary worktree.Root cause:
spawn_hooks_after_removecreates a freshRepository::at(main_path), butload_project_config()internally callscurrent_worktree()which uses the global process cwd — the removed worktree. Since that path no longer exists (or lacks.config/wt.toml), the config lookup silently returnsNoneand no project hooks are found.Solution
Pre-warm the project config cache from
main_pathbefore any hook preparation inspawn_hooks_after_remove. This ensures both post-remove and post-switch hooks can find the project config.Added:
ProjectConfig::load_at()— loads config from a specific worktree root (refactored fromload())Repository::load_project_config_at()— loads and caches project config from a specific worktree pathTesting
test_remove_current_worktree_fires_post_switch_hook— reproduces the exact scenario from wt remove implicitly switches but does not call the post-switch hook #1450 (create worktree, cd into it, remove it, verify post-switch hook fires)Closes #1450 — automated triage