Skip to content

fix: fire post-switch hooks when removing current worktree - #1452

Merged
max-sixty merged 5 commits into
mainfrom
fix/issue-1450
Mar 12, 2026
Merged

max-sixty merged 5 commits into
mainfrom
fix/issue-1450

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

When removing the current worktree (wt remove from 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_remove creates a fresh Repository::at(main_path), but load_project_config() internally calls current_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 returns None and no project hooks are found.

Solution

Pre-warm the project config cache from main_path before any hook preparation in spawn_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 from load())
  • Repository::load_project_config_at() — loads and caches project config from a specific worktree path

Testing


Closes #1450 — automated triage

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]>
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Mar 11, 2026
@max-sixty

Copy link
Copy Markdown
Owner

thanks, though I'm not sure I understand the solution.

  • firstly: Is it accurate that wt remove never calls post-switch? That would be a bad bug if so...
  • then: why not just call the post-switch hook? why are we loading configs from different files etc? Is this because the config is different between the feature and the main worktree??

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Good questions — let me clarify.

Does wt remove never call post-switch?

No, it does — that logic already exists on main (lines 812–833 of handlers.rs). User-level post-switch hooks fire correctly. The bug is narrower: project-level post-switch hooks (from .config/wt.toml) don't fire when removing the current worktree.

Why the config loading dance?

It's not about different configs between worktrees — it's the same .config/wt.toml file. The problem is how load_project_config() finds that file:

  1. load_project_config() calls current_worktree().root() to locate .config/wt.toml
  2. current_worktree() resolves via the process cwd
  3. When removing the current worktree, the cwd points to a now-deleted directory
  4. So current_worktree().root() returns Err, and the project config is cached as None
  5. When prepare_post_remove_commands and the post-switch hook prep later call load_project_config(), they get the cached None — no project hooks found

The load_project_config_at(main_path) call pre-warms that same cache using an explicit path instead of relying on cwd. After that, the normal load_project_config() calls hit the cache and find the project hooks.

That said — I can see how this feels indirect. An alternative would be to just pass main_path into load_project_config() itself (or have Repository::at(path) set the "current worktree" to path so that cwd isn't consulted). Would you prefer a different approach?

@max-sixty

Copy link
Copy Markdown
Owner

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]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Done — reverted the fix and updated the test to assert the current broken behavior. The test (test_remove_current_worktree_does_not_fire_project_post_switch_hook) now verifies that the marker file does not get created, confirming the bug.

The load_at / load_project_config_at methods are removed — the only change from main is the new test.

max-sixty and others added 2 commits March 11, 2026 21:54
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]>
@max-sixty
max-sixty merged commit 29f1c37 into main Mar 12, 2026
22 checks passed
@max-sixty
max-sixty deleted the fix/issue-1450 branch March 12, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt remove implicitly switches but does not call the post-switch hook

2 participants