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

Add template linting via djlint #25212

Merged
merged 17 commits into from
Jun 14, 2023
Merged

Conversation

silverwind
Copy link
Member

@silverwind silverwind commented Jun 12, 2023

So I found this linter which features a mode for go templates, so I gave it a try and it did find a number of valid issue, like unbalanced tags etc. It also has a number of bugs, I had to disable/workaround many issues.

Given that this linter is written in python, this does add a dependency on python >= 3.8 and poetry to the development environment to be able to run this linter locally.

  • e.g. prefixes on placeholders are removed because the linter had a false-positive on placeholder="e.g. cn=Search" for the attr=value syntax and it's not ideal anyways to write e.g. into a placeholder because a placeholder is meant to hold a sample value.
  • In templates/repo/settings/options.tmpl I simplified the logic to not conditionally create opening tags without closing tags because this stuff confuses the linter (and possibly the reader as well).

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 12, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 12, 2023
@silverwind silverwind added topic/code-linting and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 12, 2023
@silverwind silverwind changed the title Add template linting Add template linting via djlint Jun 12, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 12, 2023
@jolheiser
Copy link
Member

It's nice that it found some issues to remedy, but I am very hesitant to add another language ecosystem just for this.

@silverwind
Copy link
Member Author

It's nice that it found some issues to remedy, but I am very hesitant to add another language ecosystem just for this.

I guess most people won't run this locally anyways, but I thought I at least enable them to do so. They don't have to install python to get gitea dev setup running.

@delvh
Copy link
Member

delvh commented Jun 12, 2023

just for this.

Erm… Have you seen how many PRs we already needed because there was an error in the templates that no one noticed?
I am rather happy if we add a linter for that.
The good thing is also that Python is already installed on every machine, so you don't even need to install a new language on your system.
While I do agree that it would be better in a language we already support, I'll take what I can get.

@jolheiser
Copy link
Member

Erm… Have you seen how many PRs we already needed because there was an error in the templates that no one noticed?

I don't disagree that this is helpful.

The good thing is also that Python is already installed on every machine, so you don't even need to install a new language on your system.

I'm not sure how true this is. It may be mostly true on many Linux systems, but I'm not sure about others. Furthermore, even if Python is installed, this requires installing Poetry as well, but...

While I do agree that it would be better in a language we already support, I'll take what I can get.

I guess since it's not a requirement for dev I'm not exactly blocking, but it does feel a bit strange.

@silverwind
Copy link
Member Author

silverwind commented Jun 12, 2023

this requires installing Poetry as well

I chose Poetry because I think it's the most sane python dependency manager currently. pipx install djlint or pip install djlint will also work most likely but that leaves the problem that the dependency is unversioned so may yield differrent results depending on the installed version, so it's not something I really consider.

In short: The python dependency management ecosystems sucks, poetry is not ideal, but better then the other solutions.

BTW, macOS also comes default with Python installed. Currently I have 3.9 as /usr/bin/python3 on macOS Ventura.

@silverwind
Copy link
Member Author

I just saw they support a experimental npm installation, so we might actually be able to skip the poetry dependency (it does require python and pip still). I will try that tomorrow.

Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a simple "pip install" could be better if it works. Otherwise overall LGTM

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 13, 2023
@silverwind silverwind marked this pull request as draft June 13, 2023 07:53
@silverwind
Copy link
Member Author

I think a simple "pip install" could be better if it works. Otherwise overall LGTM

It would leave the dependency unversioned, which in the long run will bring issues of inconsistent behaviour between versions, it's better to version-lock all dependencies. I will try the npm method.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jun 13, 2023

I think a simple "pip install" could be better if it works. Otherwise overall LGTM

It would leave the dependency unversioned, which in the long run will bring issues of inconsistent behaviour between versions, it's better to version-lock all dependencies. I will try the npm method.

I do not think so. pip install can set the version. https://www.google.com/search?q=pip+install+with+version


Or, use requirements.txt for pip (https://www.google.com/search?q=pip+requirements.txt+version) , it works like package.json or go.mod

@silverwind
Copy link
Member Author

pip will pollute the global site-packages thought, I never use it because of that.

@wxiaoguang
Copy link
Contributor

pip will pollute the global site-packages thought, I never use it because of that.

Hmm yes, that's a problem and that's why pip is always used with virtualenv.

Anyway, I think linting the templates is good and have voted my approval 😄

@silverwind
Copy link
Member Author

silverwind commented Jun 13, 2023

The npm module they publish is just a pip wrapper that will pollute global python packages, so I won't consider it and think the current solution is fine as-is.

@silverwind
Copy link
Member Author

silverwind commented Jun 13, 2023

Decided to go with pip to install poetry on the action because apparently pipx is not provided by the setup-python action, but by GitHub's VM environment where it installed a python 3.10 venv instead of 3.11 as I requested.

So the proper solution would have been to install pip to install pipx, pipx to install poetry and finally poetry to install the dependencies, but that chain was getting to insane for my tastes and Actions is mostly an isolated environment anyways where we don't need to worry so much about pip site-packages conflicts.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jun 13, 2023
.github/workflows/files-changed.yml Outdated Show resolved Hide resolved
templates/repo/issue/choose.tmpl Show resolved Hide resolved
templates/repo/settings/options.tmpl Outdated Show resolved Hide resolved
templates/repo/settings/options.tmpl Outdated Show resolved Hide resolved
@techknowlogick techknowlogick enabled auto-merge (squash) June 14, 2023 17:29
@techknowlogick techknowlogick added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 14, 2023
@techknowlogick techknowlogick merged commit e24f651 into go-gitea:main Jun 14, 2023
@GiteaBot GiteaBot added this to the 1.21.0 milestone Jun 14, 2023
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 14, 2023
@silverwind silverwind deleted the template-lint branch June 14, 2023 18:19
silverwind added a commit that referenced this pull request Jun 14, 2023
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jun 15, 2023
* upstream/main: (31 commits)
  Show OAuth2 errors to end users (go-gitea#25261)
  [skip ci] Updated translations via Crowdin
  Fix index generation parallelly failure (go-gitea#25235)
  Fix variable in template (go-gitea#25267)
  Add template linting via djlint (go-gitea#25212)
  Fix edit OAuth application width (go-gitea#25262)
  Use flex to align SVG and text (go-gitea#25163)
  GitHub Actions enhancements for frontend (go-gitea#25150)
  Add missing `v` in migrations.go (go-gitea#25252)
  Change form actions to fetch for submit review box (go-gitea#25219)
  Fix panic when migrating a repo from GitHub with issues (go-gitea#25246)
  Fix description of drop custom_labels migration (go-gitea#25243)
  Fix all possible setting error related storages and added some tests (go-gitea#23911)
  [skip ci] Updated translations via Crowdin
  Revert overflow: overlay (revert go-gitea#21850) (go-gitea#25231)
  Support changing labels of Actions runner without re-registration (go-gitea#24806)
  Improve AJAX link and modal confirm dialog (go-gitea#25210)
  Use inline SVG for built-in OAuth providers (go-gitea#25171)
  Disable `Create column` button while the column name is empty  (go-gitea#25192)
  Fix profile render when the README.md size is larger than 1024 bytes (go-gitea#25131)
  ...
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Sep 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. topic/code-linting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants