From 56f5097d1c8de968b845eda9e2abd5207ee76825 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Fri, 18 Jul 2025 10:28:10 -0500 Subject: [PATCH] Prevent dirty `Cargo.lock` changes from install (#18693) Spawning from https://github.com/element-hq/synapse/pull/18689 Example CI failure that will stop people from leaving stray `Cargo.lock` changes behind, ``` Error: Cargo.lock has uncommitted changes after install. Please run 'poetry install --extras all' and commit the Cargo.lock changes. ``` --- .github/workflows/tests.yml | 42 ++++++++++++++++++++++++++++++++++++- .rustfmt.toml | 5 +++++ changelog.d/18693.misc | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 changelog.d/18693.misc diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f6250c8ed2..4e32349432 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -263,6 +263,43 @@ jobs: - run: cargo clippy --all-features -- -D warnings + lint-rust: + runs-on: ubuntu-latest + needs: changes + if: ${{ needs.changes.outputs.rust == 'true' }} + + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Install Rust + uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b # master + with: + toolchain: ${{ env.RUST_VERSION }} + - uses: Swatinem/rust-cache@98c8021b550208e191a6a3145459bfc9fb29c4c0 # v2.8.0 + + - name: Setup Poetry + uses: matrix-org/setup-python-poetry@5bbf6603c5c930615ec8a29f1b5d7d258d905aa4 # v2.0.0 + with: + # Install like a normal project from source with all optional dependencies + extras: all + install-project: "true" + poetry-version: "2.1.1" + + - name: Ensure `Cargo.lock` is up to date (no stray changes after install) + # The `::error::` syntax is using GitHub Actions' error annotations, see + # https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions + run: | + if git diff --quiet Cargo.lock; then + echo "Cargo.lock is up to date" + else + echo "::error::Cargo.lock has uncommitted changes after install. Please run 'poetry install --extras all' and commit the Cargo.lock changes." + git diff --exit-code Cargo.lock + exit 1 + fi + + # This job is split from `lint-rust` because it requires a nightly Rust toolchain + # for some of the unstable options we use in `.rustfmt.toml`. lint-rustfmt: runs-on: ubuntu-latest needs: changes @@ -274,7 +311,8 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b # master with: - # We use nightly so that it correctly groups together imports + # We use nightly so that we can use some unstable options that we use in + # `.rustfmt.toml`. toolchain: nightly-2025-04-23 components: rustfmt - uses: Swatinem/rust-cache@98c8021b550208e191a6a3145459bfc9fb29c4c0 # v2.8.0 @@ -309,6 +347,7 @@ jobs: - check-lockfile - lint-clippy - lint-clippy-nightly + - lint-rust - lint-rustfmt - lint-readme runs-on: ubuntu-latest @@ -327,6 +366,7 @@ jobs: lint-pydantic lint-clippy lint-clippy-nightly + lint-rust lint-rustfmt lint-readme diff --git a/.rustfmt.toml b/.rustfmt.toml index bf96e7743d..29e67033cc 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1 +1,6 @@ +# Unstable options are only available on a nightly toolchain and must be opted into +unstable_features = true + +# `group_imports` is an unstable option that requires nightly Rust toolchain. Tracked by +# https://github.com/rust-lang/rustfmt/issues/5083 group_imports = "StdExternalCrate" diff --git a/changelog.d/18693.misc b/changelog.d/18693.misc new file mode 100644 index 0000000000..e2fdaed6b2 --- /dev/null +++ b/changelog.d/18693.misc @@ -0,0 +1 @@ +Prevent dirty `Cargo.lock` changes from install.