Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(step): apply -C to the tethered command; tether the docs server (#3207)

Maximilian Roos5ce076c22cf9
diff --git a/.config/wt.toml b/.config/wt.toml
index 4d00d6f05..dfaa0b59e 100644
--- a/.config/wt.toml
+++ b/.config/wt.toml
@@ -2,7 +2,11 @@
# Copy target/ from main worktree using CoW (~3s vs ~68s full rebuild)
deps = "wt step copy-ignored"
assets = "task fetch-assets"
-docs = "cd docs && zola serve -p {{ branch | hash_port }}"
+# `zola serve` runs forever; tether kills its whole process tree when the
+# worktree is removed (any path — `wt remove`, `git worktree remove`, `rm -rf`),
+# so no `pre-remove`/`post-remove` cleanup is needed. `-C docs` runs zola in the
+# docs subdir; the worktree root is still what teardown watches.
+docs = "wt step tether -C docs -- zola serve -p {{ branch | hash_port }}"
[post-merge]
clippy = "pre-commit run clippy --all-files || true"
@@ -24,6 +28,3 @@ doc = "RUSTDOCFLAGS='-Dwarnings' cargo doc --no-deps --document-private-items"
[list]
url = "http://127.0.0.1:{{ branch | hash_port }}"
-
-[post-remove]
-docs = "lsof -ti :{{ branch | hash_port }} -sTCP:LISTEN | xargs kill 2>/dev/null || true"
diff --git a/docs/content/step.md b/docs/content/step.md
index a871689df..59c53ce8d 100644
--- a/docs/content/step.md
+++ b/docs/content/step.md
@@ -980,6 +980,12 @@ For pipes, redirects, variables, or globs, wrap in `sh -c`:
{{ terminal(cmd="wt step tether -- sh -c 'PORT=$P npm run dev | tee dev.log'") }}
+To run the command from a subdirectory, pass the global `-C` flag (teardown
+still watches the worktree root, so a server launched with a relative `-C` is
+torn down with the worktree):
+
+{{ terminal(cmd="wt step tether -C frontend -- npm run dev") }}
+
### Examples
Run a dev server, torn down automatically when the worktree goes away:
diff --git a/skills/worktrunk/reference/step.md b/skills/worktrunk/reference/step.md
index 2c6b9d1fc..cdaa39709 100644
--- a/skills/worktrunk/reference/step.md
+++ b/skills/worktrunk/reference/step.md
@@ -1029,6 +1029,14 @@ For pipes, redirects, variables, or globs, wrap in `sh -c`:
$ wt step tether -- sh -c 'PORT=$P npm run dev | tee dev.log'
```
+To run the command from a subdirectory, pass the global `-C` flag (teardown
+still watches the worktree root, so a server launched with a relative `-C` is
+torn down with the worktree):
+
+```bash
+$ wt step tether -C frontend -- npm run dev
+```
+
### Examples
Run a dev server, torn down automatically when the worktree goes away:
diff --git a/src/cli/step.rs b/src/cli/step.rs
index 3c08fc27e..130976a5e 100644
--- a/src/cli/step.rs
+++ b/src/cli/step.rs
@@ -734,6 +734,14 @@ For pipes, redirects, variables, or globs, wrap in `sh -c`:
$ wt step tether -- sh -c 'PORT=$P npm run dev | tee dev.log'
```
+To run the command from a subdirectory, pass the global `-C` flag (teardown
+still watches the worktree root, so a server launched with a relative `-C` is
+torn down with the worktree):
+
+```console
+$ wt step tether -C frontend -- npm run dev
+```
+
## Examples
Run a dev server, torn down automatically when the worktree goes away:
diff --git a/src/commands/step/tether.rs b/src/commands/step/tether.rs
index f184e56ef..30ca19088 100644
--- a/src/commands/step/tether.rs
+++ b/src/commands/step/tether.rs
@@ -39,15 +39,27 @@ const POLL_INTERVAL: Duration = Duration::from_millis(250);
/// Run `command` supervised; tear its whole process tree down when the command
/// exits or its worktree is removed.
-pub(crate) fn step_tether(command: &[String]) -> Result<()> {
+///
+/// `working_dir` is the global `-C <path>` flag — applied as the child's
+/// current directory so the tethered command can run from a subdirectory (a
+/// dev server in `frontend/`, zola in `docs/`), the same way `-C` works for
+/// custom subcommands. The reaper still watches the captured cwd (the worktree
+/// root), not `working_dir`, so a relative `-C docs` server is still torn down
+/// when the worktree is removed.
+pub(crate) fn step_tether(command: &[String], working_dir: Option<&Path>) -> Result<()> {
// post-start hooks run with cwd at the worktree root; capture it now so
// the reaper can notice the worktree being removed. `None` (cwd
// unavailable) degrades to "tear down only on the command's own exit",
- // never a false teardown.
+ // never a false teardown. Captured before applying `-C` to the child so
+ // the reaper watches the worktree root even when the command runs in a
+ // subdirectory.
let worktree = std::env::current_dir().ok();
let mut cmd = std::process::Command::new(&command[0]);
cmd.args(&command[1..]);
+ if let Some(dir) = working_dir {
+ cmd.current_dir(dir);
+ }
// Direct exec, no implicit shell, same as `wt step for-each`; scrub the
// directive env vars so a long-lived child can't write to the parent
// shell's cd/exec directive files.
@@ -157,7 +169,7 @@ mod tests {
// started — exercising the `trace.fail` arm. (The success path, where
// the child runs and is reaped, is covered by the `step_tether`
// integration tests.)
- let err = step_tether(&["worktrunk-nonexistent-binary-7f3a9b2c".to_string()])
+ let err = step_tether(&["worktrunk-nonexistent-binary-7f3a9b2c".to_string()], None)
.expect_err("spawning a missing binary should fail");
assert!(
err.to_string().contains("spawn tethered command"),
diff --git a/src/main.rs b/src/main.rs
index fbc603702..a87bf6168 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -182,7 +182,11 @@ fn handle_hook_command(action: HookCommand, yes: bool) -> anyhow::Result<()> {
}
}
-fn handle_step_command(action: StepCommand, yes: bool) -> anyhow::Result<()> {
+fn handle_step_command(
+ action: StepCommand,
+ working_dir: Option<std::path::PathBuf>,
+ yes: bool,
+) -> anyhow::Result<()> {
match action {
StepCommand::Commit(args) => {
let verify = args.hooks.resolve();
@@ -390,7 +394,7 @@ fn handle_step_command(action: StepCommand, yes: bool) -> anyhow::Result<()> {
clobber,
format,
} => step_relocate(branches, dry_run, commit, clobber, format),
- StepCommand::Tether { command } => step_tether(&command),
+ StepCommand::Tether { command } => step_tether(&command, working_dir.as_deref()),
StepCommand::External(args) => commands::step_alias(args, yes),
}
}
@@ -857,7 +861,7 @@ fn dispatch_command(
) -> anyhow::Result<()> {
match command {
Commands::Config { action } => handle_config_command(action, yes),
- Commands::Step { action } => handle_step_command(action, yes),
+ Commands::Step { action } => handle_step_command(action, working_dir, yes),
Commands::Hook { action } => handle_hook_command(action, yes),
Commands::Select { branches, remotes } => handle_select_command(branches, remotes),
Commands::List(args) => handle_list_command(args),
diff --git a/tests/integration_tests/step_tether.rs b/tests/integration_tests/step_tether.rs
index bb5cf4319..5facf7230 100644
--- a/tests/integration_tests/step_tether.rs
+++ b/tests/integration_tests/step_tether.rs
@@ -132,3 +132,47 @@ fn test_tether_kills_process_group_when_command_exits(repo: TestRepo) {
"the tether supervisor must exit after the command exits"
);
}
+
+/// The global `-C <dir>` flag runs the tethered command in that directory —
+/// the canonical way to start a server from a subdirectory (zola in `docs/`,
+/// a dev server in `frontend/`) given the command runs directly with no shell
+/// to `cd`. The reaper still watches the launch cwd, not `-C`.
+#[rstest]
+fn test_tether_runs_command_in_working_dir(repo: TestRepo) {
+ let subdir = repo.path().join("subdir");
+ std::fs::create_dir(&subdir).unwrap();
+ let cwdfile = repo.home_path().join("tether-cwd.txt");
+
+ // The command writes its cwd and exits; the supervisor then sweeps the
+ // group and returns, so `status()` completes with nothing left running.
+ let status = repo
+ .wt_command()
+ .args([
+ "-C",
+ subdir.to_str().unwrap(),
+ "step",
+ "tether",
+ "--",
+ "sh",
+ "-c",
+ &format!("pwd -P > \"{}\"", cwdfile.display()),
+ ])
+ .current_dir(repo.path())
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status()
+ .expect("run wt step tether -C");
+ assert!(
+ status.success(),
+ "tether should exit 0 on command self-exit"
+ );
+
+ let recorded =
+ std::fs::read_to_string(&cwdfile).expect("tethered command should record its cwd");
+ assert_eq!(
+ std::fs::canonicalize(recorded.trim()).unwrap(),
+ std::fs::canonicalize(&subdir).unwrap(),
+ "the tethered command must run in the -C directory"
+ );
+}