11ty / eleventy

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

renderTemplate fails with simple example (global versus local install) #2100

Open cfjedimaster opened 3 years ago

cfjedimaster commented 3 years ago

Describe the bug I'm trying this simple example of the renderTemplate feature (using beta8):

{% assign name = "Ray" %}
Hello World, and {{ name }}

<p>

{% renderTemplate "md" %}
# I am a title

* I am a list
* I am a list
{% endrenderTemplate %}

And when I run eleventy, I get:

ray@Hoth:~/projects/testingzone/eleventybeta$ eleventy
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] > Having trouble rendering liquid template ./index.liquid

`TemplateContentRenderError` was thrown
[11ty] > Cannot read property 'includes' of undefined, file:./index.liquid, line:8, col:1

`RenderError` was thrown
[11ty] > Cannot read property 'includes' of undefined

`TypeError` was thrown:
[11ty]     TypeError: Cannot read property 'includes' of undefined
        at new TemplateRender (/home/ray/projects/testingzone/eleventybeta/node_modules/@11ty/eleventy/src/TemplateRender.js:31:23)
        at Object.render (/home/ray/projects/testingzone/eleventybeta/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:31:12)
        at Object.renderStringShortcodeFn (/home/ray/projects/testingzone/eleventybeta/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:224:27)
        at Object.render (/home/ray/projects/testingzone/eleventybeta/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:116:40)
        at async Template._render (/home/ray/.nvm/versions/node/v14.18.0/lib/node_modules/@11ty/eleventy/src/TemplateContent.js:315:22)
        at async Template.getTemplateMapContent (/home/ray/.nvm/versions/node/v14.18.0/lib/node_modules/@11ty/eleventy/src/Template.js:1003:19)
        at async TemplateMap.populateContentDataInMap (/home/ray/.nvm/versions/node/v14.18.0/lib/node_modules/@11ty/eleventy/src/TemplateMap.js:459:39)
        at async TemplateMap.cache (/home/ray/.nvm/versions/node/v14.18.0/lib/node_modules/@11ty/eleventy/src/TemplateMap.js:360:5)
        at async TemplateWriter._createTemplateMap (/home/ray/.nvm/versions/node/v14.18.0/lib/node_modules/@11ty/eleventy/src/TemplateWriter.js:242:5)
        at async TemplateWriter.generateTemplates (/home/ray/.nvm/versions/node/v14.18.0/lib/node_modules/@11ty/eleventy/src/TemplateWriter.js:275:5)
[11ty] Wrote 0 files in 0.03 seconds (v1.0.0-beta.8)

To Reproduce See above. :)

Expected behavior I'd expect the block to render from Markdown.

Environment:

cfjedimaster commented 3 years ago

So this is interesting. I have beta8 installed globally. I also have it installed in my test folder. If I run eleventy, I get the error above. If I specifically use the local copy with npx eleventy, it runs fine. Both output the exact same version.

zachleat commented 3 years ago

image

image

Hmmmmmmmmm.

Is that the full contents of the file?

zachleat commented 3 years ago

We do have an automated test suite for these plugins that does run on Windows too https://github.com/11ty/eleventy/actions/runs/1472651558

cfjedimaster commented 3 years ago

Can you try where Eleventy is not in node_modules but installed globally?

zachleat commented 3 years ago

Had to get my $NODE_PATH going again for global node_modules but I got the same successful result 😭

zachleat commented 3 years ago

Sorry to do this but can you make a reduced test case repo that shows this locally for you?

I don’t think I’m seeing all the variables things at play here

cfjedimaster commented 3 years ago

Not a problem - will do so but may take a day.

peerreynders commented 3 years ago

Reduced test case repo

Here there are no errors - instead the template render simply stops.

Upon npm run build the template src/index.njk

{% extends "base.njk" %}

{% set content %}
<section id="book-title">
{% renderTemplate "md" %}
# Title of Book

by Author

{% endrenderTemplate %}
</section>
{% endset %}

results in the following markup dist/index.html:

<!DOCTYPE html>
<html lang="en">
  <body>
    <div id="page">

