Skip to content

fix(eager): don't reset loop-modified variables at the start of each deferred iteration - #1

Closed
AlpriElse wants to merge 2 commits into
masterfrom
fix/eager-deferred-loop-reset
Closed

AlpriElse wants to merge 2 commits into
masterfrom
fix/eager-deferred-loop-reset

Conversation

@AlpriElse

@AlpriElse AlpriElse commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Context

EagerForTag handles {% for %} in eager execution mode.

When the loop's collection is deferred, as in {% for i in deferred %}, it can't run the loop, so it writes the loop back out as template code to be rendered later.

This pull request calls that first render the eager pass and the later render the second pass.

Problem

To find out which outer variables the loop body changes, EagerForTag renders the loop-body twice.

The variables it changes are set in front of the loop, which is correct. But the second run's copy of those set tags was also written at the start of the loop body, so the variable was reset on every iteration.

It only happens when the body reads a variable and then assigns it a value that doesn't depend on it. foo = foo + 1, the case in the existing itDefersNodeWhenModifiedInForLoop fixture, takes a different path, which is why that fixture didn't catch it.

Broken Examples

{# flag = 0 from the context; deferred = [1, 2] on the second pass #}
{% for i in deferred %}{{ flag }}|{% set flag = 7 %}{% endfor %}
{# evaluates to 0|0|; expected 0|7| #}

{# item = 0 from the context; the loop variable has the same name #}
{% for item in deferred %}{{ item }}|{% set item = 0 %}{% endfor %}
{# evaluates to 0|0|; expected 1|2| #}

Eager output for the first example before this change. Note the second set inside the loop:

// after the eager render pass (this is incorrect)
{% set flag = 0 %}{% for i in deferred %}{% set flag = 0 %}{{ flag }}|{% set flag = 7 %}{% endfor %}

What this PR Does

In EagerForTag.eagerInterpret, the loop body now only includes set tags for variables that aren't already set in front of the loop. Assignments carry over between iterations again, and a loop variable with the same name as an outer variable is no longer overwritten. Eager output for the first example:

{% set flag = 0 %}{% for i in deferred %}{{ flag }}|{% set flag = 7 %}{% endfor %}

Only eager output for deferred loops changes. No existing test expectations change.

Tests

  • New in EagerForTagTest: itKeepsAssignmentsAcrossIterationsOfDeferredLoop and itDoesNotOverwriteLoopVariableShadowingAssignedOuterVariable. Both fail on master and pass with this change.
  • EagerForTagTest, ForTagTest, EagerTest and NonRevertingEagerTest all pass (445 tests). The full mvn verify passes.

Results

Template Before After
{% for i in deferred %}{{ flag }}|{% set flag = 7 %}{% endfor %} 0|0| 0|7|
{% for item in deferred %}{{ item }}|{% set item = 0 %}{% endfor %} 0|0| 1|2|

🤖 Generated with Claude Code

AlpriElse and others added 2 commits November 24, 2026 00:00
…deferred iteration

When a deferred for loop's body reads a variable and then assigns it a
value that doesn't depend on it (`{{ a }}|{% set a = 7 %}`), eager
execution preserved the variable's pre-loop value twice: once in front
of the loop (correct) and again at the start of the loop body, which
reset it on every iteration. Default mode renders `0|7|`; the eager
output rendered `0|0|`. A loop variable with the same name as the
outer variable was overwritten the same way.

Only emit second-run prefix entries for variables that the prefix in
front of the loop doesn't already preserve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlpriElse

Copy link
Copy Markdown
Owner Author

Pr opened on HubSpot/jinjava

@AlpriElse AlpriElse closed this Nov 24, 2026
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.

1 participant