Skip to content

Commit 996b5c8

Browse files
chore: leverage cargo's workspace inheritance
Previously, we would specify the version and path of our workspace dependencies in each of our crates. This is error prone as #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 #3787. Pull-Request: #3715.
1 parent d1fadc5 commit 996b5c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+726
-432
lines changed

.github/workflows/cache-factory.yml

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# This workflow _produces_ caches which are used to speed up pull request builds.
2-
# The caches are split by Rust version (stable vs MSRV per crate) because those caches cannot share any artifacts.
1+
# This workflow _produces_ a cache that is used to speed up pull request builds.
2+
#
3+
# Our CI runs a job per crate, meaning all jobs share compilation artifacts but never the full cache.
4+
# Thus, we make a single cache here that is used by all jobs and the jobs only read from this cache.
35

46
name: Cache factory
57

@@ -13,42 +15,6 @@ concurrency:
1315
cancel-in-progress: true
1416

1517
jobs:
16-
gather_msrv_versions:
17-
runs-on: ubuntu-latest
18-
outputs:
19-
versions: ${{ steps.find-rust-versions.outputs.versions }}
20-
steps:
21-
- uses: actions/checkout@v3
22-
23-
- id: find-rust-versions
24-
run: |
25-
RUST_VERSIONS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(.rust_version) | unique | del(..|nulls)')
26-
echo "versions=${RUST_VERSIONS}" >> $GITHUB_OUTPUT
27-
28-
make_msrv_cache:
29-
runs-on: ubuntu-latest
30-
needs: gather_msrv_versions
31-
strategy:
32-
fail-fast: false
33-
matrix:
34-
rust: ${{ fromJSON(needs.gather_msrv_versions.outputs.versions) }}
35-
steps:
36-
- uses: actions/checkout@v3
37-
38-
- uses: dtolnay/rust-toolchain@master
39-
with:
40-
toolchain: ${{ matrix.rust }}
41-
42-
- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
43-
with:
44-
shared-key: msrv-cache
45-
46-
- name: Compile all crates which have MSRV ${{ matrix.rust }}
47-
run: |
48-
cargo metadata --format-version=1 --no-deps | \
49-
jq -r '.packages[] | select(.rust_version == "${{ matrix.rust }}") | "+\(.rust_version) build --all-features --package \(.name)"' |
50-
xargs --verbose -L 1 cargo
51-
5218
make_stable_rust_cache:
5319
runs-on: ubuntu-latest
5420
steps:

.github/workflows/ci.yml

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,10 @@ jobs:
3232
- name: Install Protoc
3333
run: sudo apt-get install -y protobuf-compiler
3434

35-
- uses: dtolnay/rust-toolchain@stable
36-
3735
- uses: actions/checkout@v3
3836

39-
- name: Get MSRV for ${{ matrix.crate }}
40-
id: parse-msrv
41-
run: |
42-
RUST_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "${{ matrix.crate }}") | .rust_version')
43-
echo "version=${RUST_VERSION}" >> $GITHUB_OUTPUT
44-
shell: bash
45-
46-
- name: Install Rust ${{ steps.parse-msrv.outputs.version }} for MSRV check
47-
uses: dtolnay/rust-toolchain@master
48-
with:
49-
toolchain: ${{ steps.parse-msrv.outputs.version }}
50-
5137
- uses: r7kamura/rust-problem-matchers@d58b70c4a13c4866d96436315da451d8106f8f08 #v1.3.0
5238

53-
- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
54-
with:
55-
shared-key: msrv-cache
56-
save-if: false
57-
58-
- name: Check if ${{ matrix.crate }} compiles on MSRV (Rust ${{ steps.parse-msrv.outputs.version }})
59-
run: cargo +${{ steps.parse-msrv.outputs.version }} build --package ${{ matrix.crate }} --all-features
60-
6139
- uses: dtolnay/rust-toolchain@stable
6240

6341
- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
@@ -90,6 +68,21 @@ jobs:
9068
cargo metadata --format-version=1 --no-deps | \
9169
jq -e -r '.packages[] | select(.name == "${{ matrix.crate }}") | .dependencies | all(.name != "libp2p")'
9270
71+
- uses: taiki-e/cache-cargo-install-action@7dd0cff2732612ac642812bcec4ada5a279239ed # v1
72+
with:
73+
tool: tomlq
74+
75+
- name: Enforce version in `workspace.dependencies` matches latest version
76+
if: matrix.crate != 'libp2p'
77+
run: |
78+
PACKAGE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "${{ matrix.crate }}") | .version')
79+
SPECIFIED_VERSION=$(tomlq 'workspace.dependencies.${{ matrix.crate }}.version' --file ./Cargo.toml)
80+
81+
echo "Package version: $PACKAGE_VERSION";
82+
echo "Specified version: $SPECIFIED_VERSION";
83+
84+
test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION"
85+
9386
cross:
9487
name: Compile on ${{ matrix.target }}
9588
strategy:
@@ -122,6 +115,30 @@ jobs:
122115

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

118+
msrv:
119+
name: Compile with MSRV
120+
runs-on: ubuntu-latest
121+
steps:
122+
- uses: actions/checkout@v3
123+
124+
- name: Extract MSRV from workspace manifest
125+
shell: bash
126+
run: |
127+
MSRV=$(grep -oP 'rust-version\s*=\s*"\K[^"]+' Cargo.toml)
128+
echo "MSRV=$MSRV" >> $GITHUB_ENV
129+
130+
- uses: dtolnay/rust-toolchain@master
131+
with:
132+
toolchain: ${{ env.MSRV }}
133+
134+
- uses: r7kamura/rust-problem-matchers@d58b70c4a13c4866d96436315da451d8106f8f08 #v1.3.0
135+
136+
- uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f # v2.2.1
137+
with:
138+
save-if: ${{ github.ref == 'refs/heads/master' }}
139+
140+
- run: cargo +$MSRV check --workspace --all-features
141+
125142
feature_matrix: # Test various feature combinations work correctly
126143
name: Compile with select features (${{ matrix.features }})
127144
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)