Repository navigation
Conversation
Bare `wt_command()` previously inherited the test process's CWD (the worktrunk repo root) into spawned `wt` subprocesses, which silently coupled tests to worktrunk's own `.config/wt.toml` and git history. PR #2648 patched one such case (`test_config_init_already_exists`) per-test; the audit found 33 SUSPECT bare callsites (out of 330 total) with the same shape — none deliberately relying on inherited CWD. Set `current_dir` to a process-scoped empty `LazyLock<TempDir>` so the inherited CWD is never the implicit answer. The 297 sites that already call `cmd.current_dir(...)` keep their explicit override (it overwrites the new default). The per-test patch from #2648 is now subsumed by the structural fix, so it's removed. Co-authored-by: Claude <[email protected]>
worktrunk-bot
approved these changes
May 9, 2026
4 tasks done
max-sixty
added a commit
that referenced
this pull request
May 9, 2026
…2652) ## Summary `Repository::current_worktree()` resolved against the process-global `base_path()` (CWD or `-C`), not against the Repository's own `discovery_path`. The two coincide for `Repository::current()` (the common case), but `Repository::at(p)` callers — output handlers, picker, run_pipeline, tests, recovery — silently got a WorkingTree at the *process CWD* instead of at `p`. PR #2625 patched one symptom inline (`infer_default_branch_locally` was calling `current_worktree().is_linked()` and reaching for the wrong path). This change fixes the helper itself, which subsumes that workaround and the sibling sites identified in the audit (Pass B from the structural audit done in PR #2649's session): `project_config_path`, `project_config`, `require_current_branch`, `resolve_worktree_name("@")`, `resolve_worktree("@")`. ## Why structural over per-site Five sibling sites use `self.current_worktree()` with the same intent: "operate on the worktree this Repository represents." Patching each individually would leave the helper as a footgun — the next sibling added would re-introduce the bug. Fixing `current_worktree()` itself matches what its name has always promised. Audited production callers; none rely on the old "use process CWD even though `self` was constructed elsewhere" semantics. ## What's in this diff - `src/git/repository/mod.rs`: `current_worktree()` → `worktree_at(self.discovery_path.clone())`. Docstring explains the dual semantics. - `src/git/repository/config.rs`: revert PR #2625's per-method workaround (now redundant). - `src/git/repository/working_tree.rs`: update `WorkingTree::path` docstring that referenced `base_path()`. - `src/git/repository/tests.rs`: regression test pins both directions — matches `discovery_path()`, *not* process CWD. - `src/commands/worktree/switch.rs`: gate the source-worktree capture on `!is_recovered`. Without this, `current_worktree()` in recovery mode would resolve to the *recovered ancestor* (typically the main worktree) and expand `{{ base }}` / `{{ base_worktree_path }}` to main's identity instead of empty strings. Mirrors the existing `!is_recovered` gate on pre-switch hooks at line 1264. Caught by code review. - `tests/integration_tests/repository.rs`: remove three stale "use worktree_at, not current_worktree() which uses base_path()" comments. ## Test plan - [x] All 1740 unit tests pass - [x] Full integration suite (1632 tests) passes - [x] PR #2625's two regression tests still pass (the structural fix preserves the original bug-fix's behavior) - [x] `pre-commit run --all-files` clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <[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
Bare
wt_command()(free function insrc/testing/mod.rs) previously didn't setcurrent_dir, so spawnedwttest subprocesses inherited the test process's CWD — which is the worktrunk repo root, with its own.config/wt.tomland git history. PR #2648 patched one such case (test_config_init_already_exists) per-test after the nix sandbox surfaced it.This change sets a sane default at the constructor: a process-scoped empty
LazyLock<TempDir>(outside any git repo, no project config). The inherited CWD is never the implicit answer.Why structural over per-test
I audited the 330 bare
wt_command()callsites intests/integration_tests/:current_dir(...)later in their chain —Command::current_diroverwrites, so the new default is a no-op for them.current_dir. None deliberately rely on inherited CWD — they're all CWD-independent in practice today (wt --help,wt --version,wt config plugins opencode install,wt config shell init <shell>, clap routing of unknown subcommands, etc.) but precariously so. A future change to any of those code paths that starts reading project config would silently couple them to the worktrunk repo's.config/wt.tomlagain.Setting the default once removes the latent surprise; patching 33 sites would not.
What's in here
src/testing/mod.rs: addisolated_test_cwd()helper backed byLazyLock<TempDir>; callcmd.current_dir(...)fromwt_command().tests/CLAUDE.md: document the new default contract.tests/integration_tests/config_init.rs: remove the now-redundant per-test override added in ci(nightly): re-enable nix-flake job & fix sandbox failures #2648.Test plan
cargo check --testscleanmain(test_quickstart_merge, test_docs_merge_pre_merge_hook, test_readme_example_complex, ci_status::* — verified by stashing and running on main)pre-commit run --all-filesclean🤖 Generated with Claude Code