Skip to content

fix: commands run inside an unlinked Git worktree act on the main checkout's Project #516

Open

Description

@clvsh

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 prepare hook is running, such as composer install. Discovery doesn't link the worktree until prepare succeeds (DESIGN.md, Discovered worktrees, step 2).
  • prepare failed. The worktree is skipped until the daemon restarts.
  • A discovery change failed, for example because the worktree's pv.yml can't be read. It is retried, but the worktree stays unlinked meanwhile.
  • The user ran pv unlink in 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:

  1. Find the current directory's checkout root: the nearest ancestor that has a .git entry, whether a file (linked worktree) or a directory (main checkout).
  2. 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 hooks job with scope worktree:<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

  1. Shim fallback. Inside an unlinked worktree, should the php shim use (a) the global default, like any unlinked directory, or (b) the PHP selector from the worktree's own pv.yml?
  2. Nested repositories that aren't worktrees, such as a package cloned under packages/foo with 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.
  3. Submodules. Their .git is 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.rs using the isolated-HOME pattern from run_unapproved_hook_pv, with insta snapshots of each command's output.
  • Table rows:
    • unlink, project:env, non-interactive open, and php:use from inside a nested, unlinked worktree. php:use resolves 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_projects in crates/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

  1. added
    bug
    control-plane
    on Nov 19, 2026
  2. pullfrog commented on Nov 19, 2026

    @pullfrog
    Contributor

    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_path checks only path ancestry.

    Implementation plan

    1. Set the contract in 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 existing main_checkout lookup already stops at the nearest .git entry.
    2. Bound the shared resolver, rather than patching each command: find the nearest ancestor with a .git entry using fallible state::fs helpers, 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.
    3. Handle the unmatched case consistently in project.rs, php.rs, and error.rs: give unresolved worktrees a typed diagnostic explaining they are not linked yet, with guidance to inspect pv jobs or explicitly run pv link. Do not infer “preparing” from missing state alone. Preserve explicit selectors and the documented interactive open picker.
    4. Verify behavior, not just errors: add isolated-HOME integration snapshots in it/cli.rs, asserting the source remains linked and its pv.yml unchanged. 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)
    • Inspect duplicate candidates and identify useful related context
    • Trace current-project resolution, design rules, and regression-test seams
    • Post one concise, code-grounded issue response without implementation

    Pullfrog  | Implement a fix ➔ | View workflow run | via Pullfrog | Using gpt-6.1-sol | 𝕏

  3. added a commit that references this issue on Nov 20, 2026
    8887f74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcontrol-planepriority-high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions