Skip to content

Fix dev server port conflicts across worktrees#295

Closed
Eliezir wants to merge 4 commits intomainfrom
Eliezir/dev-port-conflicts
Closed

Fix dev server port conflicts across worktrees#295
Eliezir wants to merge 4 commits intomainfrom
Eliezir/dev-port-conflicts

Conversation

@Eliezir
Copy link
Copy Markdown
Collaborator

@Eliezir Eliezir commented Apr 18, 2026

Closes #290.

Summary

Fixes dev server port conflicts when running multiple worktrees in parallel (Conductor or plain git worktree). Before: only the first worktree's API binds :3001; other studios silently proxy to the stale first instance, so endpoints that only exist on the current branch return 404 in what looks like a UI bug. After: each worktree picks its own free API port, and its studio proxy follows.

How

  • scripts/dev.mjs — root dev orchestrator. Finds a free port (sequential scan from 3001), then spawns the existing pnpm --parallel dev with PORT and API_PROXY_TARGET threaded in as env vars.
  • scripts/find-free-port.mjs — pure-Node port probe. Logic adapted from Zig's findFreePort in the zig/electron branch (apps/electron/src/main/api/port.ts).
  • apps/studio/vite.config.ts — proxy target now reads API_PROXY_TARGET with http://localhost:3001 fallback.
  • package.json — root dev script now calls node scripts/dev.mjs. dev:api / dev:studio unchanged.

Approach

We went with a monorepo-level orchestrator because it's self-contained, tool-agnostic (works with plain git worktree, Conductor, multiple terminals), needs zero per-worktree config, and adds no dependencies. A more robust alternative would be a dedicated tool like Portless, which handles automatic subdomain routing with zero env wiring — at the cost of another dev dependency and per-developer setup.

Scope

Web dev only. The electron launch path already picks its own port via its own supervisor logic and is untouched. Production builds are also untouched: the api's PORT fallback stays 3001, and vite build ignores server.proxy.

Test plan

  • Run `pnpm dev` in one worktree → `[dev] API port: 3001`, studio on 5173
  • Run `pnpm dev` in a second worktree concurrently → `[dev] API port: 3002`, studio on 5174, `/api/*` requests from the second studio hit the second worktree's API
  • Temporary `console.log` on an api route in worktree 2, triggered from worktree 2's studio, appears in worktree 2's terminal (not worktree 1's)
  • Solo `pnpm dev:studio` still proxies to `:3001` (backwards compatible)
  • `pnpm typecheck` passes

Eliezir added 3 commits April 18, 2026 15:20
Running multiple worktrees in parallel (Conductor or plain git worktree)
caused studios on 5174/5175 to silently proxy to the first worktree's
API on :3001, serving stale code from the wrong branch.

A root orchestrator (scripts/dev.mjs) now picks a free port and threads
PORT + API_PROXY_TARGET into the api/studio children via env vars. The
vite proxy reads API_PROXY_TARGET with a :3001 fallback so solo dev and
prod builds are unaffected. Port-probe helper adapted from the
findFreePort in zig/electron.
@Eliezir Eliezir self-assigned this Apr 18, 2026
@Eliezir Eliezir force-pushed the Eliezir/dev-port-conflicts branch from b539b26 to c108373 Compare April 18, 2026 18:41
@Eliezir
Copy link
Copy Markdown
Collaborator Author

Eliezir commented Apr 18, 2026

Hey @elasticsounds, I also put together another change that labels the browser tab with the Conductor workspace name, so it’s easier to tell tabs apart when working with multiple branches in parallel:

image

It’s dev-only and falls back to the current behavior for anyone not using Conductor. Worth shipping, or skip it? Let me know what you think.

- Added support for setting the document title based on the VITE_WORKSPACE_NAME environment variable in main.tsx.
- Updated vite-env.d.ts to define the VITE_WORKSPACE_NAME variable for better type safety.
- Enhanced dev.mjs to detect and log the workspace name from the conductor environment or current git branch, improving clarity during development.
@Eliezir Eliezir closed this Apr 20, 2026
@Eliezir Eliezir deleted the Eliezir/dev-port-conflicts branch April 20, 2026 21:24
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.

Dev server port conflicts across multiple worktrees (Conductor) — studios proxy to the wrong API

1 participant