Auto code formatting in CI pipeline

Hi
Wanted opinion about what would be the best practice to enforce auto formatting in CI pipeline

I was thinking that I could run mix format and commit it repo before merge

One simple way is to run mix format --check-formatted as a check on the CI pipeline, so that it would fail if the code is not properly formatted. Here you can see an example with GitHub Action on my CubDB project. The action is run for every pull request as part of the build.

I often include the same check as a pre-push Git hook, so I get informed of format issues even before I push my code.

Note that this won’t fix the format issues, but rather just inform about them and cause the pipeline to fail. It is also possible to auto-fix the format and push a new commit, but I normally prefer not to, as it opens up possible issues with concurrent pipelines.

3 Likes

I do the checks in a pre-commit hook

#!/bin/sh

mix format --check-formatted
if [ $? -ne 0 ]; then exit 1; fi

mix credo --strict > /dev/null
if [ $? -ne 0 ]; then mix credo --strict; exit 1; fi

mix dialyzer > /dev/null
if [ $? -ne 0 ]; then mix dialyzer; exit 1; fi

mix test --warnings-as-errors > /dev/null
if [ $? -ne 0 ]; then mix test --warnings-as-errors; exit 1; fi
3 Likes

I don’t want CI to “rewrite” my code, but I’m all for a mix format --check-formatted step in CI :slight_smile:

2 Likes