From 2130ca84764e75c7ae9106cec671caa73ddbe416 Mon Sep 17 00:00:00 2001 From: William Tan <1284324+Ninja3047@users.noreply.github.com> Date: Thu, 15 Oct 2026 03:14:01 -0400 Subject: [PATCH] jinja : fix loop body variable scoping (#30533) Assisted-by: Codex --- common/jinja/runtime.cpp | 7 ++++--- tests/test-jinja.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/common/jinja/runtime.cpp b/common/jinja/runtime.cpp index b8a69995b..a53acf84c 100644 --- a/common/jinja/runtime.cpp +++ b/common/jinja/runtime.cpp @@ -600,6 +600,7 @@ value for_statement::execute_impl(context & ctx) const { bool noIteration = true; for (size_t i = 0; i < filtered_items.size(); i++) { JJ_DEBUG("For loop iteration %zu/%zu", i + 1, filtered_items.size()); + context loop_scope(scope); value_object loop_obj = mk_val(); loop_obj->has_builtins = false; // loop object has no builtins loop_obj->insert("index", mk_val(i + 1)); @@ -611,11 +612,11 @@ value for_statement::execute_impl(context & ctx) const { loop_obj->insert("length", mk_val(filtered_items.size())); loop_obj->insert("previtem", i > 0 ? filtered_items[i - 1] : mk_val("previtem")); loop_obj->insert("nextitem", i < filtered_items.size() - 1 ? filtered_items[i + 1] : mk_val("nextitem")); - scope.set_val("loop", loop_obj); - scope_update_fns[i](scope); + loop_scope.set_val("loop", loop_obj); + scope_update_fns[i](loop_scope); try { for (auto & stmt : body) { - value val = stmt->execute(scope); + value val = stmt->execute(loop_scope); result->push_back(val); } } catch (const continue_statement::signal &) { diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp index 572b30816..9b53e8cec 100644 --- a/tests/test-jinja.cpp +++ b/tests/test-jinja.cpp @@ -360,6 +360,38 @@ static void test_loops(testing & t) { "1x1y2x2y" ); + test_template(t, "loop assignments do not persist between iterations", + "{% for i in [1, 2] %}{% if i == 1 %}{% set x = 7 %}{% endif %}{{ x is defined }};{% endfor %}", + json::object(), + "True;False;" + ); + + test_template(t, "loop assignments shadow outer variables", + "{% set x = 9 %}{% for i in [1, 2] %}{% if i == 1 %}{% set x = 7 %}{% endif %}{{ x }};{% endfor %}{{ x }}", + json::object(), + "7;9;9" + ); + + test_template(t, "nested loop assignments do not persist between iterations", + "{% for i in [1, 2] %}{% for j in [1, 2] %}{% if j == 1 %}{% set x = i %}{% endif %}{{ x is defined }};{% endfor %}{% endfor %}", + json::object(), + "True;False;True;False;" + ); + + test_template(t, "reasoning is local to each message", + "{% for m in messages %}{% if m.reasoning_content is string %}{% set reasoning_content = m.reasoning_content %}{% elif '' in m.content %}{% set reasoning_content = m.content.split('')[0].split('')[-1] %}{% endif %}[{{ reasoning_content is defined }}:{{ reasoning_content if reasoning_content is defined else '' }}]{% endfor %}", + {{"messages", json::array({ + {{"reasoning_content", "first"}, {"content", "answer"}}, + {{"content", "answer"}}, + {{"reasoning_content", nullptr}, {"content", "answer"}}, + {{"reasoning_content", 1}, {"content", "answer"}}, + {{"reasoning_content", ""}, {"content", "answer"}}, + {{"content", "embeddedanswer"}}, + {{"content", "answer"}} + })}}, + "[True:first][False:][False:][False:][True:][True:embedded][False:]" + ); + test_template(t, "for with range", "{% for i in range(3) %}{{ i }}{% endfor %}", json::object(),