11ty / eleventy

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

If folder named _data/content exists 11ty 2 fails to build #2824

Closed Pezmc closed 1 year ago

Pezmc commented 1 year ago

Operating system

macOS Ventura 13.2.1

Eleventy

2.0.0

Describe the bug

If any data file exists under the directory name content e.g. _data/content/my-file.json 11ty fails to build, full error below. A minimum reproduction to show the error can be found at: https://github.com/Pezmc/11ty-data-content-issue

My understanding from debugging so far is that the TemplateLayout parsing tries to set data.content but that is declared with only a getter (i.e. read only). It seems likely that the _data dir having a directory named content, which is exposed on the page object, is overlapping with the setting of the page content.

This was not an issue with 11ty <2.0

Error stack trace:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing to "_site/some-url" from "./index.md" (via EleventyTemplateError)
[11ty] 2. Cannot assign to read only property 'content' of object '#<Object>' (via TypeError)
[11ty]
[11ty] Original error stack trace: TypeError: Cannot assign to read only property 'content' of object '#<Object>'
[11ty]     at Function.augmentDataWithContent (~/Git/11ty/node_modules/@11ty/eleventy/src/TemplateLayout.js:222:22)
[11ty]     at TemplateLayout.render (~/Git/11ty/node_modules/@11ty/eleventy/src/TemplateLayout.js:243:29)
[11ty]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[11ty]     at async Template.renderPageEntry (~/Git/11ty/node_modules/@11ty/eleventy/src/Template.js:793:17)
[11ty]     at async ~/Git/11ty/node_modules/@11ty/eleventy/src/Template.js:822:21
[11ty]     at async Promise.all (index 0)
[11ty]     at async Promise.all (index 1)
[11ty]     at async Eleventy.executeBuild (~/Git/11ty/node_modules/@11ty/eleventy/src/Eleventy.js:1160:13)
[11ty] Wrote 0 files in 0.04 seconds (v2.0.0)

Reproduction steps

  1. Have a 2.0.0 project
  2. Create a json file under _data/content/any-name.json
  3. Have a markdown file that uses manually created pagination
  4. Run npx @11ty/eleventy

Expected behavior

Short term fix: Rename the content folder to side-step the issue e.g. _data/my-content

Expected behaviour from 11ty: Either throw a clear error if a folder named content (and perhaps other protected internal names) is present; or guard against this happening and allow the _data tags to overlap with the internal props on the page object.

Reproduction URL

https://github.com/Pezmc/11ty-data-content-issue

Screenshots

No response

bobmonsour commented 1 year ago

I can verify that renaming the directory to something other than "content" works fine. Leads me to assume that "content" is a reserved word of some sort in eleventy.

pdehaan commented 1 year ago

Yeah, content is a special name in the bowels of Eleventy. And just because I always forget all the properties on items in a collection, here's my handy cheat-sheet from a sample project I had locally:

// index.11tydata.js
const { inspect } = require("node:util");

module.exports = {
  eleventyComputed: {
    debug(data) {
      const p = data.collections.all.at(0);
      if (p) console.log(inspect(p, {sorted: true, depth: 0}));
      return "";
    }
  }
};

OUTPUT

{
  content: [Getter],
  data: [Object],
  date: 2022-06-14T00:00:00.000Z,
  filePathStem: '/news/2022-06-14/index',
  fileSlug: '2022-06-14',
  inputPath: './src/news/2022-06-14/index.md',
  outputPath: 'www/news/2022-06-14/index.html',
  page: [Object],
  template: [Template],
  templateContent: [Getter/Setter],
  url: '/news/2022-06-14/'
}
j-f1 commented 1 year ago

It would be nice to have Eleventy detect situations like that and give a clear error message.

zachleat commented 1 year ago

This is filed at https://github.com/11ty/eleventy/issues/1173! Please subscribe over there!

zachleat commented 7 months ago

Fixed by https://github.com/11ty/eleventy/pull/3251 Shipping with 3.0.0-alpha.7