Checking Unused Dependencies in a Rust Project with Github Actions

Checking Unused Dependencies in a Rust Project with Github Actions

TL;DR

Below is the .github/workflows/udeps.yaml file I have come up with:

name: udeps

on: [push, pull_request]

jobs:
  Test:
    strategy:
      matrix:
        os: [ubuntu-latest]
        # udeps requires nightly
        rust: [nightly]

    runs-on: ${{ matrix.os }}

    steps:
      # get the code
      - uses: actions/checkout@v1
        name: "Repository Checkout"
      # set up rust environment
      - uses: actions-rs/toolchain@v1
        name: "Rust Toolchain Setup"
        with:
          profile: minimal
          toolchain: ${{ matrix.rust }}
          override: true
      # cache build to wait shorter on next builds
      - uses: actions/cache@v2
        name: "Cache Setup"
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      # install cargo-udeps
      - uses: actions-rs/cargo@v1
        name: "Udeps Installation"
        with:
          command: install
          args: cargo-udeps --locked
      # use cargo-udeps
      - uses: actions-rs/cargo@v1
        name: "Unused Dependency Check"
        with:
          command: udeps

Too Short; Gotta Read

cargo-udeps is an amazing project that helps you analyze unused dependencies on your Rust codebase. You can simply do cargo install cargo-udeps to install it. The only problem with it, however, is that it runs on nightly.

On the other hand, there's no easier way to automatize it. Currently ,there is an issue on actions-rs/meta that proposes to implement udeps, but it is open since 2019 and there's no clear indication that it will be resolved in the near future.

That does not mean there is no way, though. Create a new workflow named .github/workflows/udeps.yaml and give some initial content:

name: udeps # change to whatever you want

on: [push, pull_request] # run on push and pull requests

We need to set up jobs so that it runs on Ubuntu and Rust Nightly as below:

jobs:
  Test:
    strategy:
      matrix:
        os: [ubuntu-latest]
        rust: [nightly] # this is where we define to use rust nightly

    runs-on: ${{ matrix.os }}

And, of course, we have to define some steps

    steps:
      # check out the code
      - uses: actions/checkout@v1
        name: "Repository Checkout"
      # get the minimal requirements to compile rust
      - uses: actions-rs/toolchain@v1
        name: "Rust Toolchain Setup"
        with:
          profile: minimal
          toolchain: ${{ matrix.rust }}
          override: true

We need to install cargo-udeps in order to use it. To do that:

      - uses: actions-rs/cargo@v1
        name: "Udeps Installation"
        with:
          command: install
          args: cargo-udeps --locked

And, finally, we need to run it:

      - uses: actions-rs/cargo@v1
        name: "Unused Dependency Check"
        with:
          command: udeps
          # same as `cargo udeps`
          # we don't need `cargo +nightly udeps`
          # because we are already in nightly env

Of course, you can also add some caching configuration on it. TL;DR section on top already covers it for you.

Save, commit and push. When there's an unused dependency, this workflow will fail since cargo-udeps exits with exit-code 0.