Repository navigation
Description
Problem
Commands that resolve "the current Project" pick the nearest linked ancestor by path prefix (nearest_project_for_path). The lookup does not stop at a Git checkout boundary.
When a Git worktree lives inside a linked Project's directory, any command run in that worktree before PV links it resolves to the main checkout's Project. Claude Code puts its worktrees at .claude/worktrees/<name>, inside the repository, and some developers run git worktree add ./worktrees/<name>.
Reproduction
This uses an isolated HOME and needs no daemon, so it is safe to run. Observed on b2e18c72d.
cargo build --bin pv
pv_bin="$PWD/target/debug/pv"
tmp=$(mktemp -d); home="$tmp/home"; mkdir -p "$home"; repo="$tmp/acme"
mkdir -p "$repo/public" && cd "$repo" && git init -q
echo '<?php echo 1;' > public/index.php && git add . && git -c user.name=t -c user.email=t@t commit -qm init
HOME="$home" "$pv_bin" link --skip-hooks </dev/null
# Linked https://acme.test -> /private/var/.../acme
git worktree add -q "$repo/.claude/worktrees/happy-otter" -b feat
cd "$repo/.claude/worktrees/happy-otter"
HOME="$home" "$pv_bin" unlink </dev/null
# Unlinked acme.test -> /private/var/.../acme <- the main checkout's Project
HOME="$home" "$pv_bin" list --json
# {"projects":[]}Affected commands
Every caller of nearest_project_for_path:
| Command | Code | Effect when it resolves to the main Project |
|---|---|---|
pv unlink (no selector) |
unlink → resolve_project |
Unlinks the main Project. With the daemon running, unlinking a source also unlinks all of its discovered worktrees (DESIGN.md, Project Unlinking). |
pv project:env (no selector) |
env → resolve_project |
Prints the main Project's generated env, including its database name and the track superuser password. A developer or coding agent can copy those values into the worktree's .env, pointing the worktree at the main database. |
pv open (no hostname) |
resolve_open_project |
Opens the main Project's URL. |
pv php:use <track> |
use_track → resolve_current_project |
Writes php: into the main checkout's pv.yml and records the main Project's desired PHP track. This modifies a file outside the worktree. Read from code, not executed. |
php / composer shims |
resolve_php_runtime_for_shim |
Run with the main Project's PHP track and extension overlays, even when the branch's pv.yml asks for a different one. |
When the window is open
The window is much longer than the second or so discovery normally takes:
- The daemon is not running.
- An approved
preparehook is running, such ascomposer install. Discovery doesn't link the worktree untilpreparesucceeds (DESIGN.md, Discovered worktrees, step 2). preparefailed. The worktree is skipped until the daemon restarts.- A discovery change failed, for example because the worktree's
pv.ymlcan't be read. It is retried, but the worktree stays unlinked meanwhile. - The user ran
pv unlinkin the worktree. It stays opted out until Git removes it.
Worktrees outside the main checkout are not affected; for example, Codex uses $CODEX_HOME/worktrees/.... They have no linked ancestor, so commands correctly fail with "not resolved".
Proposed direction
Stop at the checkout boundary. A Project matches the current directory only if both are in the same Git checkout. One way:
- Find the current directory's checkout root: the nearest ancestor that has a
.gitentry, whether a file (linked worktree) or a directory (main checkout). - Reject candidate Projects whose path is above that root.
Directories with no .git ancestor keep today's behavior. Monorepo members are unaffected: repo/apps/api/src still resolves to repo/apps/api, because both are in the same checkout.
Say why nothing matched. When the current directory is inside a worktree of a linked source and has no Project yet, name the state and the next step. The pieces exist:
- Worktree ID:
.git/worktrees/<entry>/pv-id. - Preparing: a queued or running
hooksjob with scopeworktree:<source-project-id>:<worktree-id>. - Opted out: a row in
worktree_opt_outs. - Daemon not running: socket health.
A generic "This Git worktree isn't linked to PV yet" is an acceptable first step.
Decisions needed
- Shim fallback. Inside an unlinked worktree, should the
phpshim use (a) the global default, like any unlinked directory, or (b) the PHP selector from the worktree's ownpv.yml? - Nested repositories that aren't worktrees, such as a package cloned under
packages/foowith its own.git. The rule above would stop resolution there too. That is probably right, since it is a different checkout, but it changes behavior. - Submodules. Their
.gitis a file pointing into.git/modules. Same rule?
DESIGN.md's current-directory resolution paragraph (Opening Projects) needs the new rule.
Tests
- Integration test in
it/cli.rsusing the isolated-HOMEpattern fromrun_unapproved_hook_pv, with insta snapshots of each command's output. - Table rows:
unlink,project:env, non-interactiveopen, andphp:usefrom inside a nested, unlinked worktree.php:useresolves the Project before it downloads anything, so the test can stay offline.- A monorepo member resolved from a nested subdirectory, which must still work.
- A plain non-Git directory, which must behave as before.
- State test: if the resolver's signature changes, extend
nearest_project_resolution_prefers_nested_projectsincrates/state/tests/state_foundation.rs.
Acceptance criteria
- DESIGN.md states the Git-boundary rule and the shim fallback.
- From inside an unlinked worktree nested in a linked Project, none of the commands above act on the enclosing Project.
- The error names the worktree's state and what to do next.
- Monorepo members and non-Git directories resolve as before, covered by tests.
Activity
pullfrog commentedon Nov 19, 2026
No confirmed duplicate among the inspected matches. #364 covers worktree discovery and cleanup, not resolution before linking. #519 explicitly depends on this fix and can own the richer readiness/status reporting.
Code inspection confirms the shared cause:
nearest_project_for_pathchecks only path ancestry.Implementation plan
DESIGN.md. Its shim rules already support option (a): an unlinked directory uses global PHP without Project extensions. Confirm whether nested standalone repositories and submodules should also be boundaries before implementation. The daemon’s existingmain_checkoutlookup already stops at the nearest.gitentry..gitentry using falliblestate::fshelpers, then exclude Projects above that checkout root. Keep nearest-member selection within the checkout and existing behavior without a Git ancestor. Compare checkout paths, not Git common directories, which worktrees share.project.rs,php.rs, anderror.rs: give unresolved worktrees a typed diagnostic explaining they are not linked yet, with guidance to inspectpv jobsor explicitly runpv link. Do not infer “preparing” from missing state alone. Preserve explicit selectors and the documented interactiveopenpicker.HOMEintegration snapshots init/cli.rs, asserting the source remains linked and itspv.ymlunchanged. Extend state-resolution coverage for linked/unlinked worktrees, monorepo members, and non-Git directories. Extend the existing PHP/Composer integration fixtures to assert global-track execution without the source’s extension overlay or network requests.No code changed; tests were not run because this was investigation only.
Task list (3/3 completed)
gpt-6.1-sol| 𝕏fix: stop current-Project resolution at the Git checkout boundary