Skip to content

feat(switch): validate templates before worktree creation - #1500

Merged
max-sixty merged 1 commit into
mainfrom
error-run
Mar 14, 2026
Merged

max-sixty merged 1 commit into
mainfrom
error-run

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Running wt switch --create --execute='{{ unclosed' would create the worktree, then fail on template expansion — leaving behind an orphan worktree that blocks re-running the command. Now templates are validated before the irreversible worktree creation.

Adds validate_template() which does a trial expansion with placeholder values for all known template variables, catching syntax errors and undefined variable references early. Extracts setup_template_env() so the validation and real expansion paths share identical minijinja configuration (filters, functions, undefined-behavior).

The validation is deliberately more permissive than real expansion — conditional variables like upstream or commit are provided as placeholders even though they may be absent at expansion time. This means some templates can pass validation but fail later (false negatives), which is an acceptable trade-off documented in the code. No realistic false positives exist.

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

Pre-flight validation catches syntax errors and undefined variable
references before the irreversible worktree creation, so a broken
`--execute` template or hook template doesn't leave behind a worktree
that blocks re-running the command.

Extracts `setup_template_env()` to share minijinja environment setup
between `validate_template()` and `expand_template()`, ensuring both
use identical filters, functions, and undefined-behavior settings.

The validation is deliberately more permissive than real expansion —
it provides all known variables as placeholders, so templates using
conditional variables (upstream, commit, etc.) unconditionally will
pass validation but may still fail at expansion time. This is an
acceptable trade-off documented in the code.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean, well-motivated PR — the problem (orphan worktrees from broken templates) is real, and pre-flight validation is the right fix. The setup_template_env extraction is a good refactor that removes duplication between validate_template and expand_template.

A few observations:

Observations (3)

1. validate_switch_templates duplicates hook-resolution logic

The hook iteration in validate_switch_templates (lines 363–395 of handle_switch.rs) re-derives which hooks apply and from which source (user vs project config), mirroring what the hook execution path already knows. If a new hook type or config source is added later, this function would need updating in lockstep. This is a reasonable trade-off for a pre-flight check — just noting it as a maintenance surface.

2. validate_template provides all vars including context-specific ones

The doc comment already explains this well — providing target, base, hook_type, etc. as placeholders means validation is permissive. One edge case: {{ hook_type }} will validate as "PLACEHOLDER" but during real expansion it gets a specific value like "post-create". If someone wrote {% if hook_type == "post-create" %}{{ base }}{% endif %}, both branches would be taken during validation (since both hook_type and base are "PLACEHOLDER"), which is fine — just means validation can't catch branch-specific errors. The trade-off is well-documented.

3. Snapshot updates confirm the fix works

The removed lines in the snapshots (worktree creation success message, shell integration warning) confirm that the error now fires before the worktree is created — exactly as intended.

Looks good to me.

@max-sixty
max-sixty merged commit 0d4ddd1 into main Mar 14, 2026
23 checks passed
@max-sixty
max-sixty deleted the error-run branch March 14, 2026 03:34
max-sixty added a commit that referenced this pull request Mar 14, 2026
…dation (#1504)

Investigated whether other commands beyond `switch` would benefit from
the pre-flight template validation added in #1500. Checked every command
that expands templates: merge, remove, hook, for-each, alias, list.

Switch is the only command where template failure after mutation creates
a blocking half-state — the worktree exists but the same `wt switch -c
<branch>` can't be re-run. Other commands either run hooks before the
irreversible operation (so errors abort cleanly) or after it completed
(so failure is a missed notification, not a blocking state).

Added a doc comment on `validate_switch_templates` documenting this
analysis.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request Mar 14, 2026
Both `validate_switch_templates` and the hook execution path
independently derived which hooks apply and how to resolve user/project
configs. If a new hook type or config source were added, both sites
would need updating in lockstep (flagged in #1500 review).

Two changes:

- Extract `switch_post_hook_types()` to share the hook-type list between
`approve_switch_hooks` and `validate_switch_templates`
- Make `lookup_hook_configs` `pub(crate)` and use it in `commit.rs` and
`step_commands.rs` instead of manual field access
(`user_hooks.pre_commit` / `project_config.hooks.pre_commit`)

Net -12 lines across 4 files.

> _This was written by Claude Code on behalf of @max-sixty_

---------

Co-authored-by: Claude <[email protected]>
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