Skip to content

test(testing): default wt_command() CWD to an isolated tempdir - #2649

Merged
max-sixty merged 1 commit into
mainfrom
cwd-audit
May 9, 2026
Merged

max-sixty merged 1 commit into
mainfrom
cwd-audit

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

Bare wt_command() (free function in src/testing/mod.rs) previously didn't set current_dir, so spawned wt test subprocesses inherited the test process's CWD — which is the worktrunk repo root, with its own .config/wt.toml and 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 in tests/integration_tests/:

  • 297 already set current_dir(...) later in their chain — Command::current_dir overwrites, so the new default is a no-op for them.
  • 33 SUSPECT sites don't set 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.toml again.

Setting the default once removes the latent surprise; patching 33 sites would not.

What's in here

  • src/testing/mod.rs: add isolated_test_cwd() helper backed by LazyLock<TempDir>; call cmd.current_dir(...) from wt_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 --tests clean
  • All 64 SUSPECT-class tests pass with the new default (help, completion, custom subcommand routing, opencode plugin install/uninstall, config_init, config_show_theme)
  • Full integration suite has the same pre-existing flakes as main (test_quickstart_merge, test_docs_merge_pre_merge_hook, test_readme_example_complex, ci_status::* — verified by stashing and running on main)
  • All 1737 unit tests pass
  • pre-commit run --all-files clean

🤖 Generated with Claude Code

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]>
@max-sixty
max-sixty merged commit a5a8a49 into main May 9, 2026
33 checks passed
@max-sixty
max-sixty deleted the cwd-audit branch May 9, 2026 01:05
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]>
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.

2 participants