Just 15 minutes + questions, we focus on topics about using and developing nf-core
pipelines. These are recorded and made available at https://nf-co.re
, helping to build an archive of training material. Got an idea for a talk? Let us know on the #bytesize
Slack channel!
This week, Matthias Hörtenhuber (@mashehu) is going to explain the use the newly added pre-commit tool added to nf-core/tools. Pre-commits were developed to inspect the snapshot that is about to be committed and helps to check formatting etc. before adding the code to the repository.
Video transcription
The content has been edited to make it reader-friendly
git commit
. It runs prettier, checks if the code is fine and makes changes on it.
How do you set pre-commit up? Well, the good news is it comes already pre-installed with the nf-core tools as a dependency. Then you need to have a pre-commit-config.yaml
file. For example, this one here for prettier. Also, more good news, in the next tools release, this will be part of every pipeline template. Also in the modules repository, we have that set up. It doesn’t change anything for you that we have it there. But if you then also run in your repository pre-commit install
, it actually sets up this git hook. Whenever you hit commit, prettier is run beforehand and doesn’t allow you to commit until you fixed these changes.
How does it look like? I made here a short example where I just added a line in the README file. As you can see, it’s just below a heading, so Prettier will not like it. I run commit. Actually, prettier was run and fixed it. But that’s the important thing, it didn’t commit it yet. It’s changed but not added. I actually need to run commit a second time. That’s something I sometimes forget. You always need to run git commit twice if there is something wrong. If nothing is wrong, your prettier passes, then the commit runs through. This is prettier, which we use for markdown files and similar files.
One of the nice things with pre-commit is that you can use every code linting tool you want. Also, it doesn’t matter if it’s in a different language. Like you see with prettier, which is actually an NPM tool. We don’t need to have node installed to run this version of prettier with pre-commit. It just comes through the mirror there. But other tools like Python-based tools come directly from the tools itself. If we set up this config, like we have for the tools repository, for example, it automatically checks the Python files with Black and isort. In this example, I added again to the README file, but I also switched the import statements in our main.py. If I then run commit, it not only runs prettier, but it also runs Black and isort. Black was satisfied, isort found that there was an error there and fixed it fast. Prettier fixed it fast as well within the README. The only changes then were in the README, because we already had it nicely sorted before. Hit the second commit. Now the code is nice again. That was pretty much it.
Just a quick shout out to the person who actually brought this tool to us, which was Fabian. It was more than an idea how to always have prettier available in tools without requiring people to install node. He found this tool. Actually it’s very nice with having it also run with pipelines and everything. Praise Fabian. With that, I’m open for any questions if there are.
(host) Thank you very much. If you have any questions, then you can either write them in the chat, or you can just ask them straight away. Everyone should be now able to unmute themselves. Do we have any questions? It doesn’t seem so. Maxime. Maxime says hello or has a question?
(question) Yes, I have a question. You said that it’s in tools. Is it already released, or is it in the coming release.
(answer) In tools, it’s already in the release, because actually we run Prettier with pre-commit whenever we dump YAML files or JSON files in tools. For example, when we create the tests for modules, these files are now prettified with Prettier, because before we had the problem that our function actually dumped code, Prettier didn’t like. Now we run pre-commit to run Prettier on it. Also the repository itslef has Black and isort. There, it’s already in. But for the pipelines, the next release will have it in a template. Then all the pipelines also get it. You just need to run pre-commit install
to activate it. Modules since yesterday got the got the config in. If you now are in module. Pull. Then run pre-commit install
, all your modules will automatically run Prettier. Or all changes on modules will automatically be run through Prettier before you can commit. I recommend to do that. If you write modules or subworkflows.
(speaker) Bye, everybody.