Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Linter] djlint exclude option does not work as expected #1028

Open
3 tasks done
antoineauger opened this issue Nov 19, 2024 · 3 comments · Fixed by #1034
Open
3 tasks done

[BUG] [Linter] djlint exclude option does not work as expected #1028

antoineauger opened this issue Nov 19, 2024 · 3 comments · Fixed by #1034
Labels
🔍 linter 🦠 bug Something isn't working

Comments

@antoineauger
Copy link
Contributor

antoineauger commented Nov 19, 2024

System Info

  • OS: Debian GNU/Linux 12
  • Python Version: Python 3.12.7
  • djLint Version: djlint, version 1.36.1
  • template language: django (.html.j2 extension)

Issue

None of the excluded paths is transformed to an absolute path. Therefore, the check performed in https://github.com/djlint/djLint/blob/master/djlint/src.py#L47 only searches if some of the excluded patterns are contained in the current path (instead of being equal).

If --exclude=build is passed, all paths containing *build* will be ignored, even if the string only partially match a parent directory (for instance, /builds/template.html.j2, /build-my-template/template.html.j2, etc. will always be ignored no matter where djlint is ran).

Expected behavior

If --exclude=build is passed, the final regex for excluding paths should be ^{SRC}/build/.*$

How To Reproduce

See comment below: #1028 (comment)

Old reproduce steps

Tests performed on a CI runner:

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

$ poetry run python --version
Python 3.12.7

$ echo $0
/usr/bin/bash

$ poetry run djlint ./ --check
No files to check! 😢

$ poetry run djlint ${CI_PROJECT_DIR}/ --check
No files to check! 😢

$ poetry run djlint ./**/templates/*.html.j2 --check
Checking 6/6 files ━━━━━━━━━━ 00:00    
0 files would be updated.

Tests performed locally:

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

$ poetry run python --version
Python 3.12.7

$ echo $0
zsh

$ poetry run djlint ./ --check
Checking 6/6 files ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 00:00    
0 files would be updated.

$ poetry run djlint ./**/templates/*.html.j2 --check
Checking 6/6 files ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 00:00    
0 files would be updated.

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

[tool.djlint]
ignore = "H005,H006,H016,H017,H021,H030,H031"
extension = "html.j2"
extend_exclude = "frontend,htmlcov"
indent = 2

/cc @nejch

@antoineauger antoineauger added 🔍 linter 🦠 bug Something isn't working labels Nov 19, 2024
@nemovc
Copy link

nemovc commented Nov 22, 2024

I've had this issue and have managed to track it down to the default exclude regex causing it to reject filepaths which contain 'build'... which is a problem, when e.g., gitlab CI runs in /builds/<group>/<repo-slug>. Are you able to pwd inside the CI runner and confirm whether it matches any of the strings listed in https://github.com/djlint/djLint/blob/master/djlint/settings.py#L478

If so, this is also related to #790

@antoineauger
Copy link
Contributor Author

antoineauger commented Nov 22, 2024

@nemovc I just made some additional tests and I think you're right 😮

Given the following tree structure:

/builds/core/api
   top_level_template.html.j2
   subdir1/
      templates/
            inner_template.html.j2

I ran the following tests (both in CI and locally):

$ pwd
/builds/core/api

$ poetry run djlint --check . --exclude=build
No files to check! 😢

$ poetry run djlint --check . --exclude=builds
No files to check! 😢

$ poetry run djlint --check . --exclude=venv
Checking 2/2 files ━━━━━━━━━━ 00:00    
0 files would be updated.

$ poetry run djlint --check . --exclude=templates
Checking 1/1 files ━━━━━━━━━━ 00:00    
0 files would be updated.

$ poetry run djlint --check . --exclude=temp
Checking 1/1 files ━━━━━━━━━━ 00:00    
0 files would be updated.

I could not see the problem first since the default_exclude only contains build and GitLab works inside a builds directory, so not the same directory name.

The root cause of all of this is that none of the excluded paths is transformed to an absolute path.
Therefore, the check performed in https://github.com/djlint/djLint/blob/master/djlint/src.py#L47 only searches if some of the excluded patterns are contained in the current path (instead of being equal).

If --exclude=build is passed, the final regex for excluding paths should be ^{SRC}/build/.*$

I'll rename this issue and adjust the description. I think this is the root cause of:

@antoineauger antoineauger changed the title [BUG] [Linter] djlint does not recursively go through all the directories when SRC is passed [BUG] [Linter] djlint exclude option does not work as expected Nov 22, 2024
@antoineauger
Copy link
Contributor Author

@monosans Could you reopen this issue after the revert?

Thanks.

@monosans monosans reopened this Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔍 linter 🦠 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants