Fix: Handle nested premature NJK template content errors - #4352
Open
khawkins98 wants to merge 2 commits into
Open
Fix: Handle nested premature NJK template content errors#4352khawkins98 wants to merge 2 commits into
khawkins98 wants to merge 2 commits into
Conversation
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.
tl;dr: This adds better error handling in the new v4 NJK template rendering + tests.
In Eleventy 4.0.0-alpha.10 with
@11ty/nunjucks4.0.0-alpha.3, aTemplateContentPrematureUseErrorthrown inside a Nunjucks filter can be wrapped in more than oneError.causelayer. Eleventy uses that error as a signal that a template has asked for another template’s rendered content too early: it normally catches the signal, renders the dependency, and tries again. Once Nunjucks has wrapped the error several layers deep, Eleventy no longer recognizes it and stops the build instead of retrying.Conceptually, the error changes from this:
to something like this:
The existing check recognizes the original error and one
Error.causewrapper, but not the additional nesting. This change recursively checks the recognized wrapper paths until it finds the original retry signal.Conceptually, the check changes from this:
to this:
This allows any number of normal
Error.causewrappers while the repeated-error check prevents an accidental cycle from looping forever. It does not treat every arbitraryoriginalErrorproperty as a wrapper.Reproduction
Minimal reproduction: https://github.com/khawkins98/eleventy-v4-template-content-repro
The reproduction uses a synchronous Nunjucks filter that reads
templateContentfrom a tagged collection:TemplateContentPrematureUseError.0.6.Implementation
ErrorUtil.isPrematureTemplateContentError()now walks nestedError.causevalues with cycle protection.The existing guarded Liquid
originalError.originalErrorpath remains restricted toRenderErrorandUndefinedVariableError; the same guarded check is now available beneath standardError.causewrappers. The implementation does not follow arbitraryoriginalErrorproperties.This extends the approach previously accepted in #3651, which added support for one level of
Error.causewhile retaining the Liquid guards.Tests
ErrorUtilrecognizes aTemplateContentPrematureUseErrorafter it passes through a Nunjucks filter.Error.causegraphs.npm run test:server: 1,438 passed, 28 skipped.npm run check: no errors; 25 pre-existing warnings.Alternatives
The Nunjucks integration could instead preserve the original retry signal at its wrapping boundary. I used recursive detection here because Eleventy already centralizes template-engine-specific recognition in
ErrorUtil, and #3651 establishedError.causehandling there. I am happy to adjust this if a fix in@11ty/nunjucksis preferred.Disclaimer
AI assistance was used to help reduce and test the reproduction and prepare this change.