2

Rust CI Tooling: Clippy, commitlint, pre-commit and More | Rodney Lab

 2 years ago
source link: https://rodneylab.com/rust-ci-tooling/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
NEXT POST >
LATEST POST >>

Rust CI Tooling: Clippy, commitlint, pre‑commit and More #

Updated 11 minutes ago
4 minute read Gunning Fog Index: 6.8
Content by Rodney
SHARE:

🔨 Rust CI Tooling #

In this Rust CI tooling post, we see how you can use tools to keep your Rust repo polished using conventional commits together with Rust lint tools. Conventional commits are fantastic if you want automatically to generate SemVer versioning for your project. It can also assist in creating changelogs. In the video we see how you can use the commitlint CLI tool to help you get started with conventional commits. More Rust centric, we also see how automatically to run Rust formatting and linting scripts on each commit. This can be great for keeping your code consistent when working in a team. Equally, if you like your code just so, these tools will have your back. Finally we see how you can even enforce a custom commit message style, useful for example if you are working on a monorepo.

If that’s what you wanted to know then hit play on the video! After that, don’t forget to check links below. You will also find the commands mentioned and handy code snippets further down. Drop a comment below or reach for a chat on Element  as well as Twitter @mention  if you have suggestions for improvements or questions.

📹 Rust CI Tooling: Video #

Rust CI Tooling

🗳 Poll #

Are you a conventional commits fan?
  • No, I prefer to be more creative,
  • Yes, try to use them where I can.
Voting reveals latest results.

🖥 Rust CI Tooling: Code #

CLI Commands #

Installation #

  • Clippy as well as rustfmt install
rustup component add clippy rustfmt
  • Install commitizen together with pre-commit with Homebrew
brew install commitizen pre-commit
  • Install commitizen together with pre-commit with Python pip
pip install Commitizen pre-commit

Tool commands #

  • Check Rust code for errors (comes with RustUp no need to install manually)
cargo check
  • Format Rust code
cargo fmt
  • Lint Rust code
cargo clippy
  • commitlint
cz init # initialise new project
cz c # commit
cz c --retry # retry failed commit with same parameters
cz bump # bump SemVer version
cz changelog # generate CHANGELOG.md
  • pre-commit
pre-commit install # install pre-commit in new project
pre-commit run --all-files # one-off check all files

pre‑commit Config #

.pre-commit-config.yaml
1 repos:
2 - hooks:
3 - id: commitizen
4 stages:
5 - commit-msg
6 repo: https://github.com/commitizen-tools/commitizen
7 rev: v2.24.0
8 - hooks:
9 - id: fmt
10 - id: cargo-check
11 - id: clippy
12 repo: https://github.com/doublify/pre-commit-rust
13 rev: v1.0
14 - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
15 rev: v8.0.0
16 hooks:
17 - id: commitlint
18 stages: [commit-msg]
19 additional_dependencies: ["@commitlint/config-conventional"]

Custom commitlint Config Example #

commitlint.config.cjs
javascript
1 module.exports = {
2 extends: ["@commitlint/config-conventional"],
3 rules: {
4 "scope-empty": [2, "never"],

🔗 Rust CI Tooling: Links #

🏁 Rust CI Tooling: Summary #

What Rust formatting and linting tools are there? #

The Rust team ships cargo-check with Rustup. This checks your Rust code for errors wihtout fully building it. In fact it should be quicker than building the code, when you just want a quick check you have not messed something up. As well as cargo-check there is rustfmt as well as Clippy. While rustfmt just enforces the Rust standard for code formatting, Clippy goes a step futher. It can help you write better Rust. It goes beyond cargo-check which just checks for errors. Clippy will offer tips. For example, it might suggest a map is a clearer way of writing a particular block. As well as make suggestions it provides links so you can open web pages to get a better idea of why it thinks the suggestion might improve code

Can you integrate cargo-check, rustfmt and Clippy into your Rust CI process? #

Yes, it is not too much hassle to add cargo-check and rustfmt together with Clippy into your continuous integration process. Using git hooks you can check your code is clean for all three tools on each commit. In fact, we have seen the pre-commit tool can be super helpful here. Adding pre-commit with a hook for the tools will help keep your repo polished whether working in a team or on your own pet project.

How can you enforce customised conventional commits in your project? #

commitlint is a very helpful tool for enforcing a commit comment style. Conventional commits offer a standard for git commit messages which add consistency over projects. As well as being useful to humans, they help with automated continuous integration tasks like generating change logs and SemVer versioning. You can set conventional commits as a base standard then, augment the rules to suite your project. For example, if you have a monorepo and want to make sure a scope is included in each commit, you can add a rule for this. You can even go further and specify a list of allowed scopes. This might catch typos, keeping your commits message just how you intended them to be!

🙏🏽 Feedback #

Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as SvelteKit. Also subscribe to the newsletter to keep up-to-date with our latest projects.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK