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.48k stars 634 forks source link

Templating fails when a comment contains '#}' #1428

Open mgarstecki opened 1 year ago

mgarstecki commented 1 year ago

Hi,

I get the following error when my template contains the string #} as a comment:

  Error: unexpected end of comment
    at Object._prettifyError (/home/mat/node_modules/nunjucks/src/lib.js:36:11)
    at Template.render (/home/mat/node_modules/nunjucks/src/environment.js:538:21)
    at /home/mat/node_modules/nunjucks/src/environment.js:366:27
    at createTemplate (/home/mat/node_modules/nunjucks/src/environment.js:315:9)
    at handle (/home/mat/node_modules/nunjucks/src/environment.js:327:11)
    at /home/mat/node_modules/nunjucks/src/environment.js:339:9
    at next (/home/mat/node_modules/nunjucks/src/lib.js:328:7)
    at Object.asyncIter (/home/mat/node_modules/nunjucks/src/lib.js:334:3)
    at Environment.getTemplate (/home/mat/node_modules/nunjucks/src/environment.js:321:9)
    at Environment.render (/home/mat/node_modules/nunjucks/src/environment.js:360:10)

Minimal template that causes the issue:

#}

This is an issue because that pattern can easily be introduced when commenting blocks in the template. I encountered it when templating Terraform code, at some point my IDE commented the code in this way:

#block = {
#  test = "value"
#}

This kind of comment pattern can easily sneak-in when editing code.

I'd expect nunjucks to skip the #} token if it misses the opening one, for ease of use and to avoid being surprised: I don't expect a user of the lib to understand that this is interpreted as a end-of-comment token, especially since this tends to follow harmless comment lines.

Thanks for the hard work, available for discussion of course :slightly_smiling_face:

NextThread commented 10 months ago

hey, can I work on this issue