Git Worktrees for Coding Agents Break on .env, node_modules and Port 3000. Fix All Three
Описание и применение
В работе с git‑worktree для кода‑агентов отсутствуют .env, node_modules и уникальный порт, из‑за чего тесты запускаются в неверной среде. Добавление хука post‑checkout копирует .env, устанавливает зависимости через pnpm и гарантирует изоляцию портов, устраняя проблемы.
Задача
Coding agents using git worktrees fail because each worktree lacks its own .env, node_modules, and a dedicated port, causing tests to run against incorrect environments.
Результат
After applying the hook, each agent runs in an isolated environment with its own .env, node_modules, and port, eliminating test failures caused by shared state.
Рабочий процесс
- 1. Add a post‑checkout hook that runs after git worktree add.
- 2. In the hook, copy .env from the main worktree to the new worktree.
- 3. Install dependencies per worktree using pnpm or npm ci, avoiding shared node_modules.
- 4. Ensure each agent uses a unique port by configuring Vite or Playwright accordingly.
Доказательства
Git worktree add creates a folder and checks out a commit into it. The new worktree shares the repository's history and config with your main checkout. It gets the tracked files of that commit and nothing else.
Symlinking .env into each worktree looks tidy and causes trouble later. Every worktree then reads and writes one file.
pnpm keeps each package once in a content-addressable store and hard-links files from there, "consuming no additional disk space".
- Дата записи: 2026-10-14T00:40:59.458Z