Skip to content

feat(switch): preserve subdirectory position when switching worktrees - #939

Merged
max-sixty merged 4 commits into
mainfrom
cd-mono
Feb 6, 2026
Merged

max-sixty merged 4 commits into
mainfrom
cd-mono

Conversation

@max-sixty

@max-sixty max-sixty commented Feb 5, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • When switching worktrees from a subdirectory (e.g., apps/gateway/), preserve the position by cd-ing into the equivalent subdirectory of the target worktree
  • Falls back silently to the worktree root if the subdirectory doesn't exist in the target
  • Applies to both wt switch and wt select
  • resolve_subdir_in_target accepts cwd as a parameter (no global state reads in the output layer)

Closes #937

Test plan

  • test_switch_preserves_subdir — switches from apps/gateway/, verifies directive cd's to subdir in target
  • test_switch_falls_back_to_root_when_subdir_missing — subdir only in source, verifies fallback to root
  • test_switch_create_preserves_subdir — --create from a committed subdir, verifies preservation
  • 4 unit tests for resolve_subdir_in_target (no source root, subdir exists, subdir missing, at root)
  • All 1004 existing integration tests pass
  • Windows CI passes (path separator handling verified)

This was written by Claude Code on behalf of @max-sixty

🤖 Generated with Claude Code

max-sixty and others added 4 commits February 5, 2026 15:06
When switching from a subdirectory (e.g., apps/gateway/), land in the
same subdirectory of the target worktree if it exists, falling back to
the worktree root otherwise.

Closes #937

Co-Authored-By: Claude <[email protected]>
- Accept cwd parameter instead of reading std::env::current_dir() inside
  the function, eliminating global state dependency and making unit tests
  safe for parallel execution
- Fix Windows CI: use Path::join for cross-platform separator handling
  in test assertions

Co-Authored-By: Claude <[email protected]>
Use Path::new("apps").join("gateway") instead of string literal
"apps/gateway" in test_switch_preserves_subdir and
test_switch_falls_back_to_root_when_subdir_missing assertions.

Co-Authored-By: Claude <[email protected]>
Push process state reading to the edge — command handlers read cwd and
pass it down, rather than the output layer reading global state.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 114b5eb into main Feb 6, 2026
20 checks passed
@max-sixty
max-sixty deleted the cd-mono branch February 6, 2026 02:15
max-sixty pushed a commit that referenced this pull request Aug 5, 2026
## Problem

Since #939 / #3344, `wt switch` and `wt remove` preserve the user's
subdirectory position — from `monorepo.feature/subproject/` you land in
`monorepo/subproject/`. #3723 reports that this is lost one layer down:
with `[aliases] finish = "wt remove -y"`, `wt finish` drops you at the
primary worktree root.

The resolution reads the user's position from the wt process's own cwd
(`resolve_subdir_in_target`, called with `std::env::current_dir()`).
That answers "where is the user standing?" only for a top-level
invocation. Alias and hook bodies run with the worktree root as their
working directory, so the nested `wt` strips the source root off a cwd
that *is* the source root, gets an empty relative path, and falls back
to the destination root.

## Solution

The CD directive file already travels to exactly the children that are
allowed to move the user's shell. The shell's directory now travels with
it: `apply_cd_directive_env` sets `WORKTRUNK_SHELL_CWD` wherever the CD
file is re-added (`Cmd::stream` and the concurrent runner), and
`scrub_directive_env_vars` strips it alongside the other directive vars,
so an untrusted child neither keeps nor receives it.

`shell_exec::shell_cwd()` reads it back, preferring the inherited value
over the process cwd — which is what makes nesting compose, since each
layer forwards the shell's directory rather than its own. Three sites
ask that question and now go through it: `wt switch`, `wt remove`
(`prepare_remove_directory_change`), and `wt step relocate`, whose
existing comment already asks to behave identically to the other two.

Nothing about the working directory of alias or hook *bodies* changes —
`{{ cwd }}` is still the worktree root, per the documented contract. The
only change is what a nested `wt` believes about the user's position.

## Testing

Two integration tests in `tests/integration_tests/step_alias.rs`, both
failing before the change with the exact symptom reported:

```
CD file should preserve the subdirectory (…/repo/apps/gateway), got: "…/repo\n"
```

- `test_alias_wrapping_remove_preserves_subdir` — the reported case
(`[aliases] finish = "wt remove -y"` run from `feature/apps/gateway`).
- `test_alias_wrapping_switch_preserves_subdir` — the same for `wt
switch` inside an alias.

The existing subdirectory-preservation tests in `directives.rs`
(including the fall-back-to-root cases) still pass, as does the full
integration suite — apart from
`test_copy_ignored_preserves_file_executable_permissions`, which fails
identically on `main` in this sandbox (umask `0002`, expects `0644` gets
`0664`) and is unrelated.

The open question from the first revision is answered: the new remove
test leaves two processes with a cwd inside the worktree being removed
(the alias parent in the subdirectory, the nested `wt` at the root)
where the existing test has one, and `test (windows)` passes on it.

Review follow-ups are in 31d8b3d (pure `shell_cwd_from` plus its unit
test, `SHELL_CWD_ENV_VAR` added to the scrub-coverage test, corrected
`startup_cwd()` comment) and 2578348 (the fixtures in that unit test
derive from `temp_dir()` instead of a `cfg!(windows)` literal pair,
whose untaken arm was the last `codecov/patch` miss). One
`code-coverage` run failed on
`progressive_handler::tests::on_update_pokes_run_preview_only_when_the_visible_pane_changes`
— unrelated to this change, passing in `test (linux)` on the same commit
and in the coverage run on the parent commit — and passed on re-run.

Closes #3723

---------

Co-authored-by: worktrunk-bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Worktree friction: add flag to automatically cd into previous subdirectory

1 participant