Skip to content

sdkman emits "Could not find .sdkmanrc" when removing the current worktree #2759

@donvince

Description

@donvince

Summary

Running wt remove from inside the worktree being removed causes sdkman to emit:

Could not find .sdkmanrc

and fail to deactivate the previous worktree's SDK correctly.

Root cause

execute_instant_removal_or_fallback immediately renames the worktree directory (to a staging path) during the binary's execution — before the binary exits and before the shell sources the directive file.

Sequence:

  1. Binary runs → renames worktree dir (.sdkmanrc now gone at old path)
  2. Binary exits
  3. Shell sources directive file → executes cd /main/path
  4. sdkman's chpwd hook fires → looks for .sdkmanrc at the previous (removed) worktree path → not found

The cd directive is written correctly, but the directory is already gone when sdkman checks it.

Expected behaviour

sdkman finds .sdkmanrc at the previous worktree path when its hook fires, deactivates the old SDK cleanly, then loads the SDK from the main worktree's .sdkmanrc.

Proposed fix

When changed_directory=true (user is inside the removed worktree), skip execute_instant_removal_or_fallback and instead write the removal as a backgrounded shell command into the directive file:

( git -C '/main/path' worktree remove '/worktree/path' && git -C '/main/path' branch -d 'branch-name' ) &

This ensures the sequence becomes:

  1. Binary exits
  2. Shell sources directive file:
    a. cd '/main/path' → sdkman fires while .sdkmanrc still exists at the old path ✅
    b. ( git worktree remove ... ) & → deletion runs in background after cd

The changed_directory=false path (removing a different worktree) is unaffected.

Environment

  • wt 0.50.0
  • macOS
  • sdkman with a .sdkmanrc in the worktree being removed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions