11ty / eleventy

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

Eleventy:EleventyErrorHandler trace isn't useful to debugging (within _this_ context) #2923

Open remy opened 1 year ago

remy commented 1 year ago

Operating system

macOS Big Sur 11.7.4

Eleventy

2.0.1

Describe the bug

I've just upgraded from 0.5 to 2.0.1 (yeah…I know…). I tried upgrading to v1 also and get the same error, so reporting against latest. I also installed the upgrade helper (both v1 and v2 for the respective versions of 11ty) but neither fail to see this error.

This is the error:

[11ty] Problem writing Eleventy templates:
[11ty] tags is not iterable (via TypeError)
  Eleventy:EleventyErrorHandler (error stack): TypeError: tags is not iterable
  Eleventy:EleventyErrorHandler     at Pagination.circularReferenceCheck (./node_modules/@11ty/eleventy/src/Plugins/Pagination.js:51:21)
  Eleventy:EleventyErrorHandler     at Pagination.setData (./node_modules/@11ty/eleventy/src/Plugins/Pagination.js:77:10)
  Eleventy:EleventyErrorHandler     at new Pagination (./node_modules/@11ty/eleventy/src/Plugins/Pagination.js:21:10)
  Eleventy:EleventyErrorHandler     at Template.getTemplates (./node_modules/@11ty/eleventy/src/Template.js:703:21)
  Eleventy:EleventyErrorHandler     at TemplateMap.initDependencyMap (./node_modules/@11ty/eleventy/src/TemplateMap.js:414:41)
  Eleventy:EleventyErrorHandler     at async TemplateMap.cache (./node_modules/@11ty/eleventy/src/TemplateMap.js:458:5)
  Eleventy:EleventyErrorHandler     at async TemplateWriter._createTemplateMap (./node_modules/@11ty/eleventy/src/TemplateWriter.js:330:5)
  Eleventy:EleventyErrorHandler     at async TemplateWriter.generateTemplates (./node_modules/@11ty/eleventy/src/TemplateWriter.js:360:5)
  Eleventy:EleventyErrorHandler     at async TemplateWriter.write (./node_modules/@11ty/eleventy/src/TemplateWriter.js:407:23)
  Eleventy:EleventyErrorHandler     at async Eleventy.executeBuild (./node_modules/@11ty/eleventy/src/Eleventy.js:1191:13) +0ms

The problem is that I don't know where this is being accessed to be able to debug the actual problem. I've seen variations of this bug over the years and it tends to throw in the same way - X is not Y + internal stacktrace but nothing pointing to the template it tried to render.

Using DEBUG=* eleventy gives a detailed result (which was used above) but doesn't actually detail what caused this trace. The previous line is a dump of all the metadata (to my pages) under Dev:Eleventy:Template and before that it's rendered the 404 page. I'm guessing there's some parallelisation going on so the order of generated messages (prior) to this one probably isn't related (or certainly doesn't make any sense if it is!).

What I'd like to see as the simplest solution would be to echo out the template that caused this failure - this would help me (and others) to debug the particular problem.

Reproduction steps

Unknown now to replicate (otherwise it would be fixable! 😄 )

Expected behavior

At least to dump out the template being rendered - if only during a fatal error.

Reproduction URL

No response

Screenshots

No response

pdehaan commented 1 year ago

@remy Do you have a public repo you can share?

"TypeError: tags is not iterable"

My guts are saying somewhere is [dynamically?] setting tags to an empty string or undefined or something else subtle that is blocking up the internals.

I'd also start by looking at whatever data is being paginated over (re: "at Pagination.circularReferenceCheck…", but that may or not be relevant if something is paginating over collections.all since it wouldn't narrow anything down).

pdehaan commented 1 year ago

Nevermind. I found your repo and can reproduce w/ "@11ty/eleventy": "^0.5.4", and the error in 2.0.1:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] tags is not iterable (via TypeError)
[11ty]
[11ty] Original error stack trace: TypeError: tags is not iterable
[11ty]     at Pagination.circularReferenceCheck (<redacted>/node_modules/@11ty/eleventy/src/Plugins/Pagination.js:51:21)
[11ty]     ...
[11ty] Copied 1 file / Wrote 0 files in 0.05 seconds (v2.0.1)

I'll take a look.

pdehaan commented 1 year ago

I think I found it.

> eleventy

[11ty] Writing dist/404.html from ./src/404.md (liquid)
[11ty] Writing dist/tag/ips/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/backlight/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/bivert/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/unmodified/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/amp/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/gba/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/prosound/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/tag/repair/index.html from ./src/tag/_tag.njk
[11ty] Writing dist/index.html from ./src/index.njk
[11ty] Benchmark     61ms  29%    10× (Configuration) "cssmin" Nunjucks Filter
[11ty] Copied 32 files / Wrote 10 files in 0.15 seconds (15.0ms each, v2.0.1)

The issue seems to be you have a src/_data/tags.json file, so I guess that is conflicting w/ the page's tags front matter processing (since tags is assumed to be an array of strings, and this is an unrelated object, which isn't iterable).

Seems to build now if I rename the file to a non-conflicting "src/_data/tags2.json" and tweaked the 2 references:

git diff | cat
diff --git a/package.json b/package.json
index 4af5af7..7848489 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
   },
   "license": "MIT",
   "devDependencies": {
-    "@11ty/eleventy": "^0.5.4",
+    "@11ty/eleventy": "^2.0.1",
     "luxon": "^1.0.0",
     "markdown-it": "^8.4.2",
     "markdown-it-anchor": "^4.0.0"
diff --git a/src/index.njk b/src/index.njk
index 569bbf8..0281010 100644
--- a/src/index.njk
+++ b/src/index.njk
@@ -3,7 +3,7 @@ section: home
 title: Retro By Rem
 layout: layouts/base-page.njk
 pagination:
-  data: tags
+  data: tags2
   resolve: values
   alias: tag
   size: 1000
diff --git a/src/tag/_tag.njk b/src/tag/_tag.njk
index 3b69b2b..d1f3f98 100644
--- a/src/tag/_tag.njk
+++ b/src/tag/_tag.njk
@@ -1,7 +1,7 @@
 ---
 section: tag
 pagination:
-  data: tags
+  data: tags2
   resolve: values
   size: 1
   alias: data

Might be a few other stray issues. I see you're reaching down into the Nunjucks ctx context, so stuff like this might also need to be tweaked:

  eleventyConfig.addFilter('getTitle', function (s) {
    const tags = this.ctx.tags;

    if (!tags[s]) {
      return 'glass lens';
    }

    return tags[s].short || tags[s].title;
  });
remy commented 1 year ago

Thanks for this. Just wanted to let you know that Gmail spam filter ate all these messages so I completely missed it.

I ended up working around by downgrading. It was more than the error itself was impossible (to me) to trace back to the origin (and thank you for the patch 👍)