Skip to content

fix(hooks): fail a hook before it starts when its template error is already decided - #4787

Merged
max-sixty merged 2 commits into
mainfrom
agent-af5060245b62ee113
Nov 15, 2026
Merged

max-sixty merged 2 commits into
mainfrom
agent-af5060245b62ee113

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

A hook with an unguarded {{ branch }} can't expand in a detached worktree, where branch is unset by design (#4009). In a background hook that failure was silent: the runner renders each step (#3042), so wt remove announced the post-remove hook, exited 0, and the error reached only runner.log. In a jj-colocated repo, where every worktree is detached, that happens on every removal.

Hook preparation already rejects a template that can't parse before any step runs. PreparedPipeline::validated now also rejects one whose render fails without reading anything an earlier step can change, which excludes vars.* on a branch and Worktrunk's template functions. Such a template gets the same error and exit status as a syntax error, in every hook, foreground and background. An error that depends on vars or a function still surfaces when its step runs. The hook help says so, and points a command that identifies the worktree at worktree_name or worktree_path.

Worth weighing: wt remove with such a post-remove hook now removes the worktree, prints the error and exits 1, as a syntax error already makes it. Blocking hooks such as pre-remove now fail before step 1 rather than at the broken step, which reverses part of #3042's deferral for errors decided at preparation. Aliases keep deferring every expansion error to its step, and the picker's alt-x removal sends the error only to the debug log, as it does for syntax errors.

johnstegeman reported this in rex-config#9. Requested by max-sixty. With the source change reverted, the new detached post-remove test and the new decided-error foreground case fail; with it they pass. The run-time tests now use a vars-dependent fixture that really is deferred, and the hooks, remove, prune, merge, switch, picker and alias integration tests pass.

This was written by Claude Code on behalf of max-sixty

…ady decided

A `post-remove` hook with an unguarded `{{ branch }}` cannot expand for a
detached worktree. That much is the documented contract: since #4009,
`branch` is unset in a detached worktree rather than the literal `HEAD`,
which git resolves as a ref, and every hook path builds it that way. The
defect was where the error went. `pre-remove` reports it and blocks the
removal. `post-remove`, like every background hook, renders its templates
in the detached runner (#3042), so the error reached only
`.git/wt/logs/.../runner.log`: `wt remove` announced the hook, exited 0,
and the command never ran. In a jj-colocated repository, where jj keeps
every worktree detached, a hook that names `branch` failed this way on
every removal.

A pipeline's context is frozen when it is prepared. Before a step renders,
only `vars.*` (the branch's git config, which an earlier step may write)
and Worktrunk's template functions (which read the repository) can change.
A template that reads neither, or reads `vars` where there is no branch
and so no config behind it, renders the same before the pipeline spawns
as when its step runs. `run_hooks_background` now renders such templates
without `-v` output and prints the first error after the hook's announce
line. The pipeline still spawns, so earlier steps run as before, and the
command's exit status doesn't change. `TemplateContext::known_expansion_error`
makes the check over every read in the template, a local's included,
since `{% set f = f() %}` hides the call from MiniJinja's
`undeclared_variables`. It renders through `render_template`, the core of
`expand_template_with` without its logging.

The hook help now says that a command identifying the worktree rather than
its branch can use `worktree_name` or `worktree_path`, which every
worktree has. Guarding with `{% if branch %}` would turn such a hook into
a silent no-op in a detached worktree.
…cided

The previous commit printed a background hook's template error when the
frozen context already decided it, then spawned the pipeline anyway and
kept exit 0. That made three behaviors for a template that can't render:
a syntax error failed the hook before it started, a decided error printed
and ran the steps before it, and an error that depends on run-time state
reached only the runner's log.

Now there are two. `PreparedPipeline::validated` checks each template
with `TemplateContext::known_expansion_error` beside
`validate_template_syntax`, so a decided error fails the hook exactly as a
syntax error does: the same rendered error, exit 1, and no step runs, in
the foreground and the background alike. An unguarded `{{ branch }}` in a
`post-remove` for a detached worktree now leaves the removal done and
fails `wt remove` with the error. A template whose outcome depends on
`vars.*` on a branch, or on a template function, still renders when its
step runs, and in the background its error still reaches only the log.

`report_known_expansion_error` and `PreparedCommand::known_expansion_error`
are gone. `known_expansion_error` drops its escape-mode parameter, since
escaping can't make a render fail.

Behavior that changes for an error the context decides:
- `wt hook <post-*>` and every background hook: exit 1, nothing spawned.
- A foreground pipeline, including `--foreground` under the Warn policy:
  exit 1 before step 1. Warn still continues past failures that happen
  when a step runs.
- `wt hook <type> --dry-run`: the same error and exit 1 as before, raised
  before any preview prints.
- Aliases don't go through `validated` and are unchanged.

Tests that pinned the deferral (`test_foreground_pipeline_undefined_var_runs_earlier_steps`,
`test_foreground_template_failure_policy`,
`test_background_hook_undefined_var_fails_in_runner`) now use
`{% if not vars.ready %}…{% endif %}`, whose outcome an earlier step could
change, so they still cover the run-time path.
`test_foreground_pipeline_decided_error_aborts_before_first_step` covers a
syntax error and a decided undefined variable;
`test_user_post_remove_unguarded_branch_fails_before_hook_starts` covers
the reported case.
@max-sixty max-sixty changed the title fix(hooks): report a background hook's template error when it is already decided fix(hooks): fail a hook before it starts when its template error is already decided Nov 14, 2026
@max-sixty
max-sixty merged commit b200ed5 into main Nov 15, 2026
37 checks passed
@max-sixty
max-sixty deleted the agent-af5060245b62ee113 branch November 15, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants