Skip to content

fix(eager): preserve parent state read by a deferred aliased import - #1373

Draft
AlpriElse wants to merge 6 commits into
HubSpot:masterfrom
AlpriElse:fix/eager-deferred-import-parent-state
Draft

AlpriElse wants to merge 6 commits into
HubSpot:masterfrom
AlpriElse:fix/eager-deferred-import-parent-state

Conversation

@AlpriElse

Copy link
Copy Markdown
Contributor

Touches the same code as #1369, but fixes a different problem.

Context

EagerImportTag handles {% import 'lib' as x %} in eager execution mode.

When the import is inside a deferred block, such as {% if deferred %}, and lib defines a macro, the import can't be inlined. It's written back out as a raw {% import %} tag, which runs again when the output is fully rendered later.

Problem

When the import was left as a raw tag, the parent template's state that lib reads, such as earlier set tags and macros defined in the parent, wasn't kept in the eager output.

On the second render pass, lib saw those as undefined.

EagerImportTag throws away the child's render in this case and calls AliasedEagerImportingStrategy.handleDeferredTemplateFile. That method records only the path expression and the alias. Nothing recorded which parent variables the child reads, so they were never written out in front of the import. include and from ... import aren't affected, because they inline the child's render instead of running it again.

Broken Examples

{#
  lib:
    {% macro mm(p) %}[]{% endmacro %}
    {% set v = x + 1 %}
#}
{% set x = 5 %}{% if deferred %}{% import 'lib' as lib %}{{ lib.v }}{% endif %}
{#
  evaluates to: 1  (x is undefined on the second pass, and undefined + 1 = 1)
  expected:     6
#}

{#
  lib:
    {% macro mm(p) %}[]{% endmacro %}
    {% set v = m(0) %}
#}
{% macro m(p) %}[{{ p }}]{% endmacro %}{% if deferred %}{% import 'lib' as lib %}{{ lib.v }}{% endif %}
{#
  fails with:   "Could not resolve function 'm'"
  expected:     [0]
#}

What this PR Does

When the import of a macro-defining template is deferred, EagerImportTag now collects the candidate names (variables and macros) that the imported template's source references. When it returns the raw import, it writes the parent's variables and macros among those names in front of it, using EagerReconstructionUtils.hydrateReconstructionFromContextBeforeDeferring and a DeferredToken. EagerPrintTag and EagerForTag already use this pattern. Eager output for the first example:

{% set x = 5 %}{% if deferred %}{% set current_path = '' %}{% import 'lib' as lib %}{{ lib.v }}{% endif %}

Only EagerImportTag changes; the importing strategies and shared eager utilities don't. No existing test expectations change.

Tests

  • New in EagerImportTagTest, with fixtures macro-and-set-reading-parent-var.jinja and macro-and-set-calling-parent-macro.jinja: itPreservesParentVariableReadByDeferredImportWithMacro and itPreservesParentMacroCalledByDeferredImportWithMacro. Both fail on master and pass with this change. The second pass runs in a fresh default-mode interpreter, so nothing from the eager pass leaks in.
  • The eager and import test classes (Eager*Test, NonRevertingEagerTest, *Import*Test, DeferredTest) all pass (811 tests). The full mvn verify passes (2251 tests).
  • This branch merges cleanly with fix(eager): keep nested import alias paths consistent with flattened parent imports #1369, which also changes aliased imports. With both changes, the same 813 eager and import tests pass.

Results

The same templates as the Broken Examples above:

{# Example 1 #}
{% set x = 5 %}{% if deferred %}{% import 'lib' as lib %}{{ lib.v }}{% endif %}

{# Example 2 #}
{% macro m(p) %}[{{ p }}]{% endmacro %}{% if deferred %}{% import 'lib' as lib %}{{ lib.v }}{% endif %}
Example Before After
1 1 6
2 FATAL: Could not resolve function 'm' [0]

🤖 Generated with Claude Code

AlpriElse and others added 5 commits November 24, 2026 00:03
When an aliased import inside a deferred block (or one whose child has
deferred nodes) is left as a raw import tag for the next pass, the
parent's variables and macros that the imported template reads were not
reconstructed, so the second pass saw them as undefined. Collect the
words the imported template uses and reconstruct/defer them in front of
the deferred import tag, as other deferred tags do.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The second pass used jinjava.newInterpreter(), whose config has the test
base's legacy overrides, while the eager pass used its own. Build both
from one config method that only differs in execution mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlpriElse
AlpriElse marked this pull request as ready for review November 24, 2026 10:05
@AlpriElse

Copy link
Copy Markdown
Contributor Author

@jasmith-hs these are all my gifts for tonight sweet child, don't wanna burn all my Claude limits for these funzies but I've got another dozen unconfirmed bugs found

…erred import

When an aliased import of a macro-defining template is deferred, the parent
macros the imported template reads are reconstructed in front of it. A
parent macro that uses caller(), called by the imported template with
{% call %}, failed to reconstruct: caller() couldn't be resolved, which is
a FATAL error, and the rebuilt macro lost its {{ caller() }}.

Like EagerCallTag, register a deferred placeholder caller macro in a
temporary scope while reconstructing, so caller() stays in the macro.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlpriElse
AlpriElse marked this pull request as draft November 24, 2026 10:55
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