Repository navigation
fix(templates): add the tojson filter for structured vars - #4524
Merged
Merged
Conversation
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
previously approved these changes
Oct 20, 2026
worktrunk-bot
left a comment
Collaborator
There was a problem hiding this comment.
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.
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]>
worktrunk-bot
approved these changes
Oct 20, 2026
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sjsonfeature was off. Since #3129 removed JSON on hook stdin, templates are how hooks get structured values, so they had no way to pass one tojqorcurl -d.This enables the
jsonfeature and documentstojsonbeside the vars dot-access example inwt hook --help.tojsonwrites',<,>and&inside strings as\uescapes, 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,.yamlor.yml, as it already did HTML escaping for.html. Worktrunk's template names are config keys, so a custom column namedjsonwould have rendered JSON-quoted cells, and one namedhtmlalready rendered HTML-escaped cells; the shared template environment now turns name-based autoescaping off. minijinja'stojsonalso writes an undefined value asnull, so a wrapper rejects it the way printing does, and a mistypedvarskey errors instead of sendingnull.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 namedjsonorhtmlrenders the raw value. New coverage: a doc-template test for the documentedcurlexample in POSIX mode, a missing-keytojsonerror case, and the custom-column test now uses a header namedjson. Each of the three fails with its part of the change reverted.Bare
{{ vars.cfg }}still renders Python-style;tojsonis the supported way to get JSON.