<section id="book-title">
    </div>
  </body>
</html>

i.e the template fails to render past the point of the renderTemplate "md".

Platform:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

$ node -v
v16.13.0

$ npm -v
8.1.0
$ 

FYI: https://github.com/11ty/eleventy/blob/master/src/TemplateRender.js#L29

    console.log('\ninputDir', inputDir);
    this.inputDir = inputDir ? inputDir : this.config.dir.input;
    console.log('this.inputDir', this.inputDir);

    this.includesDir = TemplatePath.join(
      this.inputDir,
      this.config.dir.includes
    );
    console.log('includesDir:', this.includesDir);

results in

$ npm run build

> 11ty-rendertemplate@1.0.0 build
> npx @11ty/eleventy

inputDir src
this.inputDir src
includesDir: src/_includes

inputDir src/_includes
this.inputDir src/_includes
includesDir: src/_includes/_includes
[11ty] Writing dist/index.html from ./src/index.njk
[11ty] Wrote 1 file in 0.08 seconds (v1.0.0-beta.8)

So that second TemplateRender instance is provided with the includes path as the input path.

cfjedimaster commented 3 years ago

So to be clear, you see an error (template not rendering), but not the same issue in console as me? Also, do you see a difference between global eleventy versus local?

peerreynders commented 3 years ago

Given I can get it to fail locally I never explored a global installation.

However I used your error message to zero in on the faulty includesDir setting of that second TemplateRender instance - which likely is responsible for the failure.

Too be clear I'm not familiar with the code that creates TemplateRender - the first question that comes to my mind is why are two instances created when there is only one renderTemplate render.

The difference between "build error" and "no build error" may simply be a manifestation of "liquid" vs "nunjucks" responding to the same root cause.

zachleat commented 3 years ago

Awesome, thank you for a test case—lookin’

zachleat commented 3 years ago

Couple of things came from this issue. We were absolutely passing in the wrong directory there to Template Render. That fix will go up with Beta.9

The other thing happening here is a Nunjucks specific issue with nested async shortcodes. Work in progress is happening here for that https://github.com/11ty/eleventy/pull/1749

cfjedimaster commented 3 years ago

This still doesn't sound exactly like my issue, but I'm going to wait and test again in 9. :)

peerreynders commented 3 years ago

Looks like I'll be using the following for a while

{# src/index.njk #}
{% extends "base.njk" %}

{% set content %}
<section id="book-title">
{% renderTemplateMd %}
# Title of Book

by Author

{% endrenderTemplateMd %}
</section>
{% endset %}
// .eleventy.js
//
const markdownIt = require('markdown-it');

const md = markdownIt({
  html: true,
});

function renderMd(content, inline) {
  return inline ? md.renderInline(content) : md.render(content);
}

const dir = {
  input: 'src',
  includes: '_includes',
  output: 'dist',
};

module.exports = function(config) {
  config.addPairedShortcode("renderTemplateMd", renderMd);

  return {
    dir
  };
};

resulting in

<!DOCTYPE html>
<html lang="en">
  <body>
    <div id="page">

<section id="book-title">
<h1>Title of Book</h1>
<p>by Author</p>

</section>
    </div>
  </body>
</html>
zachleat commented 3 years ago

For the record @peerreynders I believe your issue is separate @cfjedimaster’s original issue. I filed yours at #2108. Specifically note this comment: https://github.com/11ty/eleventy/issues/2108#issuecomment-974714754

zachleat commented 3 years ago

My suspicion here @cfjedimaster is that this might be mixing and matching a global with a local installation?

Like, maybe you’re executing a global Eleventy but it’s requiring from a local Eleventy. And the versions are different?

What’s your NODE_PATH? Are there local installations of Eleventy at play?

cfjedimaster commented 2 years ago

Both the local Eleventy and global are the same version version. NODE_PATH is not defined.

tfrancart commented 9 months ago

FYI, I stumbled on the same issue as originally reported when using eleventy Github action while my local build works. I use v2.0.1 locally. The Github action is doing a global install of eleventy, but then also runs an npm install, which will install a local eleventy. I use Nunjucks.