worktree-compose 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -15
- package/SKILL.md +5 -5
- package/dist/commands/list.js +1 -1
- package/dist/compose/isolation.d.ts +16 -5
- package/dist/compose/isolation.js +41 -12
- package/dist/compose/override.d.ts +152 -0
- package/dist/compose/override.js +542 -0
- package/dist/compose/parse.d.ts +7 -0
- package/dist/compose/parse.js +121 -29
- package/dist/compose/types.d.ts +23 -0
- package/dist/context.d.ts +15 -0
- package/dist/context.js +26 -5
- package/dist/core/clean.js +4 -0
- package/dist/core/compose.d.ts +66 -4
- package/dist/core/compose.js +157 -7
- package/dist/core/doctor.d.ts +4 -3
- package/dist/core/doctor.js +78 -13
- package/dist/core/list.d.ts +5 -1
- package/dist/core/list.js +19 -12
- package/dist/core/prune.js +5 -1
- package/dist/core/start.d.ts +3 -1
- package/dist/core/start.js +21 -7
- package/dist/core/stop.js +4 -3
- package/dist/core/teardown.d.ts +3 -1
- package/dist/core/teardown.js +16 -9
- package/dist/mcp/server.js +1 -1
- package/dist/ports/allocate.d.ts +26 -3
- package/dist/ports/allocate.js +78 -14
- package/dist/ports/extract.d.ts +12 -0
- package/dist/ports/extract.js +48 -4
- package/dist/ports/types.d.ts +12 -1
- package/dist/sync/env.d.ts +19 -3
- package/dist/sync/env.js +21 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ wtc list
|
|
|
51
51
|
- [Selecting worktrees](#selecting-worktrees)
|
|
52
52
|
- [Preparing your compose file](#preparing-your-compose-file)
|
|
53
53
|
- [Monorepos & custom env files](#monorepos--custom-env-files)
|
|
54
|
-
- [How It Works](#how-it-works) — [stable identity](#stable-worktree-identity), [ports](#port-allocation), [project names](#container-isolation-and-compose_project_name), [file sync](#file-sync), [env files](#env-files)
|
|
54
|
+
- [How It Works](#how-it-works) — [stable identity](#stable-worktree-identity), [ports](#port-allocation), [project names](#container-isolation-and-compose_project_name), [runtime remapping](#runtime-remapping), [file sync](#file-sync), [env files](#env-files)
|
|
55
55
|
- [Commands](#commands) — [what each one does to your data](#what-each-command-does-to-your-data)
|
|
56
56
|
- [Configuration (Optional)](#configuration-optional)
|
|
57
57
|
- [Docker, Podman, OrbStack, Colima](#docker-podman-orbstack-colima)
|
|
@@ -70,7 +70,7 @@ Spin up multiple developers or AI agents on the same repo — each in its own [g
|
|
|
70
70
|
|
|
71
71
|
`wtc` reads your existing compose file and turns every worktree into a fully isolated, side-by-side environment — unique host ports, separate containers, networks, and volumes — with **zero configuration**.
|
|
72
72
|
|
|
73
|
-
- ⚡ **Zero config** — reads your existing `docker-compose.yml` / `compose.yaml
|
|
73
|
+
- ⚡ **Zero config** — reads your existing `docker-compose.yml` / `compose.yaml` as it is: hardcoded host ports and `container_name` are remapped at runtime, never edited
|
|
74
74
|
- 🔌 **Automatic ports** — unique, collision-free host ports per worktree
|
|
75
75
|
- 📦 **Full isolation** — separate containers, networks, and volumes for each stack
|
|
76
76
|
- 📌 **Stable identity** — a worktree keeps its index, ports, and project name; removing one never renumbers the others
|
|
@@ -108,7 +108,7 @@ wtc clean
|
|
|
108
108
|
|
|
109
109
|
## How is this different from `docker compose -p`?
|
|
110
110
|
|
|
111
|
-
`docker compose -p` only namespaces the project. You'd still hand-pick free host ports, hand-edit each worktree's `.env`, copy infra files in, remember which project belongs to which worktree, and tear everything down yourself. `wtc` builds on that same project-name isolation and automates the rest: collision-free **port allocation
|
|
111
|
+
`docker compose -p` only namespaces the project. You'd still hand-pick free host ports, hand-edit each worktree's `.env`, copy infra files in, remember which project belongs to which worktree, and tear everything down yourself. `wtc` builds on that same project-name isolation and automates the rest: collision-free **port allocation** (hardcoded host ports included), a **stable index and project name** per worktree, per-worktree **env file injection** (including `COMPOSE_PROJECT_NAME`, so plain `docker compose` in the worktree hits the right stack), infra-file **sync**, one-command **`promote`**, safe **cleanup**, and a built-in **MCP server** so AI agents can drive it all themselves.
|
|
112
112
|
|
|
113
113
|
## Quick Start
|
|
114
114
|
|
|
@@ -176,19 +176,25 @@ wtc mcp # MCP server for AI agents (stdio)
|
|
|
176
176
|
|
|
177
177
|
`wtc` looks for `compose.yaml`, `compose.yml`, `docker-compose.yaml`, or `docker-compose.yml` (in that order) at the root of the main checkout.
|
|
178
178
|
|
|
179
|
-
|
|
179
|
+
You don't have to change it. Each worktree gets its own host ports either way:
|
|
180
180
|
|
|
181
181
|
```yaml
|
|
182
|
-
# wtc
|
|
182
|
+
# A variable (upper-case name): wtc sets it per worktree and writes it to the worktree's env file
|
|
183
183
|
ports:
|
|
184
184
|
- "${BACKEND_PORT:-8000}:8000"
|
|
185
185
|
|
|
186
|
-
# wtc
|
|
186
|
+
# Hardcoded: wtc remaps it per worktree at runtime; the file stays as it is
|
|
187
187
|
ports:
|
|
188
188
|
- "8080:8080"
|
|
189
189
|
```
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
Hardcoded host ports (and a fixed `container_name`) are [remapped at runtime](#runtime-remapping) through a compose override `wtc` generates, which needs Docker Compose 2.24.4 or newer. `wtc start` says what it remapped:
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
ℹ Remapping hardcoded host ports per worktree at runtime (your compose file is unchanged): nginx 8080:80. To use one in env values, give it a variable, e.g. "${NGINX_PORT:-8080}:80".
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Use a variable when something else needs the port's per-worktree value: the variable is written to the worktree's env file and can be used in [`envOverrides`](#envoverrides) (a `DATABASE_URL`, an API URL), while a remapped port is only published. With an engine that can't remap, `wtc` warns about hardcoded ports instead, and they stay the same in every worktree:
|
|
192
198
|
|
|
193
199
|
```
|
|
194
200
|
⚠ Service "nginx" uses a raw port mapping (8080:80). To enable port isolation, change it to: "${NGINX_PORT:-8080}:80"
|
|
@@ -223,9 +229,9 @@ If `wtc` finds hardcoded ports, it warns you and suggests the fix:
|
|
|
223
229
|
|
|
224
230
|
`wtc start` and `wtc doctor` warn about compose settings that make worktree stacks collide or share data (`wtc list` only prints how many there are):
|
|
225
231
|
|
|
226
|
-
-
|
|
232
|
+
- **Hardcoded host ports and `container_name: db`** — every stack would publish the same ports and use the same container name, so the second one fails to start. `wtc` [remaps both at runtime](#runtime-remapping) and only warns when it can't; then write the port as `"${DB_PORT:-5432}:5432"`, and remove `container_name` or make it per-stack: `container_name: ${COMPOSE_PROJECT_NAME:-myapp}-db`.
|
|
233
|
+
- **Host port ranges** (`"8000-8010:8000-8010"`) — not remapped: every stack binds the same ports.
|
|
227
234
|
- **Top-level volumes or networks with a fixed `name:` or `external: true`** — every stack shares them. A `name:` that contains `${COMPOSE_PROJECT_NAME}` is per-stack and fine.
|
|
228
|
-
- **Hardcoded host ports** — see above.
|
|
229
235
|
- **Cross-worktree port collisions** — see [`portStride`](#clustered-default-ports-and-portstride).
|
|
230
236
|
|
|
231
237
|
### Included compose files
|
|
@@ -341,12 +347,46 @@ A stride of `100` gives every worktree a 100-port band, which is collision-free
|
|
|
341
347
|
|
|
342
348
|
Each worktree's stack runs as the compose project `{repo}-wt-{index}-{branch}`, lowercased with anything other than `a-z`, `0-9`, and `-` turned into `-` — for example `myapp-wt-1-feature-auth`. Compose names containers, networks, and volumes after the project, so nothing is shared. `{repo}` is the main checkout's directory name (for a bare repository, the repository's own name), so it is the same from every worktree.
|
|
343
349
|
|
|
344
|
-
`wtc` passes `-p <project>` on every compose call, exports `COMPOSE_PROJECT_NAME` and the allocated ports into compose's environment, and writes `COMPOSE_PROJECT_NAME=<project>` into the worktree's root env file. With the default `.env`, a plain `docker compose ps` / `logs` / `exec` (or a Makefile) run inside the worktree therefore reaches the same stack on the same ports.
|
|
350
|
+
`wtc` passes `-p <project>` on every compose call, exports `COMPOSE_PROJECT_NAME` and the allocated ports into compose's environment, and writes `COMPOSE_PROJECT_NAME=<project>` into the worktree's root env file (plus `COMPOSE_FILE` when it [remaps hardcoded settings](#runtime-remapping)). With the default `.env`, a plain `docker compose ps` / `logs` / `exec` (or a Makefile) run inside the worktree therefore reaches the same stack on the same ports.
|
|
345
351
|
|
|
346
|
-
Compose only reads a file named `.env` on its own. With a custom `envFile` such as `.env.development`, pass it explicitly (`docker compose --env-file .env.development ps`) or pass `-p <project>` (the `project` field of `wtc list --json`).
|
|
352
|
+
Compose only reads a file named `.env` on its own. With a custom `envFile` such as `.env.development`, pass it explicitly (`docker compose --env-file .env.development ps`) or pass `-p <project>` (the `project` field of `wtc list --json`). `-p` is enough for `ps`, `logs`, and `exec`; `up` also needs `--env-file`, so it loads the [runtime remapping](#runtime-remapping)'s `COMPOSE_FILE` (or run `wtc restart .`).
|
|
347
353
|
|
|
348
354
|
`${COMPOSE_PROJECT_NAME}` is also available in `envOverrides` and `workspaceEnvOverrides` values, e.g. `"QUEUE_PREFIX": "${COMPOSE_PROJECT_NAME}-jobs"`.
|
|
349
355
|
|
|
356
|
+
### Runtime remapping
|
|
357
|
+
|
|
358
|
+
A hardcoded host port (`"8080:80"`) or a fixed `container_name` would make every worktree stack collide. Instead of asking you to edit the compose file, `wtc` writes a compose override for each worktree stack:
|
|
359
|
+
|
|
360
|
+
```yaml
|
|
361
|
+
# .git/wtc/compose/myapp-wt-1-feature-auth.yaml (generated, rewritten on every start)
|
|
362
|
+
services:
|
|
363
|
+
web:
|
|
364
|
+
ports: !override
|
|
365
|
+
- "28081:80"
|
|
366
|
+
container_name: !reset null
|
|
367
|
+
networks:
|
|
368
|
+
default:
|
|
369
|
+
aliases:
|
|
370
|
+
- "web"
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
- `ports: !override` replaces the service's port list: every entry as written, with only the hardcoded host ports swapped for the worktree's (same IP, protocol, and long-form fields). A plain override can't do this, because compose merges port lists and would still publish `8080`.
|
|
374
|
+
- `container_name: !reset null` drops the fixed name, so compose names the containers after the project (`myapp-wt-1-feature-auth-web-1`). The old name stays a network alias on the stack's own networks, so containers that reach the service by it (`DB_HOST=mydb`) still do, and reach this stack's copy. Not on a network every stack shares (fixed `name:` or `external`), where the alias would answer for all of them, nor with `network_mode`.
|
|
375
|
+
- Every compose call on the stack (`start`, `stop`, teardown) runs with `COMPOSE_FILE` listing the compose file, the worktree's own `compose.override.yaml` if it has one, and this file. `wtc` also writes that `COMPOSE_FILE` into the worktree's root env file, so a plain `docker compose up` run from the worktree's root uses the same ports. (From a subdirectory, compose finds the compose file itself and ignores `COMPOSE_FILE` in `.env`.)
|
|
376
|
+
- The file lives in `.git/wtc/compose/`, never in your working tree. `start` rewrites it after syncing the compose file; `stop` and teardown use it as the stack was started. It is deleted when `wtc prune` or `wtc clean` forgets the worktree; a worktree that `clean` keeps also keeps its override, since its env file still lists it.
|
|
377
|
+
- `wtc list` shows a remapped port as `<service>:<default port>`, e.g. `web:8080=28081`, as `start` allocates it for that worktree. It has no variable, so nothing is written to env files for it and `envOverrides` can't refer to it; give it a variable in the compose file when an env value needs it.
|
|
378
|
+
|
|
379
|
+
`!override` and `!reset` need Docker Compose 2.24.4 or newer. `wtc` checks once per run that the engine applies them (`compose config` on a throwaway file; no containers, no daemon) and otherwise remaps nothing and warns as before. It also leaves alone:
|
|
380
|
+
|
|
381
|
+
- what the worktree's own `compose.override.yaml` (or `docker-compose.override.yml`, …) sets for a service: its `ports` (or `extends`) or `container_name` would be discarded;
|
|
382
|
+
- the ports of a service that uses `extends`, which may inherit ports `wtc` can't see;
|
|
383
|
+
- the ports of a service from an `include:`d file whose ports use variables: compose fills those in from that file's own `.env`, while an override is filled in from the main project's;
|
|
384
|
+
- host port ranges, a hardcoded port too high to move, and one whose new port the stack already publishes (they stay as they are, with a warning);
|
|
385
|
+
- a `container_name` another service joins by name (`network_mode: container:mydb`, `volumes_from`, `ipc`, `pid`): an alias can't stand in for the container itself;
|
|
386
|
+
- everything, when you set `COMPOSE_FILE` yourself: in your environment, the root env file, the main checkout's `.env`, `envOverrides`, or a worktree's own `.env` (either `COMPOSE_FILE=` or `COMPOSE_FILE:`), or set a different `COMPOSE_PATH_SEPARATOR` there. A `COMPOSE_FILE` that is exactly the list `wtc` wrote (a worktree's `.env` exported by direnv, say) doesn't count; one you added a file to does.
|
|
387
|
+
|
|
388
|
+
`wtc doctor` lists what is remapped, or why it can't be.
|
|
389
|
+
|
|
350
390
|
### File Sync
|
|
351
391
|
|
|
352
392
|
Before every start, `wtc` copies infrastructure files from the main checkout into the worktree, so every worktree runs the latest Docker setup:
|
|
@@ -362,7 +402,7 @@ Sync copies over what is at those paths and removes nothing else: files that onl
|
|
|
362
402
|
On every `start` and `restart`, for the root and each selected workspace:
|
|
363
403
|
|
|
364
404
|
1. **Copy fresh from main:** `<envFile>` (default `.env`), falling back to `.env.example`. The root gets an empty file if neither exists; a workspace with neither is skipped.
|
|
365
|
-
2. **Inject:** `COMPOSE_PROJECT_NAME` (root only), every allocated port variable, then `envOverrides` (root) or `workspaceEnvOverrides` (that workspace). **A key that already exists is rewritten in place** — comments, ordering, and all other lines are preserved. Keys that don't exist are appended in a marked block, which is replaced on every run and never accumulates.
|
|
405
|
+
2. **Inject:** `COMPOSE_PROJECT_NAME` (root only; with [runtime remapping](#runtime-remapping) also `COMPOSE_FILE`), every allocated port variable, then `envOverrides` (root) or `workspaceEnvOverrides` (that workspace). **A key that already exists is rewritten in place** — comments, ordering, and all other lines are preserved. Keys that don't exist are appended in a marked block, which is replaced on every run and never accumulates.
|
|
366
406
|
|
|
367
407
|
With `"envOverrides": { "DATABASE_URL": "postgresql://app:app@localhost:${POSTGRES_PORT}/app" }` and a main `.env` of
|
|
368
408
|
|
|
@@ -496,7 +536,7 @@ Tears down (`compose down -v --remove-orphans`, deleting volumes) and forgets **
|
|
|
496
536
|
wtc doctor [--json]
|
|
497
537
|
```
|
|
498
538
|
|
|
499
|
-
A ✔ / ⚠ / ✖ checklist with hints: git version; repository and main-checkout detection; compose file found and parsed (services, included files); config parsed (`.wtcrc.json` or `package.json#wtc`); compose engine and version (when it doesn't work, whether `WTC_COMPOSE_COMMAND`, `composeCommand`, or auto-detection chose it); daemon reachable; isolatable
|
|
539
|
+
A ✔ / ⚠ / ✖ checklist with hints: git version; repository and main-checkout detection; compose file found and parsed (services, included files); config parsed (`.wtcrc.json` or `package.json#wtc`); compose engine and version (when it doesn't work, whether `WTC_COMPOSE_COMMAND`, `composeCommand`, or auto-detection chose it); daemon reachable; isolatable ports, including hardcoded ones [remapped at runtime](#runtime-remapping) (or why they can't be); [isolation warnings](#settings-that-break-isolation); cross-worktree port collisions; missing worktrees; orphaned stacks; each worktree's stack state; and allocated ports already taken by something else for worktrees whose stack isn't running. Read-only. Exits 1 if any check fails.
|
|
500
540
|
|
|
501
541
|
### `wtc skill`
|
|
502
542
|
|
|
@@ -706,7 +746,7 @@ wtc clean # worktrees with uncommitted work are kept; --force discar
|
|
|
706
746
|
- **Node.js** >= 20
|
|
707
747
|
- **Git** 2.17 or newer, with worktree support (2.31+ recommended)
|
|
708
748
|
- **Docker** with Compose v2 (`docker compose`), or **Podman** (`podman compose` / `podman-compose`) — see [engines](#docker-podman-orbstack-colima)
|
|
709
|
-
-
|
|
749
|
+
- Docker Compose 2.24.4 or newer to [remap hardcoded host ports and `container_name`](#runtime-remapping); with older engines, write host ports as `${VAR:-default}`
|
|
710
750
|
|
|
711
751
|
## Troubleshooting
|
|
712
752
|
|
|
@@ -718,7 +758,7 @@ Start with **`wtc doctor`**: it checks the engine, the daemon, ports, isolation
|
|
|
718
758
|
|
|
719
759
|
**"No extra worktrees found"** — Create worktrees first: `git worktree add -b my-branch ../my-feature`
|
|
720
760
|
|
|
721
|
-
**Ports not changing** —
|
|
761
|
+
**Ports not changing** — `wtc doctor` lists which ports are isolated, and whether hardcoded ones are remapped at runtime or why not (it needs Docker Compose 2.24.4+). Otherwise write host ports as `${VAR:-default}`, with an upper-case variable. A plain `docker compose up` run from a subdirectory of the worktree ignores `COMPOSE_FILE` in `.env` and publishes the hardcoded ports; run it from the worktree's root, or use `wtc restart .`.
|
|
722
762
|
|
|
723
763
|
**"Bind for 0.0.0.0:25435 failed: port is already allocated"** — Something else listens on that port. `wtc doctor` reports allocated ports that are already taken; stop the other process, or raise `portStride`. If you run `docker compose` yourself with a custom `envFile`, pass `--env-file <envFile>` (Compose only reads `.env` on its own) or export the port (e.g. `POSTGRES_PORT=25435 docker compose up`).
|
|
724
764
|
|
package/SKILL.md
CHANGED
|
@@ -30,14 +30,14 @@ Prefer `.` (the worktree containing the current directory) when working inside a
|
|
|
30
30
|
|
|
31
31
|
| Command | Effect |
|
|
32
32
|
|---------|--------|
|
|
33
|
-
| `wtc start [wt...] [--env <file>] [--workspace <ws>]... [--no-build]` | Sync compose/Dockerfiles/`sync` paths from main, copy env files fresh from main, inject ports + `COMPOSE_PROJECT_NAME`, `compose up -d --build`. Stops at the first failure. |
|
|
33
|
+
| `wtc start [wt...] [--env <file>] [--workspace <ws>]... [--no-build]` | Sync compose/Dockerfiles/`sync` paths from main, copy env files fresh from main, inject ports + `COMPOSE_PROJECT_NAME`, remap hardcoded host ports and `container_name` at runtime, `compose up -d --build`. Stops at the first failure. |
|
|
34
34
|
| `wtc stop [wt...]` | `compose stop`. Containers and **all volumes are kept**. |
|
|
35
35
|
| `wtc restart [wt...] [same flags]` | `compose stop`, then start with `--remove-orphans`; changed services are recreated, volumes kept. |
|
|
36
36
|
| `wtc list` / `wtc ls` `[--json]` | Read-only. Rows: `index, branch, path, project, status, running, total, url, ports, orphan, unavailable`. Status: `up`, `partial`, `exited` (stopped), `down` (no containers), `?` (engine unreachable). `unavailable`: `"missing"` (locked, directory missing — e.g. unmounted drive), `"main"` (became the main checkout), or `null`. |
|
|
37
37
|
| `wtc promote <wt> [--force]` | Copy the worktree's changes (committed since it branched, staged, unstaged, untracked; deletions too) into the checkout you run it from, uncommitted. Skips compose files and local env files. |
|
|
38
38
|
| `wtc prune [--dry-run]` | Tear down (`down -v`, **deletes volumes**) and forget stacks of worktrees that were already deleted. |
|
|
39
39
|
| `wtc clean [--dry-run] [--force] [--keep-worktrees] [--sweep]` | `down -v` on **every** stack (**deletes all their volumes**), remove worktrees that hold no work, sweep leftovers this checkout owns. |
|
|
40
|
-
| `wtc doctor [--json]` | Diagnose git, compose file, config, engine + daemon, port isolation, `container_name` / shared volumes, collisions, orphans, busy ports. Exit 1 on any ✖. |
|
|
40
|
+
| `wtc doctor [--json]` | Diagnose git, compose file, config, engine + daemon, port isolation (and what is remapped at runtime, or why not), `container_name` / shared volumes, collisions, orphans, busy ports. Exit 1 on any ✖. |
|
|
41
41
|
| `wtc skill [--install] [--dir <path>]` | Print this skill, or install it to `~/.claude/skills/worktree-compose/SKILL.md` (or `<path>/worktree-compose/`). |
|
|
42
42
|
| `wtc mcp` | MCP server on stdio. |
|
|
43
43
|
|
|
@@ -53,7 +53,7 @@ Prefer `.` (the worktree containing the current directory) when working inside a
|
|
|
53
53
|
- A worktree's index and compose project are recorded in `.git/wtc/state.json` the first time wtc acts on it (`list`, `doctor` and dry runs only read) and never change: removing a worktree never renumbers the others, renaming its branch keeps the project, `git worktree move` keeps everything. A new worktree gets the lowest free index.
|
|
54
54
|
- A deleted worktree becomes an orphan: `wtc list` shows `(removed)` and its index stays reserved until `wtc prune` or `wtc clean`. A worktree git still lists is never an orphan: a locked one whose directory is missing shows `(locked, missing on disk)` and `prune`/`clean` leave its stack alone.
|
|
55
55
|
- If wtc cannot tell which checkout is the main one (bare repo whose default-branch worktree is mid-rebase/on another branch, or a `--separate-git-dir` repo seen from a linked worktree before wtc ran in the main checkout), `start`, `restart` and `clean` refuse with the fix in the message; tell the user rather than working around it.
|
|
56
|
-
- Ports: `20000 + default_port + index × portStride` (`portStride` default `1`).
|
|
56
|
+
- Ports: `20000 + default_port + index × portStride` (`portStride` default `1`). A host port written as `${VAR:-default}` (upper-case `VAR`) gets `VAR` in the worktree's env file. A hardcoded one (`"8080:80"`) and a fixed `container_name` are remapped at runtime by a generated compose override in `.git/wtc/compose/` (needs Docker Compose 2.24.4+; the compose file is never edited; the old container name stays a network alias inside the stack). A remapped port has no variable: `wtc list` shows it as `<service>:<default port>` (`web:8080=28081`). With an engine that can't remap, hardcoded ports and `container_name` are only warned about.
|
|
57
57
|
- Project: `{repo}-wt-{index}-{branch}`, sanitized (e.g. `myapp-wt-1-feature-auth`).
|
|
58
58
|
|
|
59
59
|
## Env files
|
|
@@ -61,7 +61,7 @@ Prefer `.` (the worktree containing the current directory) when working inside a
|
|
|
61
61
|
- Every start/restart copies `<envFile>` (default `.env`, fallback `.env.example`) **fresh from main**, then writes `COMPOSE_PROJECT_NAME` (root only), the allocated `*_PORT` variables, and `envOverrides`. Existing keys are rewritten in place; the rest go in a `# --- wtc port overrides ---` block.
|
|
62
62
|
- Edits made only in a worktree's env file are lost on the next start. Put lasting values in main's env file or in `envOverrides` / `workspaceEnvOverrides`.
|
|
63
63
|
- Only those keys are written. A value with a port inside (`DATABASE_URL=…:5434/app`) needs an override: `"DATABASE_URL": "postgresql://app:app@localhost:${POSTGRES_PORT}/app"`. `${COMPOSE_PROJECT_NAME}` interpolates too.
|
|
64
|
-
- With the default `.env`, a plain `docker compose ps|logs|exec` inside the worktree reaches the worktree's stack. With a custom `envFile` (e.g. `.env.development`), add `--env-file .env.development` or `-p <project>` (from `wtc list --json`)
|
|
64
|
+
- With the default `.env`, a plain `docker compose ps|logs|exec` inside the worktree reaches the worktree's stack. When hardcoded settings are remapped, the root env file also gets `COMPOSE_FILE`, so a plain `docker compose up` from the worktree's root uses the same ports (not from a subdirectory: prefer `wtc restart .`). With a custom `envFile` (e.g. `.env.development`), add `--env-file .env.development` or `-p <project>` (from `wtc list --json`); `-p` is enough for `ps|logs|exec`, but `up` needs `--env-file` (it carries the remap's `COMPOSE_FILE`) — or use `wtc restart .`.
|
|
65
65
|
- Monorepos: `--env .env.development --workspace server` (repeatable) also copies and injects `apps/server/.env.development`. Use `--env`, never `--env-file` (Node intercepts it).
|
|
66
66
|
|
|
67
67
|
## Promote rules
|
|
@@ -74,7 +74,7 @@ Prefer `.` (the worktree containing the current directory) when working inside a
|
|
|
74
74
|
|
|
75
75
|
## Diagnose
|
|
76
76
|
|
|
77
|
-
Run `wtc doctor` first when a stack won't start, ports look wrong, or `wtc list` shows `?` or `(removed)` rows. Typical fixes:
|
|
77
|
+
Run `wtc doctor` first when a stack won't start, ports look wrong, or `wtc list` shows `?` or `(removed)` rows. Typical fixes: drop fixed `name:`/`external` on volumes and networks, raise `portStride` on collisions, give a port a `${VAR:-default}` variable when an env value (e.g. `DATABASE_URL` via `envOverrides`) needs it; where runtime remapping is unavailable, remove `container_name` (or use `${COMPOSE_PROJECT_NAME}`) and write host ports as `${VAR:-default}`; `wtc prune` for orphans, start the Docker/Podman daemon or set `WTC_COMPOSE_COMMAND`.
|
|
78
78
|
|
|
79
79
|
## Config (optional)
|
|
80
80
|
|
package/dist/commands/list.js
CHANGED
|
@@ -53,7 +53,7 @@ export async function listCommand(options = {}) {
|
|
|
53
53
|
]);
|
|
54
54
|
for (const row of result.rows) {
|
|
55
55
|
const ports = Object.entries(row.ports)
|
|
56
|
-
.map(([
|
|
56
|
+
.map(([label, port]) => `${label}=${port}`)
|
|
57
57
|
.join(" ");
|
|
58
58
|
if (row.orphan || row.unavailable) {
|
|
59
59
|
const note = row.orphan
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import type { PortMapping } from "../ports/types.js";
|
|
2
|
+
import type { RemapTargets } from "./override.js";
|
|
2
3
|
import type { ComposeFile } from "./types.js";
|
|
3
4
|
/**
|
|
4
5
|
* Compose settings that defeat per-worktree isolation: a fixed
|
|
5
6
|
* `container_name` (the second stack cannot start), and volumes/networks with
|
|
6
|
-
* a fixed `name:` or `external` (every stack shares them).
|
|
7
|
+
* a fixed `name:` or `external` (every stack shares them). Services in
|
|
8
|
+
* `reset` have their container_name dropped at runtime, so they are fine.
|
|
7
9
|
*/
|
|
8
|
-
export declare function composeIsolationWarnings(composeFile: ComposeFile): string[];
|
|
9
|
-
/**
|
|
10
|
-
|
|
10
|
+
export declare function composeIsolationWarnings(composeFile: ComposeFile, reset?: ReadonlySet<string>): string[];
|
|
11
|
+
/**
|
|
12
|
+
* Hardcoded `host:container` port mappings that stay the same in every
|
|
13
|
+
* worktree: those not in `remapped` (remapped at runtime).
|
|
14
|
+
*/
|
|
15
|
+
export declare function rawPortWarnings(portMappings: PortMapping[], remapped?: PortMapping[]): string[];
|
|
16
|
+
/**
|
|
17
|
+
* What runtime remapping does, for `wtc start` (info, not warnings: the
|
|
18
|
+
* stacks no longer collide). Remapped ports have no variable, so env values
|
|
19
|
+
* cannot follow them; the hint says how to give one a name.
|
|
20
|
+
*/
|
|
21
|
+
export declare function remapNotes(targets: RemapTargets, portMappings: PortMapping[]): string[];
|
|
11
22
|
/**
|
|
12
23
|
* Advisory cross-worktree collision warning over the indices actually in use
|
|
13
24
|
* (live worktrees plus orphans that still reserve theirs).
|
|
14
25
|
*/
|
|
15
|
-
export declare function portCollisionWarnings(portMappings: PortMapping[], portStride: number, indices: number[]): string[];
|
|
26
|
+
export declare function portCollisionWarnings(portMappings: PortMapping[], portStride: number, indices: number[], remapped?: PortMapping[]): string[];
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { suggestEnvVars } from "../ports/extract.js";
|
|
2
2
|
import { detectCrossWorktreeCollisions, recommendPortStride, } from "../ports/allocate.js";
|
|
3
3
|
import { isProjectScopedName } from "./parse.js";
|
|
4
4
|
/**
|
|
5
5
|
* Compose settings that defeat per-worktree isolation: a fixed
|
|
6
6
|
* `container_name` (the second stack cannot start), and volumes/networks with
|
|
7
|
-
* a fixed `name:` or `external` (every stack shares them).
|
|
7
|
+
* a fixed `name:` or `external` (every stack shares them). Services in
|
|
8
|
+
* `reset` have their container_name dropped at runtime, so they are fine.
|
|
8
9
|
*/
|
|
9
|
-
export function composeIsolationWarnings(composeFile) {
|
|
10
|
+
export function composeIsolationWarnings(composeFile, reset = new Set()) {
|
|
10
11
|
const warnings = [];
|
|
11
12
|
for (const svc of composeFile.services) {
|
|
12
|
-
if (svc.containerName && !isProjectScopedName(svc.containerName)) {
|
|
13
|
+
if (svc.containerName && !isProjectScopedName(svc.containerName) && !reset.has(svc.name)) {
|
|
13
14
|
warnings.push(`Service "${svc.name}" sets container_name "${svc.containerName}" — every worktree stack would use the same name and the second one fails to start. Remove container_name.`);
|
|
14
15
|
}
|
|
15
16
|
}
|
|
@@ -21,18 +22,46 @@ export function composeIsolationWarnings(composeFile) {
|
|
|
21
22
|
}
|
|
22
23
|
return warnings;
|
|
23
24
|
}
|
|
24
|
-
/**
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Hardcoded `host:container` port mappings that stay the same in every
|
|
27
|
+
* worktree: those not in `remapped` (remapped at runtime).
|
|
28
|
+
*/
|
|
29
|
+
export function rawPortWarnings(portMappings, remapped = []) {
|
|
30
|
+
const suggestions = suggestEnvVars(portMappings);
|
|
31
|
+
const done = new Set(remapped);
|
|
26
32
|
return portMappings
|
|
27
|
-
.filter((m) => m.envVar === null)
|
|
28
|
-
.map((p) =>
|
|
29
|
-
`
|
|
33
|
+
.filter((m) => m.envVar === null && !done.has(m))
|
|
34
|
+
.map((p) => p.range
|
|
35
|
+
? `Service "${p.serviceName}" publishes a host port range (${p.raw}) — every worktree stack binds the same ports. ` +
|
|
36
|
+
`wtc cannot remap ranges; publish single ports, or leave the range out of worktree stacks.`
|
|
37
|
+
: `Service "${p.serviceName}" uses a raw port mapping (${p.raw}). ` +
|
|
38
|
+
`To enable port isolation, change it to: "\${${suggestions.get(p)}:-${p.defaultPort}}:${p.containerPort}"`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* What runtime remapping does, for `wtc start` (info, not warnings: the
|
|
42
|
+
* stacks no longer collide). Remapped ports have no variable, so env values
|
|
43
|
+
* cannot follow them; the hint says how to give one a name.
|
|
44
|
+
*/
|
|
45
|
+
export function remapNotes(targets, portMappings) {
|
|
46
|
+
const notes = [];
|
|
47
|
+
if (targets.ports.length > 0) {
|
|
48
|
+
const suggestions = suggestEnvVars(portMappings);
|
|
49
|
+
const first = targets.ports[0];
|
|
50
|
+
notes.push(`Remapping hardcoded host ports per worktree at runtime (your compose file is unchanged): ` +
|
|
51
|
+
`${targets.ports.map((m) => `${m.serviceName} ${m.raw}`).join(", ")}. ` +
|
|
52
|
+
`To use one in env values, give it a variable, e.g. "\${${suggestions.get(first)}:-${first.defaultPort}}:${first.containerPort}".`);
|
|
53
|
+
}
|
|
54
|
+
if (targets.containerNames.length > 0) {
|
|
55
|
+
notes.push(`Dropping fixed container names per worktree at runtime (your compose file is unchanged): ` +
|
|
56
|
+
`${targets.containerNames.map((c) => `${c.service} ("${c.name}")`).join(", ")}.`);
|
|
57
|
+
}
|
|
58
|
+
return notes;
|
|
30
59
|
}
|
|
31
60
|
/**
|
|
32
61
|
* Advisory cross-worktree collision warning over the indices actually in use
|
|
33
62
|
* (live worktrees plus orphans that still reserve theirs).
|
|
34
63
|
*/
|
|
35
|
-
export function portCollisionWarnings(portMappings, portStride, indices) {
|
|
64
|
+
export function portCollisionWarnings(portMappings, portStride, indices, remapped = []) {
|
|
36
65
|
const inUse = [...new Set(indices)];
|
|
37
66
|
if (inUse.length < 2)
|
|
38
67
|
return [];
|
|
@@ -40,7 +69,7 @@ export function portCollisionWarnings(portMappings, portStride, indices) {
|
|
|
40
69
|
// abort the command that triggered the warning.
|
|
41
70
|
let collisions;
|
|
42
71
|
try {
|
|
43
|
-
collisions = detectCrossWorktreeCollisions(portMappings, portStride, inUse);
|
|
72
|
+
collisions = detectCrossWorktreeCollisions(portMappings, portStride, inUse, remapped);
|
|
44
73
|
}
|
|
45
74
|
catch {
|
|
46
75
|
return [];
|
|
@@ -51,7 +80,7 @@ export function portCollisionWarnings(portMappings, portStride, indices) {
|
|
|
51
80
|
.slice(0, 3)
|
|
52
81
|
.map((c) => `${c.port} (wt${c.a.worktreeIndex}/${c.a.serviceName} ↔ wt${c.b.worktreeIndex}/${c.b.serviceName})`)
|
|
53
82
|
.join(", ");
|
|
54
|
-
const recommended = recommendPortStride(portMappings, Math.max(...inUse));
|
|
83
|
+
const recommended = recommendPortStride(portMappings, Math.max(...inUse), remapped);
|
|
55
84
|
const fix = recommended > 0
|
|
56
85
|
? `Set "portStride": ${recommended} in .wtcrc.json (or package.json#wtc) to resolve.`
|
|
57
86
|
: `No single portStride resolves this — check for services sharing the same default port.`;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { ComposeEngine } from "./engine.js";
|
|
2
|
+
import type { ComposeFile } from "./types.js";
|
|
3
|
+
import type { PortAllocation, PortMapping } from "../ports/types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Runtime remapping. Two compose settings make every worktree stack collide
|
|
6
|
+
* with the others: a hardcoded host port (`8080:80`) and a fixed
|
|
7
|
+
* `container_name`. Instead of asking for the compose file to be edited, wtc
|
|
8
|
+
* writes a compose override for each worktree stack that replaces the
|
|
9
|
+
* service's `ports` (`!override`, hardcoded host ports swapped for the
|
|
10
|
+
* worktree's allocated ones) and drops `container_name` (`!reset`, keeping
|
|
11
|
+
* the old name as a network alias), and adds it to `COMPOSE_FILE` for every
|
|
12
|
+
* compose call on that stack — and in the worktree's root env file, so plain
|
|
13
|
+
* `docker compose` there agrees. The compose file itself is never changed.
|
|
14
|
+
*
|
|
15
|
+
* A plain override cannot do this: compose merges `ports` lists, so the
|
|
16
|
+
* hardcoded port would still be published. The `!override`/`!reset` tags
|
|
17
|
+
* need Docker Compose 2.24.4+ (and are partial or missing in other
|
|
18
|
+
* engines), so wtc first checks that the engine merges them as expected,
|
|
19
|
+
* and otherwise keeps warning as before.
|
|
20
|
+
*/
|
|
21
|
+
/** Override files compose loads next to the compose file, in its order of preference. */
|
|
22
|
+
export declare const USER_OVERRIDE_FILENAMES: string[];
|
|
23
|
+
/** What wtc remaps for a stack. */
|
|
24
|
+
export interface RemapTargets {
|
|
25
|
+
/** Hardcoded host-port mappings: each gets the worktree's allocated port. */
|
|
26
|
+
ports: PortMapping[];
|
|
27
|
+
/**
|
|
28
|
+
* Fixed container names: dropped, so compose names the containers per
|
|
29
|
+
* project. The old name stays reachable inside the stack as a network
|
|
30
|
+
* alias, unless `noAlias` (the user's own override sets the networks).
|
|
31
|
+
*/
|
|
32
|
+
containerNames: {
|
|
33
|
+
service: string;
|
|
34
|
+
name: string;
|
|
35
|
+
noAlias?: boolean;
|
|
36
|
+
}[];
|
|
37
|
+
}
|
|
38
|
+
export declare function remapIsEmpty(t: RemapTargets): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Settings wtc could remap: hardcoded host ports (not ranges) and fixed
|
|
41
|
+
* container names, plus why some are left alone:
|
|
42
|
+
* - a service that uses `extends` may inherit ports wtc cannot see, which
|
|
43
|
+
* a replaced list would drop;
|
|
44
|
+
* - a service from an included file whose ports use variables: compose
|
|
45
|
+
* fills those in from that file's own env (its directory's `.env`), but
|
|
46
|
+
* an override is interpolated with the main project's;
|
|
47
|
+
* - a port too high to move (above 65435);
|
|
48
|
+
* - a container name another service joins by name (`network_mode:
|
|
49
|
+
* container:<name>`, `volumes_from`, `ipc`, `pid`): an alias cannot stand
|
|
50
|
+
* in for a container identity.
|
|
51
|
+
*/
|
|
52
|
+
export declare function remapCandidates(composeFile: ComposeFile, mappings: PortMapping[]): {
|
|
53
|
+
targets: RemapTargets;
|
|
54
|
+
skipped: string[];
|
|
55
|
+
};
|
|
56
|
+
/** The compose override file compose would load from `dir` by itself, or null. */
|
|
57
|
+
export declare function findUserOverride(dir: string): string | null;
|
|
58
|
+
/**
|
|
59
|
+
* Leave alone what the user's own compose override in `dir` also sets for
|
|
60
|
+
* a service (`ports` or `extends`, `container_name`): replacing it would
|
|
61
|
+
* discard theirs. A container name one of its services joins by name
|
|
62
|
+
* (`network_mode: container:<name>`, …) is kept too. Where it sets the
|
|
63
|
+
* service's networks, the old container name gets no alias (theirs could
|
|
64
|
+
* be `network_mode`). Returns what remains, with a warning for each
|
|
65
|
+
* setting left alone.
|
|
66
|
+
*/
|
|
67
|
+
export declare function withoutUserOverride(targets: RemapTargets, dir: string): {
|
|
68
|
+
targets: RemapTargets;
|
|
69
|
+
warnings: string[];
|
|
70
|
+
};
|
|
71
|
+
/** The separator compose splits `COMPOSE_FILE` on (wtc's calls and the env files it writes). */
|
|
72
|
+
export declare function composePathSeparator(): string;
|
|
73
|
+
/** A `COMPOSE_FILE` value for `files`, or null when a path contains the separator. */
|
|
74
|
+
export declare function composeFileValue(files: string[]): string | null;
|
|
75
|
+
/**
|
|
76
|
+
* The value an env file assigns to `key`: its last assignment, in either
|
|
77
|
+
* form compose's dotenv parser accepts (`KEY=value`, `KEY: value`),
|
|
78
|
+
* unquoted. Null when the file or the key is missing.
|
|
79
|
+
*/
|
|
80
|
+
export declare function envFileValue(file: string | null, key: string): string | null;
|
|
81
|
+
/** The `COMPOSE_FILE` an env file sets, or null. */
|
|
82
|
+
export declare function envFileComposeFile(file: string | null): string | null;
|
|
83
|
+
/** Where wtc keeps a stack's override: inside the git directory, never committed. */
|
|
84
|
+
export declare function overridePath(commonDir: string, project: string): string;
|
|
85
|
+
/**
|
|
86
|
+
* True when a `COMPOSE_FILE` value is exactly a list wtc writes — the
|
|
87
|
+
* compose file, then the compose override compose itself would pick in
|
|
88
|
+
* that directory if there is one, then one of wtc's overrides: the line
|
|
89
|
+
* wtc wrote into a worktree's env file, exported into the shell by a tool
|
|
90
|
+
* that loads `.env` (direnv's `dotenv`, mise, autoenv). That is not a file
|
|
91
|
+
* list of the user's, so it must not switch remapping off. Any other list
|
|
92
|
+
* (a file added, another override picked) is the user's.
|
|
93
|
+
*/
|
|
94
|
+
export declare function isWtcComposeFile(value: string, commonDir: string): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Whether the inherited `COMPOSE_FILE` names `file`: a list the shell still
|
|
97
|
+
* holds (direnv, or one the user extended) would break if it were deleted.
|
|
98
|
+
*/
|
|
99
|
+
export declare function inheritedComposeFileNames(file: string): boolean;
|
|
100
|
+
/** A `COMPOSE_FILE` the user set (not one wtc wrote), from a value. */
|
|
101
|
+
export declare function isUserComposeFile(value: string | null | undefined, commonDir: string): boolean;
|
|
102
|
+
export declare function writeOverride(file: string, content: string): void;
|
|
103
|
+
export declare function removeOverride(file: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* The override YAML for one stack. A service whose hardcoded ports are
|
|
106
|
+
* remapped gets its whole `ports` list (`!override` replaces it): every
|
|
107
|
+
* entry as written, except the remapped host ports. A service whose
|
|
108
|
+
* container_name is dropped (`!reset`) keeps the old name as an alias on
|
|
109
|
+
* its own networks. Strings are quoted, so YAML 1.1 parsers never read
|
|
110
|
+
* `2222:22` as a base-60 number.
|
|
111
|
+
*/
|
|
112
|
+
export declare function renderOverride(project: string, composeFile: ComposeFile, targets: RemapTargets, allocations: PortAllocation[]): string;
|
|
113
|
+
/** True when `config` output shows the probe override applied: one port, published on 2, no container_name. */
|
|
114
|
+
export declare function probeApplied(stdout: string): boolean;
|
|
115
|
+
/** Forget cached probe results (tests). */
|
|
116
|
+
export declare function resetRemapProbeCache(): void;
|
|
117
|
+
/**
|
|
118
|
+
* Whether the engine merges `!override` and `!reset` the way runtime
|
|
119
|
+
* remapping needs: `config` on a throwaway project in a temp directory,
|
|
120
|
+
* passed through `COMPOSE_FILE` exactly like the real override. It needs
|
|
121
|
+
* no daemon and touches no containers. Cached per process and engine (a
|
|
122
|
+
* failure for a minute).
|
|
123
|
+
*/
|
|
124
|
+
export declare function engineSupportsRemap(engine: ComposeEngine): boolean;
|
|
125
|
+
export interface RemapDecision {
|
|
126
|
+
/** What wtc remaps at runtime; null when there is nothing to remap or it cannot. */
|
|
127
|
+
targets: RemapTargets | null;
|
|
128
|
+
/** Why the candidates are not remapped, or null. */
|
|
129
|
+
problem: string | null;
|
|
130
|
+
/** Hardcoded settings left alone, and why. */
|
|
131
|
+
warnings: string[];
|
|
132
|
+
}
|
|
133
|
+
export interface RemapInputs {
|
|
134
|
+
composeFile: ComposeFile;
|
|
135
|
+
mappings: PortMapping[];
|
|
136
|
+
/** The main checkout (where its own compose override would be). */
|
|
137
|
+
mainRoot: string;
|
|
138
|
+
/** The git common dir (where wtc keeps the overrides). */
|
|
139
|
+
commonDir: string;
|
|
140
|
+
/**
|
|
141
|
+
* Env files that could carry a `COMPOSE_FILE` of the user's: the root
|
|
142
|
+
* env file wtc copies into worktrees, and the main checkout's `.env`
|
|
143
|
+
* (which compose reads by itself).
|
|
144
|
+
*/
|
|
145
|
+
envFiles: (string | null)[];
|
|
146
|
+
/** The root `envOverrides`, which wtc writes into the worktree's env file. */
|
|
147
|
+
envOverrides?: Record<string, string>;
|
|
148
|
+
/** Resolves the engine; throwing means "no engine" (nothing to remap with). */
|
|
149
|
+
engine: () => ComposeEngine;
|
|
150
|
+
}
|
|
151
|
+
/** Decide what to remap for this repository (see the module comment). */
|
|
152
|
+
export declare function decideRemap(inputs: RemapInputs): RemapDecision;
|