mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.54k stars 638 forks source link

Including template many times in forloop causes a `RangeError: Maximum call stack size exceeded` #643

Open hanamura opened 8 years ago

hanamura commented 8 years ago

It may depend on an environment how many items matter, but using include many times in forloop causes a RangeError.

Steps to reproduce

JavaScript code:

Templates:

Render template with 10,000 items:

node index.js 10000 problem-a.html

Expect to proceed without errors, but actually RangeError is caused:

Template render error: (/home/travis/build/hanamura/nunjucks-include-range-error/template/problem-a.html)
  Template render error: (/home/travis/build/hanamura/nunjucks-include-range-error/template/problem-a.html)
  Template render error: (/home/travis/build/hanamura/nunjucks-include-range-error/template/problem-b.html)
  Template render error: (/home/travis/build/hanamura/nunjucks-include-range-error/template/problem-a.html)

...

  RangeError: Maximum call stack size exceeded
    at Object.exports.prettifyError (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/lib.js:34:15)
    at /home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:485:31
    at root [as rootRenderFunc] (eval at <anonymous> (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:564:24), <anonymous>:39:3)
    at Obj.extend.render (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:478:15)
    at /home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:311:35
    at createTemplate (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:234:25)
    at handle (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:249:25)
    at /home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/environment.js:263:21
    at next (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/lib.js:207:13)
    at Object.exports.asyncIter (/home/travis/build/hanamura/nunjucks-include-range-error/node_modules/nunjucks/src/lib.js:214:5)

It is not caused with a few files. This works fine:

node index.js 10 problem-a.html

Workaround

Avoid using include and write templates inline.

Template:

Render:

node index.js 10000 workaround-a.html

This is an example repository:

git clone https://github.com/hanamura/nunjucks-include-range-error.git
cd nunjucks-include-range-error
npm install
npm test

This is Travis CI URL that represent it:

carljm commented 8 years ago

Thanks for the report! I'll be honest - I think this may be complex to fix, and I don't personally have any intention to work on it. I would gladly review and accept a pull request that fixes it, however!

hydiak commented 8 years ago

There is no such problem with actual version of nunjucks: https://github.com/hanamura/nunjucks-include-range-error/pull/1

But this problem is still exists. I'll try to make reproducible test case.