Skip to content

[API]Circular references in templates causes "RangeError: Maximum call stack size exceeded" error #1522

@broofa

Description

@broofa

Description

If two templates reference one another, nunjucks.render() fails with the following error:

/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/lib.js:32
    err = new Error(old.message);
          ^

Template render error: (/private/tmp/nunjucks_circular_dependency_test/a.njk)
Template render error: (/private/tmp/nunjucks_circular_dependency_test/a.njk)
Template render error: (/private/tmp/nunjucks_circular_dependency_test/a.njk)
...
...line repeats ~1,500 times
...
  RangeError: Maximum call stack size exceeded
    at Object._prettifyError (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/lib.js:32:11)
    at /private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:464:19
    at Template.root [as rootRenderFunc] (eval at _compile (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:527:18), <anonymous>:23:3)
    at Template.render (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:454:10)
    at /private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:301:27
    at createTemplate (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:254:9)
    at handle (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:265:11)
    at /private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/environment.js:276:9
    at next (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/lib.js:258:7)
    at Object.asyncIter (/private/tmp/nunjucks_circular_dependency_test/node_modules/nunjucks/src/lib.js:263:3)

Node.js v24.2.0

How to reproduce

  1. Unzip the following [small, minimal] project
    nunjucks_circular_dependency_test.zip
  2. cd nunjucks_circular_dependency_test
  3. npm install
  4. npm test

Expected behavior

  1. The Error#message should be concise and descriptive (i.e. not 1,500 lines long!)
  2. It should describe the problem (circular dependency between templates). Ideally it would describe all of the templates involed in the dependency. (e.g. "Circular dependency detected: a.njk -> b.njk -> a.njk".
  3. It should contain appropriate lineno and colno properties.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions