From 0658c24a0fe55edabe6bebb1cf4ba613249d2021 Mon Sep 17 00:00:00 2001 From: mnobile-hs <191622299+mnobile-hs@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:24:00 -0800 Subject: [PATCH 1/2] Record a flattened import's own alias as its full import alias Since #1368, an aliased import inside a macro from another aliased import is bound in the current scope once the parent import has finished, instead of being nested under the parent's alias map. Its full import alias was still the dotted `parent.child` path, though, and that is the path any import inside the child file walks to find where to nest its own alias map. So if a file imported this way has its own top-level aliased import, that import walks `parent.child`, finds nothing at `child`, and fails with "Encountered a problem with import alias maps". The error aborts the whole macro call, and eager output loses everything the macro would have rendered. Default execution mode renders it normally. Decide once, in the constructor, whether to nest under the parent (only while the parent import is still in progress), and build the full import alias from that decision. Imports inside a flattened import then nest under its alias map's actual location. Co-Authored-By: Claude Opus 5.5 --- .../eager/importing/AliasedEagerImportingStrategy.java | 10 ++++------ .../jinjava/lib/tag/eager/EagerImportTagTest.java | 10 ++++++++++ .../eager/importtag/nested-top-import-middle.jinja | 3 +++ .../tags/eager/importtag/nested-top-import-outer.jinja | 3 +++ 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/tags/eager/importtag/nested-top-import-middle.jinja create mode 100644 src/test/resources/tags/eager/importtag/nested-top-import-outer.jinja diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/eager/importing/AliasedEagerImportingStrategy.java b/src/main/java/com/hubspot/jinjava/lib/tag/eager/importing/AliasedEagerImportingStrategy.java index f73ee4180..425aca507 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/eager/importing/AliasedEagerImportingStrategy.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/eager/importing/AliasedEagerImportingStrategy.java @@ -37,7 +37,7 @@ public AliasedEagerImportingStrategy( this.currentImportAlias = currentImportAlias; maybeParentImportAlias = importingData.getOriginalInterpreter().getContext().getImportResourceAlias(); - if (maybeParentImportAlias.isPresent()) { + if (isParentImportInProgress(importingData.getOriginalInterpreter())) { fullImportAlias = String.format("%s.%s", maybeParentImportAlias.get(), currentImportAlias); } else { @@ -138,9 +138,7 @@ public String getFinalOutput(String output, JinjavaInterpreter child) { @SuppressWarnings("unchecked") private void constructFullAliasPathMap(JinjavaInterpreter child) { - String[] allAliases = isParentImportInProgress(child) - ? fullImportAlias.split("\\.") - : new String[] { currentImportAlias }; + String[] allAliases = fullImportAlias.split("\\."); Map currentMap = child.getContext().getParent(); for (int i = 0; i < allAliases.length - 1; i++) { Object maybeNextMap = currentMap.get(allAliases[i]); @@ -164,10 +162,10 @@ private void constructFullAliasPathMap(JinjavaInterpreter child) { ); } - private boolean isParentImportInProgress(JinjavaInterpreter child) { + private boolean isParentImportInProgress(JinjavaInterpreter interpreter) { return maybeParentImportAlias .map(MetaContextVariables::getTemporaryImportAlias) - .map(child.getContext()::containsKey) + .map(interpreter.getContext()::containsKey) .orElse(false); } diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java index 4903444e8..9cd008ba7 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java @@ -549,6 +549,16 @@ public void itDoesNotAddImportAliasToLocalDictShadowingParentAlias() { assertThat(result.trim()).isEqualTo("B_B_B-{'name': 'local'}"); } + @Test + public void itImportsTopLevelAliasOfFileImportedInsideMacroFromAliasedImport() { + setupResourceLocator(); + String result = interpreter.render( + "{% import 'nested-top-import-outer.jinja' as outer %}{{ outer.render() }}" + ); + assertThat(interpreter.getErrors()).isEmpty(); + assertThat(result.trim()).isEqualTo("B_B_B"); + } + @Test public void itKeepsDeferredImportAliasesInsideOwnScope() { setupResourceLocator(); diff --git a/src/test/resources/tags/eager/importtag/nested-top-import-middle.jinja b/src/test/resources/tags/eager/importtag/nested-top-import-middle.jinja new file mode 100644 index 000000000..595a2bc80 --- /dev/null +++ b/src/test/resources/tags/eager/importtag/nested-top-import-middle.jinja @@ -0,0 +1,3 @@ +{% import 'printer-b.jinja' as printer %}{% macro go() -%} +{{ printer.print() }} +{%- endmacro %} diff --git a/src/test/resources/tags/eager/importtag/nested-top-import-outer.jinja b/src/test/resources/tags/eager/importtag/nested-top-import-outer.jinja new file mode 100644 index 000000000..235074b69 --- /dev/null +++ b/src/test/resources/tags/eager/importtag/nested-top-import-outer.jinja @@ -0,0 +1,3 @@ +{% macro render() -%} +{% import 'nested-top-import-middle.jinja' as middle %}{{ middle.go() }} +{%- endmacro %} From f4c97e5cba20f434a1d55e99cff8dd85dc7552e9 Mon Sep 17 00:00:00 2001 From: mnobile-hs <191622299+mnobile-hs@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:41:52 -0800 Subject: [PATCH 2/2] Cover imports inside a flattened import's macro called in a deferred block While a macro runs in a deferred block, the temporary alias of the import it came from is in scope. Before the previous commit, an import inside one of that file's macros therefore treated its flattened parent as still importing and walked the stale dotted alias, failing with "Encountered a problem with import alias maps". Co-Authored-By: Claude Opus 5.5 --- .../jinjava/lib/tag/eager/EagerImportTagTest.java | 14 ++++++++++++++ .../importtag/deferred-nested-import-middle.jinja | 3 +++ .../importtag/deferred-nested-import-outer.jinja | 3 +++ 3 files changed, 20 insertions(+) create mode 100644 src/test/resources/tags/eager/importtag/deferred-nested-import-middle.jinja create mode 100644 src/test/resources/tags/eager/importtag/deferred-nested-import-outer.jinja diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java index 9cd008ba7..59f148cfc 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java @@ -559,6 +559,20 @@ public void itImportsTopLevelAliasOfFileImportedInsideMacroFromAliasedImport() { assertThat(result.trim()).isEqualTo("B_B_B"); } + @Test + public void itImportsInsideMacroOfImportFromMacroCalledInDeferredBlock() { + setupResourceLocator(); + String result = interpreter.render( + "{% import 'deferred-nested-import-outer.jinja' as outer %}" + + "{% if deferred %}{{ outer.render() }}{% endif %}" + ); + assertThat(interpreter.getErrors()).isEmpty(); + removeDeferredContextKeys(); + context.put("deferred", true); + context.getDeferredTokens().clear(); + assertThat(interpreter.render(result).trim()).isEqualTo("B_B_B"); + } + @Test public void itKeepsDeferredImportAliasesInsideOwnScope() { setupResourceLocator(); diff --git a/src/test/resources/tags/eager/importtag/deferred-nested-import-middle.jinja b/src/test/resources/tags/eager/importtag/deferred-nested-import-middle.jinja new file mode 100644 index 000000000..608d38497 --- /dev/null +++ b/src/test/resources/tags/eager/importtag/deferred-nested-import-middle.jinja @@ -0,0 +1,3 @@ +{% macro go() -%} +{% import 'printer-b.jinja' as printer %}{{ printer.print() }} +{%- endmacro %} diff --git a/src/test/resources/tags/eager/importtag/deferred-nested-import-outer.jinja b/src/test/resources/tags/eager/importtag/deferred-nested-import-outer.jinja new file mode 100644 index 000000000..3366200b6 --- /dev/null +++ b/src/test/resources/tags/eager/importtag/deferred-nested-import-outer.jinja @@ -0,0 +1,3 @@ +{% macro render() -%} +{% import 'deferred-nested-import-middle.jinja' as middle %}{{ middle.go() }} +{%- endmacro %}