Skip to content

Git - remember the last selected worktree location - #348040

Merged
Ladislau Szomoru (lszomoru) merged 1 commit into
mainfrom
lszomoru/agents/fix-worktree-location-persistence-bug
Nov 13, 2026
Merged

Ladislau Szomoru (lszomoru) merged 1 commit into
mainfrom
lszomoru/agents/fix-worktree-location-persistence-bug

Conversation

@lszomoru

Copy link
Copy Markdown
Member

Fixes #284185

Repro steps

  1. Open a Git repository (for example <parent>/app).
  2. Run Git: Create Worktree... (also available from the Repositories view), pick Create new branch... and enter feature-one.
  3. When asked for the worktree path, choose a folder other than the suggested <parent>/app.worktrees/feature-one, for example <parent>/custom-worktrees/feature-one (type it or use the folder button).
  4. Run Git: Create Worktree... again with branch feature-two.
  5. 🐛 The suggested path is <parent>/app.worktrees/feature-two again instead of <parent>/custom-worktrees/feature-two.

Root cause

Before #278107, the git.createWorktree command saved the parent folder of the worktree the user created under the worktreeRoot:<repository root> global state key whenever it differed from the stored value. The refactor moved this into Repository.createWorktree() and changed the condition to defaultWorktreeRoot && !pathEquals(newWorktreeRoot, defaultWorktreeRoot). While nothing is stored yet, which is the case for every repository after the refactor, defaultWorktreeRoot is undefined and nothing is ever written. As a result, the first custom location is never remembered and the command keeps suggesting <repo>.worktrees/....

Because the write now lived in Repository.createWorktree(), it also ran for extension API callers (Repository.createWorktree in git.d.ts). Once a value was stored, an API caller that passes an explicit path could overwrite the user's choice. For example, Copilot recreates an unarchived session worktree at its original path.

Fix

  • CommandCenter._createWorktree() (the interactive command) now saves path.dirname() of the created worktree when it differs from the stored value or nothing is stored. This restores the behavior from before Git - refactor create/delete worktree and expose extension API #278107: the folder the user last picked is suggested next time. Picking the default location stores the default folder, which is harmless and makes the setting reflect what the user last used.
  • Repository.createWorktree() still reads the remembered folder to build the default path for branch-only callers, but no longer writes it. Extension API callers, including ones that pass an explicit path (such as the Agents window or Copilot agents-*/copilot-* worktrees), no longer change the user's remembered location. For branch-only callers the computed parent folder is already the remembered or default folder, so dropping the write there loses nothing.

No new git processes; the change only touches global state handling.

Validation

Command Result
pnpm run gulp compile-extension:git ✅ passed, 0 errors
node_modules/.bin/tsc -p extensions/git/tsconfig.json --noEmit ✅ passed (re-run after rebasing on main)
pnpm run eslint extensions/git/src/commands.ts extensions/git/src/repository.ts ✅ passed, no findings
./scripts/test-integration.sh --suite git ✅ 90 passing, 2 pending (includes the public API worktree tests that pass an explicit path)
node test/scenario/out/runScenario.js --dev <scenario> with the fix reverted (before) ❌ fails as expected at the second worktree: suggests <parent>/app.worktrees/feature-two instead of <parent>/custom-worktrees/feature-two
node test/scenario/out/runScenario.js --dev <scenario> with the fix (after) ✅ passed: suggests <parent>/custom-worktrees/feature-two

The UI scenario (using the validate-ui-scenario harness) drives a Code OSS dev build of this branch with a fresh profile against a scratch repository in a temp folder:

  1. Run Git: Create Worktree..., pick Create new branch... (feature-one), confirm that <parent>/app.worktrees/feature-one is suggested, replace it with <parent>/custom-worktrees/feature-one, and confirm that the worktree is created on disk.
  2. Run Git: Create Worktree... again (feature-two) and assert that the path input box suggests <parent>/custom-worktrees/feature-two.

The "before" run used the same build with only this change reverted. The native folder picker can't be automated, so the scenario types the custom path into the input box. The command accepts a typed path the same way.

Before/after captioned recordings and per-step screenshots exist for both runs. GitHub doesn't accept video uploads through the CLI, so they're available on request. Before: the second Create Worktree suggests app.worktrees/feature-two. After: it suggests custom-worktrees/feature-two.

No unit test was added: no pure function changed, and the behavior lives in an interactive quick input flow, which the UI scenario above covers.

The Create Worktree command again stores the parent folder of the worktree the user created, so it is suggested next time. Repository.createWorktree (also used by the extension API) no longer writes the remembered location, so API callers that pass an explicit path do not overwrite the user's choice.

Fixes #284185

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 23:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change restoring the remembered worktree location behavior.

No open issues 🎉

What changed in this PR

Restores Git’s last-selected worktree location behavior, addressing #284185 after the worktree API refactor.

Changes:

  • Saves the parent folder after successful interactive worktree creation, including when no location was previously stored.
  • Prevents extension API callers from overwriting the user’s remembered location while preserving it as the default for branch-only calls.
File Description
extensions/​git/​src/​repository.ts Removes remembered-location writes from shared worktree creation.
extensions/​git/​src/​commands.ts Persists the last-used location in the interactive command.

Review effort: Balanced

@lszomoru
Ladislau Szomoru (lszomoru) merged commit 5bd6b95 into main Nov 13, 2026
94 of 97 checks passed
@lszomoru
Ladislau Szomoru (lszomoru) deleted the lszomoru/agents/fix-worktree-location-persistence-bug branch November 13, 2026 09:10
@vs-code-engineering vs-code-engineering Bot added this to the 1.147.0 milestone Nov 13, 2026
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.

Worktrees not preserving last selected location

3 participants