Skip to content

Commit

Permalink
chore: leverage cargo's workspace inheritance
Browse files Browse the repository at this point in the history
Previously, we would specify the version and path of our workspace dependencies in each of our crates. This is error prone as libp2p#3658 (comment) for example shows. Problems like these happened in the past too.

There is no need for us to ever depend on a earlier version than the most current one in our crates. It thus makes sense that we manage this version in a single place.

Cargo supports a feature called "workspace inheritance" which allows us to share a dependency declaration across a workspace and inherit it with `{ workspace = true }`.

We do this for all our workspace dependencies and for the MSRV.

Resolves libp2p#3787.

Pull-Request: libp2p#3715.
  • Loading branch information
thomaseizinger authored May 2, 2023
1 parent d1fadc5 commit 996b5c8
Show file tree
Hide file tree
Showing 89 changed files with 726 additions and 432 deletions.
42 changes: 4 additions & 38 deletions .github/workflows/cache-factory.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This workflow _produces_ caches which are used to speed up pull request builds.
# The caches are split by Rust version (stable vs MSRV per crate) because those caches cannot share any artifacts.
# This workflow _produces_ a cache that is used to speed up pull request builds.
#
# Our CI runs a job per crate, meaning all jobs share compilation artifacts but never the full cache.
# Thus, we make a single cache here that is used by all jobs and the jobs only read from this cache.

name: Cache factory

Expand All @@ -13,42 +15,6 @@ concurrency:
cancel-in-progress: true

jobs:
gather_msrv_versions:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.find-rust-versions.outputs.versions }}
steps:
- uses: actions/checkout@v3

- id: find-rust-versions
run: |
RUST_VERSIONS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(.rust_version) | unique | del(..|nulls)')
echo "versions=${RUST_VERSIONS}" >> $GITHUB_OUTPUT
make_msrv_cache:
runs-on: ubuntu-latest
needs: gather_msrv_versions
strategy:
fail-fast: false
matrix:
rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }}
steps:
- uses: actions/checkout@v3

- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}

- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
with:
shared-key: msrv-cache

- name: Compile all crates which have MSRV ${{ matrix.rust }}
run: |
cargo metadata --format-version=1 --no-deps | \
jq -r '.packages[] | select(.rust_version == "${{ matrix.rust }}") | "+\(.rust_version) build --all-features --package \(.name)"' |
xargs --verbose -L 1 cargo
make_stable_rust_cache:
runs-on: ubuntu-latest
steps:
Expand Down
61 changes: 39 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,10 @@ jobs:
- name: Install Protoc
run: sudo apt-get install -y protobuf-compiler

- uses: dtolnay/rust-toolchain@stable

- uses: actions/checkout@v3

- name: Get MSRV for ${{ matrix.crate }}
id: parse-msrv
run: |
RUST_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "${{ matrix.crate }}") | .rust_version')
echo "version=${RUST_VERSION}" >> $GITHUB_OUTPUT
shell: bash

- name: Install Rust ${{ steps.parse-msrv.outputs.version }} for MSRV check
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ steps.parse-msrv.outputs.version }}

- uses: r7kamura/rust-problem-matchers@d58b70c4a13c4866d96436315da451d8106f8f08 #v1.3.0

- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
with:
shared-key: msrv-cache
save-if: false

- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
run: cargo +${{ steps.parse-msrv.outputs.version }} build --package ${{ matrix.crate }} --all-features

- uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
Expand Down Expand Up @@ -90,6 +68,21 @@ jobs:
cargo metadata --format-version=1 --no-deps | \
jq -e -r '.packages[] | select(.name == "${{ matrix.crate }}") | .dependencies | all(.name != "libp2p")'
- uses: taiki-e/cache-cargo-install-action@7dd0cff2732612ac642812bcec4ada5a279239ed # v1
with:
tool: tomlq

- name: Enforce version in `workspace.dependencies` matches latest version
if: matrix.crate != 'libp2p'
run: |
PACKAGE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "${{ matrix.crate }}") | .version')
SPECIFIED_VERSION=$(tomlq 'workspace.dependencies.${{ matrix.crate }}.version' --file ./Cargo.toml)
echo "Package version: $PACKAGE_VERSION";
echo "Specified version: $SPECIFIED_VERSION";
test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION"
cross:
name: Compile on ${{ matrix.target }}
strategy:
Expand Down Expand Up @@ -122,6 +115,30 @@ jobs:

- run: cargo check --package libp2p --all-features --target=${{ matrix.target }}

msrv:
name: Compile with MSRV
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Extract MSRV from workspace manifest
shell: bash
run: |
MSRV=$(grep -oP 'rust-version\s*=\s*"\K[^"]+' Cargo.toml)
echo "MSRV=$MSRV" >> $GITHUB_ENV
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.MSRV }}

- uses: r7kamura/rust-problem-matchers@d58b70c4a13c4866d96436315da451d8106f8f08 #v1.3.0

- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
with:
save-if: ${{ github.ref == 'refs/heads/master' }}

- run: cargo +$MSRV check --workspace --all-features

feature_matrix: # Test various feature combinations work correctly
name: Compile with select features (${{ matrix.features }})
runs-on: ubuntu-latest
Expand Down
Loading

0 comments on commit 996b5c8

Please sign in to comment.