11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.3k stars 496 forks source link

Liquid/Nunjucks: Line numbers in error messages are incorrect #3076

Open rubenwardy opened 1 year ago

rubenwardy commented 1 year ago

Operating system

Linux

Eleventy

2.0.1

Describe the bug

Stacktraces from errors in Markdown files give an incorrect line number. It appears that the length of the front matter is not taken into account.

Reproduction steps

  1. Either use eleventy_repro.zip, or:

    1. npm init
    2. npm install --save @11ty/eleventy
    3. Create file.md with the following content:

      ---
      title: Title of the page
      description: this is a description
      ---
      
      {% doesnotexist %}
  2. Run npx @11ty/eleventy

  3. See the following error message mentions "line:2" but the error is on line 6.

    [11ty] Problem writing Eleventy templates: (more in DEBUG output)
    [11ty] 1. Having trouble rendering liquid template ./file.md (via TemplateContentRenderError)
    [11ty] 2. tag "doesnotexist" not found, file:./file.md, line:2, col:1 (via ParseError)
    [11ty] 3. tag "doesnotexist" not found (via AssertionError)
    [11ty] 
    [11ty] Original error stack trace: AssertionError: tag "doesnotexist" not found
    [11ty]     at assert (/home/ruben/dev/tmp/eleventy_repro/node_modules/liquidjs/dist/liquid.node.cjs.js:319:15)
    [11ty]     at Parser.parseToken (/home/ruben/dev/tmp/eleventy_repro/node_modules/liquidjs/dist/liquid.node.cjs.js:2284:17)
    [11ty]     at Parser.parseTokens (/home/ruben/dev/tmp/eleventy_repro/node_modules/liquidjs/dist/liquid.node.cjs.js:2276:33)
    [11ty]     at Parser.parse (/home/ruben/dev/tmp/eleventy_repro/node_modules/liquidjs/dist/liquid.node.cjs.js:2270:21)
    [11ty]     at Liquid.parse (/home/ruben/dev/tmp/eleventy_repro/node_modules/liquidjs/dist/liquid.node.cjs.js:3618:28)
    [11ty]     at Liquid.compile (/home/ruben/dev/tmp/eleventy_repro/node_modules/@11ty/eleventy/src/Engines/Liquid.js:255:28)
    [11ty]     at Markdown.compile (/home/ruben/dev/tmp/eleventy_repro/node_modules/@11ty/eleventy/src/Engines/Markdown.js:68:28)
    [11ty]     at TemplateRender.getCompiledTemplate (/home/ruben/dev/tmp/eleventy_repro/node_modules/@11ty/eleventy/src/TemplateRender.js:269:26)
    [11ty]     at Template.compile (/home/ruben/dev/tmp/eleventy_repro/node_modules/@11ty/eleventy/src/TemplateContent.js:362:42)
    [11ty]     at async Template._render (/home/ruben/dev/tmp/eleventy_repro/node_modules/@11ty/eleventy/src/TemplateContent.js:486:16)
    [11ty] Wrote 0 files in 0.06 seconds (v2.0.1)
  4. Rename file.md to file.liquid or file.html. See same error occurs

Expected behavior

The error message should say the error is on line 6 of the file. Ideally, this should be given as a single path so it is clickable like: /home/ruben/dev/tmp/eleventy_repro/file.md:6

Reproduction URL

No response

Screenshots

No response

rubenwardy commented 1 year ago

I understand that the template parser probably only receives the content and not the frontmatter, but it should have an offset passed in so that the line numbers match up accurately. Either that, or Eleventy could catch the errors. As it is, the line number is pretty unhelpful and was confusing initially

I started using Eleventy this week and this issue is one of the main pain points I had