google / yamlfmt

An extensible command line tool or library to format yaml files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: pre-commit integration

jamesbraza opened this issue · comments

I come in peace, from the Python world 👽

If you could somehow get this to work with pre-commit, it would be sick! You could get lots of new adopters...

Welcome!
I have never tried pre-commit before, interesting tool. I don't think there's much value in me adding support for installing this repo directly; I'm hoping to keep this repo as generic as possible so don't want to buy into any specific environment. However making a hook that would run the yamlfmt binary would be pretty easy. I got it working by doing:

repos:
- repo: local
  hooks:
  - id: yamlfmt
    name: yamlfmt
    language: system
    entry: yamlfmt
    types: [yaml]

I can add instructions for that somewhere in the repo, maybe in the README.

Yes that looks like it would work, I will give it a try.

I think an addition to the README could be good, otherwise feel free to close this out! Thanks again

While the above suggested local hook works, the lack of a hook repo (i.e. one with .pre-commit-hooks.yaml) has a few disadvantages:

Depends on the system language

  • So if someone new clones a repo with this hook in it, git commit will fail if the binary is not on the path.
  • This also implies it won't work with https://pre-commit.ci.
  • The system language is really the fallback, as pre-commit can also not manage the environment and dependencies of this hook. I don't know how go works but if you install two python packages with conflicting dependencies on a user or system level, you can't use them properly (which is why there are virtual environments).

Higher barriers to use the tool with pre-commit

As you said, a local hook works, but people have to be familiar enough with the framework to use local hooks (or find this issue). Also, your tool won't be listed in https://pre-commit.com/hooks.html, which I for example used just now to find a yaml formatter (and there are not really good options).

As far as the buying into a specific environment argument goes, I can understand. But really all you do though is to put a yaml file into your package source. There is no magic and other popular formatters did so, I think it does not create any incompatibilities going forward.

Mirror repo as solution?
An alternative to support pre-commit in this repo is make your tool available through another git repo that contains just the hook. These are called mirror repos and the pre-commit project maintains many of these for popular tools like mypy and others. Not sure under which conditions they accept new mirror repos but I can investigate that for you if you are interested.

Thanks for the perspective on pre-commit. The tool has never been a good fit for me so I do not know much about how people tend to use it. I'll start by quickly addressing some of the concerns:

So if someone new clones a repo with this hook in it, git commit will fail if the binary is not on the path.

That is indeed a barrier, but it seems like it's not super uncommon with a number of pre-commit hooks. For the Python environment its understandable that this is rarely a problem since code is distributed rather than binaries, but it looks like with non-Python tools this is a pretty common pattern.

This also implies it won't work with https://pre-commit.ci/.

That's true, it probably wouldn't. I've never seen this before.

I don't know how go works but if you install two python packages with conflicting dependencies on a user or system level, you can't use them properly (which is why there are virtual environments).

This isn't a concern for the scenario I posted above. yamlfmt is distributed as a self-sufficient single binary that does not depend on anything else in the system.

As far as the buying into a specific environment argument goes, I can understand. But really all you do though is to put a yaml file into your package source.

Yeah, that's true. The main thing I dislike is that I truly wish I didn't have to clutter the root directory with it. Technically, the root directory of this repo is the actual API package, and I wish I didn't need to clutter it with stuff other than the important things like go.mod and the actual source files. Often in other languages, source code is in src, and the root directory is free to have config files dumped into it. With my setup, I really want to try and keep the root directory clean. I am trying not to be unreasonable about it, but it feels like saying yes to one thing means I could be saying yes to lots of things and lead to an unwieldy root directory.

However, it seems the pre-commit framework is quite popular and that there's demand for it. My first choice would be to get it accepted as a mirror repo; I'd happily help with whatever I needed to do to keep that up to date. I can take a look into that and see how to get that sort of thing approved. I'll report back what I can find out.
If it is not possible to get it accepted as a mirror, then I suppose we can put the hook in the root of this repo so people can use it. I'm not interested in deliberately making it harder for people to use the tool, so whether it's through the mirror repo or the yaml file in this repo, I'll try to make it happen.

Great @braydonk, thanks for the explanation. I have myself contributed the R intregration to pre-commit and maintain a hook repo with many R hooks, so let me know if I can help. The maintainer of pre-commit is very friendly and willing to help.

@jamesbraza @lorenzwalthert Thanks for your patience and great info about pre-commit. After looking into it I realized that it is far more effort than it's worth to maintain a mirror repo, and have decided just to include a .pre-commit-hooks.yaml file in this repo directly. It's available on main and will be in the next release which will be some time in the next 2 weeks.

Unfortunately I could not find a nice way to allow a pre-commit hook to work without having go installed, so unfortunately that is still a requirement of using the hook.

Going to re-open the issue for now since I've decided to withhold the docs until v0.8.0 is released. I didn't realize pre-commit warned against using mutable rev entries like main. If you would like to try it before v0.8.0 is released, you can add the following to your repos section:

- repo: https://github.com/google/yamlfmt
  rev: main
  hooks:
    - id: yamlfmt

Will close this after v0.8.0 release.

Needed to do a patch release and this was already in main so it's released in v0.7.1!

This hook is now listed in https://pre-commit.com/hooks.html!