djLint icon indicating copy to clipboard operation
djLint copied to clipboard

[BUG] [Linter]extend_excluded being ignored when invoked via pre-commit

Open StevenMapes opened this issue 2 years ago • 5 comments

System Info

  • OS: Ubuntu 20.04 and Ubuntu 22.04
  • Python Version 3.11.1
  • djLint Version 1.28.0
  • template language: e.g. django

Issue

I have a large project where some HTML filed produced by sphinx need to be added to the exclude list. They all exist under the path ./docs/_build/html/*.html. If I try setting either exclude or extend_exclude within pyproject.toml or .djlintrc the values are ignored. when pre-commit runs the linting. Any entry within ignore is included though.

If I install djlint into the venv and run it on the CLI then it works.

The only way I can get pre-commit to work is to setup the .pre-commit-config.yaml to force it to run against the single templates folder I have for that porject:

- repo: https://github.com/Riverside-Healthcare/djLint
  rev: v1.28.0
  hooks:
    - id: djlint-django
      files: "templates"

However if I had per app templates this would not work.

How To Reproduce

See this test project - https://github.com/StevenMapes/djlinktest/blob/master/mysite/pyproject.toml

  1. Clone it
  2. install these two dependencies pre-commit==3.3.2, Django==4.2.1
  3. setup pre-commit pre-commit install
  4. Edit the file docs/_build/html/tracking.html and add the following HTML anywhere within the page <span></span>
  5. Try to commit the file

djlint will still run and will fail even though the path is set to be excluded within the pyproject.toml

[tool.djlint]
profile="django"
ignore = "H030,H031,T003,H020,H023,H021,H006"
extend_exclude="docs/_build/html/"

I've also tested using the following

[tool.djlint]
profile="django"
ignore = "H030,H031,T003,H020,H023,H021,H006"
extend_exclude="docs,docs/,docs/_build/html/,docs/_build/html/*"

If you then want it to pass edit the file .pre-commit-config.yaml and enable the last line. Then try to commit again and you'll find the folder is ignored

StevenMapes avatar May 22 '23 16:05 StevenMapes

Thanks for opening your first issue here!

welcome[bot] avatar May 22 '23 16:05 welcome[bot]

(Cross-post from https://github.com/Riverside-Healthcare/djLint/issues/558#issuecomment-1574570447)

I'm also seeing an issue where I can't manage to exclude a .temp and node_modules folder simultaneously. I was able to hack around this by passing both exclude and extend_exclude, but this doesn't generalize to more than 2 folders.

[tool.djlint]
exclude = "\\.temp"
extend_exclude = "node_modules"

lgarron avatar Jun 03 '23 02:06 lgarron

Update. I've managed to get it working but by using the exclude option within the .pre-commit-config.yaml file rather than the prproject.toml

My yaml file now reads as:

- repo: https://github.com/Riverside-Healthcare/djLint
  rev: v1.28.0
  hooks:
    - id: djlint-django
      files: "templates"
      exclude: r'docs/_build/html/.*'

And the toml file reads as

[tool.djlint]
profile="django"
ignore = "H030,H031,T003,H020,H023,H021,H006"

If I want to exclude multiple paths then you can use the or within reg ex e.g@

- repo: https://github.com/Riverside-Healthcare/djLint
  rev: v1.28.0
  hooks:
    - id: djlint-django
      files: "templates"
      exclude: r'docs/_build/html/.*|template/exclude/.*'

StevenMapes avatar Jun 08 '23 11:06 StevenMapes

Thanks for the comments here, If you don't mind, I will reopen this to reminder to look closer at how pre-commit is passing in the files. I'm wondering if this is the same issue (https://stackoverflow.com/questions/61032281/exclude-some-files-on-running-black-using-pre-commit) that the files are pass in through cli, and therefore the exclude is ignored.

christopherpickering avatar Jun 08 '23 13:06 christopherpickering

I am seeing a similar bug with exclude and maybe this will provide some insight. When I pass in ***/**/*.html the file paths are resolved to a list of file paths by click. In this function djlint handles each file path in the for loop, hitting continue each time and bypassing the exclude regex check. However when I pass in . instead, djlint will handle it in a separate part of the function since it's not a file, and the paths are filtered against the exclude regex. Is it possible that the pre-commit code is passing in something like **/*.html instead of a directory path?

GeorgeSchneelochVA avatar Oct 04 '23 21:10 GeorgeSchneelochVA