Skip to content

fix(templates): add the tojson filter for structured vars - #4524

Merged
max-sixty merged 3 commits into
mainfrom
feat/template-tojson
Oct 20, 2026
Merged

max-sixty merged 3 commits into
mainfrom
feat/template-tojson

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Since the minijinja 3 upgrade in #4442, {{ vars.cfg }} renders a JSON object or array var in Python style ({'port': 3000}) where v0.80.0 rendered JSON, and {{ vars.cfg | tojson }} fails with "unknown filter" because minijinja's json feature was off. Since #3129 removed JSON on hook stdin, templates are how hooks get structured values, so they had no way to pass one to jq or curl -d.

This enables the json feature and documents tojson beside the vars dot-access example in wt hook --help. tojson writes ', <, > and & inside strings as \u escapes, which JSON parsers decode; the shell formatter still single-quotes the result, so -d {{ vars.config | tojson }} passes one argument.

Two adjustments come with the feature. It makes minijinja choose JSON autoescaping from a template name ending in .json, .js, .yaml or .yml, as it already did HTML escaping for .html. Worktrunk's template names are config keys, so a custom column named json would have rendered JSON-quoted cells, and one named html already rendered HTML-escaped cells; the shared template environment now turns name-based autoescaping off. minijinja's tojson also writes an undefined value as null, so a wrapper rejects it the way printing does, and a mistyped vars key errors instead of sending null.

Verified by rerunning the release-review repro against the branch binary: {{ vars.cfg | tojson }} now renders {"list": [1, 2], "port": 3000}, and a custom column named json or html renders the raw value. New coverage: a doc-template test for the documented curl example in POSIX mode, a missing-key tojson error case, and the custom-column test now uses a header named json. Each of the three fails with its part of the change reverted.

Bare {{ vars.cfg }} still renders Python-style; tojson is the supported way to get JSON.

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

max-sixty and others added 2 commits October 19, 2026 22:27
Since the minijinja 3 upgrade (#4442), `{{ vars.cfg }}` renders a JSON
object or array var in Python style (`{'port': 3000}`), where v0.80.0
rendered JSON. With #3129 removing JSON on hook stdin, templates are how
hooks receive structured values, and `{{ vars.cfg | tojson }}` failed
with "unknown filter" because minijinja's `json` feature was off.

Enable the `json` feature so `tojson` exists, and document it beside
the vars dot-access example in `wt hook --help`. The filter writes `'`,
`<`, `>` and `&` inside strings as `\u` escapes, which JSON parsers
decode back; the shell formatter still single-quotes the result.

The feature also makes minijinja choose JSON autoescaping from a
template name ending in `.json`, `.js`, `.yaml` or `.yml`, as it
already chose HTML escaping for `.html`. Template names are config keys,
so a custom column named `json` would have rendered JSON-quoted cells
and one named `html` already rendered HTML-escaped cells. The shared
template environment now turns name-based autoescaping off.

minijinja's `tojson` writes an undefined value as `null`. Worktrunk's
wrapper rejects it as printing does, so a mistyped `vars` key errors
instead of sending `null` to a command.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes Oct 20, 2026

@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.

nix-flake fails on the new tojson assertion in test_expand_template_vars_json_dot_access: the flake's worktrunk-tests check builds without debug_assertions, so MiniJinja's environment debug flag is off and the error reads Failed to expand test: undefined value @ line 1, without the expression name (job log). The existing undefined-value test in the same file already accepts both the named and unnamed forms for this reason. The suggestion below checks the error kind, which both builds produce.

Comment thread src/config/expansion.rs Resolved
@worktrunk-bot
worktrunk-bot dismissed their stale review October 20, 2026 07:29

CI failed — nix-flake: new tojson assertion expects the debug-only error form

…d error

MiniJinja names the undefined expression only with debug info on, so the
release-profile nix check saw "undefined value @ line 1".

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit d890a16 into main Oct 20, 2026
43 checks passed
@max-sixty
max-sixty deleted the feat/template-tojson branch October 20, 2026 15:40
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