Repository navigation
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
apps/gateway/), preserve the position by cd-ing into the equivalent subdirectory of the target worktreewt switchandwt selectresolve_subdir_in_targetacceptscwdas a parameter (no global state reads in the output layer)Closes #937
Test plan
test_switch_preserves_subdir— switches fromapps/gateway/, verifies directive cd's to subdir in targettest_switch_falls_back_to_root_when_subdir_missing— subdir only in source, verifies fallback to roottest_switch_create_preserves_subdir—--createfrom a committed subdir, verifies preservationresolve_subdir_in_target(no source root, subdir exists, subdir missing, at root)🤖 Generated with Claude Code