djlint / djLint

✨ HTML Template Linter and Formatter. Django - Jinja - Nunjucks - Handlebars - GoLang
https://djLint.com
GNU General Public License v3.0
686 stars 84 forks source link

[BUG] [Linter] #826

Open athoag-sony opened 6 months ago

athoag-sony commented 6 months ago

System Info

Issue

I'm not sure if this a bug or me not understanding how djlint parses relative paths, but I can't get djlint to recognize jinja2 templates that are included via {% include 'relative_path/other_template.jinja2' %}.

How To Reproduce

I'm using djlint as a pre-commit hook. Here is my pre-commit-config.yaml:

repos:
  - repo: https://github.com/djlint/djLint
    rev: v1.34.1
    hooks:
      - id: djlint-jinja
        name: Run Jinja2 linter

Let's say we have a project structure like this:

app
├── pre-commit-config.yaml
├── templates
│   ├── base
│   │   ├── dummy_head.jinja2
│   ├── dummy.jinja2

I am linting app/templates/dummy.jinja2 whose contents are:

<!DOCTYPE html>
<html lang="en">
    {% include "base/dummy_head.jinja2" %}
</html>

And the file app/templates/base/dummy_head.jinja2 has these contents:

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <title>My GUI Project</title>
</head>

The key here is that there is a <title> tag in this included template, and I have verified that it renders in the browser. We will see that djlint does not see this <title> tag when it is contained in the included template.

From the root directory if I run my pre-commit hook like this:

poetry run pre-commit run djlint-jinja --files app/templates/dummy.jinja2

I get this set of errors back from djlint:

app/templates/dummy.jinja2
───────────────────────────────────────────────────────────────────────────────
H016 2:0 Missing title tag in html. <html lang="en"> 
H030 2:0 Consider adding a meta description. <html lang="en"> 
H031 2:0 Consider adding meta keywords. <html lang="en"> 

Linted 1 file, found 3 errors.

This is unexpected since I have a <title> tag in "base/dummy_head.jinja2", the template I included in dummy.jinja2.

However, if I simply replace the line: {% include "base/dummy_head.jinja2" %} with the contents of that file in app/templates/dummy.jinja2 I see the H016 error go away.

I have tried replacing the relative path "base/dummy_head.jinja2" with the full path from the root directory of the project, but I get the same djlint errors.

Contents of .djlintrc/pyproject.toml [tool.djlint]

Not relevant since I am running djlint as a pre-commit hook.