From 4cfc8e6acde820b0fd63cbcc2280fbe084c072df Mon Sep 17 00:00:00 2001 From: Worktrunk Bot Date: Wed, 13 May 2026 09:56:58 -0700 Subject: [PATCH] docs(extending): note alias template timing and how to defer to nested wt (#2754) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- docs/content/extending.md | 11 +++++++++++ skills/worktrunk/reference/extending.md | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/content/extending.md b/docs/content/extending.md index 70940f6fc..2f13ede53 100644 --- a/docs/content/extending.md +++ b/docs/content/extending.md @@ -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 diff --git a/skills/worktrunk/reference/extending.md b/skills/worktrunk/reference/extending.md index 037f0dc2a..73ed7618f 100644 --- a/skills/worktrunk/reference/extending.md +++ b/skills/worktrunk/reference/extending.md @@ -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