docs(extending): note alias template timing and how to defer to nested wt (#2754)

Addresses [#2753](https://github.com/max-sixty/worktrunk/issues/2753) —
adds a short subsection under Aliases documenting that templates render
at alias dispatch (using the alias-invocation worktree's context), so a
nested `wt` command's own template variables resolve against the outer
worktree unless wrapped in `{% raw %}…{% endraw %}`.

The example uses the reporter's pattern (`wt switch ... --execute 'echo
{{ worktree_path }}'`) verified against the release binary in a scratch
repo:

- Direct: `wt switch other --no-cd --execute 'echo {{ worktree_path }}'`
-> target path
- Aliased without `{% raw %}`: -> outer worktree path (the bug)
- Aliased with `{% raw %}{{ worktree_path }}{% endraw %}`: -> target
path

`test_docs_are_in_sync` propagates the change into
`skills/worktrunk/reference/extending.md`.

Co-authored-by: worktrunk-bot <254187624+worktrunk-bot@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Worktrunk Bot
2026-05-13 09:56:58 -07:00
committed by GitHub
co-authored by worktrunk-bot Claude Opus 4.7
parent 52555982a8
commit 4cfc8e6acd
2 changed files with 22 additions and 0 deletions
+11
View File
@@ -117,6 +117,17 @@ Every step sees the same `{{ args }}` and bound variables — `wt release -- --d
`wt` commands that change the parent shell's directory — `wt switch`, `wt merge` (leaving the removed source), `wt remove` of the current worktree — still do so when invoked from an alias; the Worktrunk shell integration propagates the change through. Other shell state doesn't persist: the alias runs in a subshell, so `cd`, `export`, and similar commands only affect that subshell.
### Deferring expansion to a nested `wt` command
Alias templates render once at dispatch, using the alias-invocation worktree's context. A nested `wt` command in the alias body — for example `wt switch --execute '…'` — therefore receives already-rendered text, so a variable like `{{ worktree_path }}` inside the inner command's template resolves to the *outer* worktree rather than the one `wt switch` is targeting. Wrap the inner template in `{% raw %}…{% endraw %}` so it passes through unrendered and the inner `wt` command expands it in its own context:
```toml
[aliases]
echo-target = "wt switch {{ args }} --no-cd --execute 'echo {% raw %}{{ worktree_path }}{% endraw %}'"
```
`wt echo-target other` now prints the path of the `other` worktree, not the worktree the alias was invoked from.
### Recipe: rebase every worktree onto its upstream
```toml
+11
View File
@@ -121,6 +121,17 @@ Every step sees the same `{{ args }}` and bound variables — `wt release -- --d
`wt` commands that change the parent shell's directory — `wt switch`, `wt merge` (leaving the removed source), `wt remove` of the current worktree — still do so when invoked from an alias; the Worktrunk shell integration propagates the change through. Other shell state doesn't persist: the alias runs in a subshell, so `cd`, `export`, and similar commands only affect that subshell.
### Deferring expansion to a nested `wt` command
Alias templates render once at dispatch, using the alias-invocation worktree's context. A nested `wt` command in the alias body — for example `wt switch --execute '…'` — therefore receives already-rendered text, so a variable like `{{ worktree_path }}` inside the inner command's template resolves to the *outer* worktree rather than the one `wt switch` is targeting. Wrap the inner template in `{% raw %}…{% endraw %}` so it passes through unrendered and the inner `wt` command expands it in its own context:
```toml
[aliases]
echo-target = "wt switch {{ args }} --no-cd --execute 'echo {% raw %}{{ worktree_path }}{% endraw %}'"
```
`wt echo-target other` now prints the path of the `other` worktree, not the worktree the alias was invoked from.
### Recipe: rebase every worktree onto its upstream
```toml