Hacker News new | ask | show | jobs
by roma1n 2585 days ago
How do you enforce the use of a formatter in a project -- pre-commit hook? pre-receive hook? both? And is there a convention to put a specific dotfile at the project root to hint at your IDE/editor that it should use a certain tool for automatic formatting?
6 comments

I recommend the server-side `update` hook (much like `pre-receive`, but a little easier to use and more flexible).

Here's an example with Prettier:

https://coolaj86.com/articles/server-side-git-hooks-for-code...

We are using a pre-commit hook but I also have an on-save hook in my editor. Further, we also have a CI job that runs `black --check` which returns 1 (failing the build) if the files weren't completely formatted with black. In Go, the convention is that everyone's editor runs gofmt on change, and because the convention is so strong, that's usually enough (although many larger projects also have a CI job similar to the one described above).

Having used both extensively, my biggest grievances (in order) are that there aren't more editors that with good on-save support, that black is relatively slow (compared to gofmt), and that black isn't more opinionated and less configurable.

We use pre-commit.com – configure your project with dotfiles for various tools so things like editor auto-format & lint, pre-commit, etc. can pick them up, and then have a Git pre-commit hook which is also run by your CI tool for consistency.

Here’s a reference:

https://github.com/LibraryOfCongress/coding-standards

The CI check can be pretty simple — for example: https://github.com/LibraryOfCongress/concordia/blob/2f813f18...

I run this as a job in my build pipeline:

black --diff

black --check

The first one shows you what's different (so you have logs in your build job), the second one fails your job if there are any diffs. You could just do the second one if you're a fan of the whole brevity thing.

CI runs a linter and fails the build if the output is non-empty. PRs can't be merged unless the build passes on the branch.
Yeah no because this changes the code and does not merely complain
One, that shouldn't matter on CI anyway. Two, you can tell black to just print the difference with --diff and tell it to just check the files with --check.
Yeah, true, though ideally you would do it before the commit so you wouldn't have one extra lint commit per code commit
I use the format on save for black + prettier.