From 57d68b16f10d2ac8a64225a0dfe96e3184eaf144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20L=C3=BCder?= Date: Thu, 23 Apr 2026 07:57:36 -0500 Subject: [PATCH] fix(worktree-cli): forward dev flags to vite and reuse existing branches Two small fixes that together make `precisa-worktree setup && precisa-worktree dev --detach` actually work end-to-end for OSS repos: 1. `dev` was invoking the service script as `pnpm --filter dev -- --port `. Under pnpm 9 + a plain `"dev": "vite"` script, the `--` separator is preserved in the spawned argv, so vite saw `vite -- --port 4333` and silently fell back to its default port (4321). Removing the `--` forwards the flags directly (`vite --port 4333`). 2. `setup` always ran `git worktree add -b `, which fatal-errors if the branch already exists locally or on origin (common when bringing up a worktree for a branch that was created in the main worktree and pushed to origin). Detect that case via `git show-ref` and fall back to `git worktree add ` to check out the existing branch. --- packages/worktree-cli/src/commands/dev.ts | 5 +++- packages/worktree-cli/src/commands/setup.ts | 31 +++++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/worktree-cli/src/commands/dev.ts b/packages/worktree-cli/src/commands/dev.ts index 0819767..d996f37 100644 --- a/packages/worktree-cli/src/commands/dev.ts +++ b/packages/worktree-cli/src/commands/dev.ts @@ -113,7 +113,10 @@ function spawnService( }; const rawArgs = devArgs(svc); - const args = ['--filter', svc.pnpmFilter, 'dev', '--', ...rawArgs.map(interpolate)]; + // No `--` separator: with pnpm 9 + `"dev": "vite"`, inserting `--` + // causes vite to receive it as a literal argv entry (`vite -- --port NNN`) + // and silently ignore the port flag, falling back to its default. + const args = ['--filter', svc.pnpmFilter, 'dev', ...rawArgs.map(interpolate)]; const env = { ...process.env }; for (const [k, v] of Object.entries(svc.env ?? {})) { diff --git a/packages/worktree-cli/src/commands/setup.ts b/packages/worktree-cli/src/commands/setup.ts index 7a1c666..fb04af9 100644 --- a/packages/worktree-cli/src/commands/setup.ts +++ b/packages/worktree-cli/src/commands/setup.ts @@ -1,4 +1,4 @@ -import { execFileSync } from 'node:child_process'; +import { execFileSync, spawnSync } from 'node:child_process'; import { existsSync, mkdirSync, writeFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; @@ -25,11 +25,17 @@ export async function setup({ branch, cfg, repoRoot }: SetupOptions): Promise Creating worktree for '${branch}' at ${wtPath}...`)); - // Fetch origin/main from the main worktree; the new worktree branches from it. + // Fetch origin/main from the main worktree; a fresh worktree branches from it. execFileSync('git', ['-C', repoRoot, 'fetch', 'origin', 'main'], { stdio: 'inherit' }); - execFileSync('git', ['-C', repoRoot, 'worktree', 'add', '-b', branch, wtPath, 'origin/main'], { - stdio: 'inherit', - }); + + // If the branch already exists (local or remote-tracking), check it out + // into the new worktree instead of creating a fresh branch — otherwise + // `git worktree add -b` errors out on the duplicate. + const reuseExisting = branchExists(repoRoot, branch); + const addArgs = reuseExisting + ? ['-C', repoRoot, 'worktree', 'add', wtPath, branch] + : ['-C', repoRoot, 'worktree', 'add', '-b', branch, wtPath, 'origin/main']; + execFileSync('git', addArgs, { stdio: 'inherit' }); const entry = await allocate(cfg, branch); @@ -75,3 +81,18 @@ export async function setup({ branch, cfg, repoRoot }: SetupOptions): Promise