Skip to content

Commit f94565d

Browse files
committed
chore: add rust-precommit skill and cross-platform script
1 parent 93285a8 commit f94565d

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
name: "rust-precommit"
3+
description: "Run the mandatory PET Rust pre-commit checks (cargo fmt + cargo clippy -D warnings) before every commit. Invoke whenever you are about to commit Rust changes, after applying review feedback, or whenever you need to verify formatting and lint cleanliness."
4+
---
5+
6+
# Rust Pre-Commit Checks
7+
8+
The PET maintainer workflow requires `cargo fmt --all` and `cargo clippy --all -- -D warnings` to pass before every commit. This skill wraps both into a single script invocation.
9+
10+
## When to use
11+
12+
- Before staging any Rust changes for commit.
13+
- After fixing review comments on a PR, before pushing.
14+
- Whenever the agent is asked "run pre-commit checks" or "validate the Rust code".
15+
16+
## How to run
17+
18+
From the workspace root:
19+
20+
### Windows (pwsh)
21+
22+
```powershell
23+
./scripts/rust-precommit.ps1
24+
```
25+
26+
### macOS / Linux
27+
28+
```bash
29+
./scripts/rust-precommit.sh
30+
```
31+
32+
Both scripts:
33+
34+
1. Run `cargo fmt --all` (auto-formats; fails only on unrecoverable errors).
35+
2. Run `cargo clippy --all -- -D warnings` (treats warnings as errors).
36+
3. Exit non-zero on the first failure so callers can gate commits.
37+
38+
## Handling failures
39+
40+
- **fmt failure:** rare; usually a syntax error preventing parsing. Fix the underlying compile error first.
41+
- **clippy failure:** read the diagnostic, fix the code. Do **not** add `#[allow(...)]` to suppress unless absolutely justified.
42+
- After any fix, re-run the script until it reports `ALL PRE-COMMIT CHECKS PASSED`.
43+
44+
## Notes
45+
46+
- The script must be run from the repository root (or pass the workspace path as the first arg to the `.sh` variant / `-WorkspacePath` to the `.ps1` variant).
47+
- Keep the script in sync with `.github/copilot-instructions.md` "Required Before Committing" section.

scripts/rust-precommit.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env pwsh
2+
# Rust pre-commit checks for PET workspace.
3+
# Runs `cargo fmt --all` and `cargo clippy --all -- -D warnings`.
4+
# Exits non-zero on any failure so callers can gate commits.
5+
6+
[CmdletBinding()]
7+
param(
8+
[string]$WorkspacePath = (Get-Location).Path
9+
)
10+
11+
$ErrorActionPreference = 'Stop'
12+
Set-Location -Path $WorkspacePath
13+
14+
Write-Host '=== RUST PRE-COMMIT CHECKS ===' -ForegroundColor Cyan
15+
16+
if (-not (Get-Command cargo -ErrorAction SilentlyContinue)) {
17+
Write-Error 'FAIL: cargo not found in PATH'
18+
exit 1
19+
}
20+
21+
Write-Host ''
22+
Write-Host '--- Step 1: cargo fmt --all ---' -ForegroundColor Cyan
23+
& cargo fmt --all
24+
if ($LASTEXITCODE -ne 0) {
25+
Write-Error 'FAIL: cargo fmt failed.'
26+
exit 1
27+
}
28+
Write-Host 'PASS: Formatting complete.' -ForegroundColor Green
29+
30+
Write-Host ''
31+
Write-Host '--- Step 2: cargo clippy --all -- -D warnings ---' -ForegroundColor Cyan
32+
& cargo clippy --all -- -D warnings
33+
if ($LASTEXITCODE -ne 0) {
34+
Write-Error 'FAIL: clippy reported errors. Fix them, then re-run.'
35+
exit 1
36+
}
37+
Write-Host 'PASS: Clippy clean.' -ForegroundColor Green
38+
39+
Write-Host ''
40+
Write-Host '=== ALL PRE-COMMIT CHECKS PASSED ===' -ForegroundColor Green
41+
Write-Host 'Safe to commit.'

scripts/rust-precommit.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
# Rust pre-commit checks for PET workspace.
3+
# Runs `cargo fmt --all` and `cargo clippy --all -- -D warnings`.
4+
# Exits non-zero on any failure so callers can gate commits.
5+
6+
set -euo pipefail
7+
8+
WORKSPACE="${1:-.}"
9+
cd "$WORKSPACE"
10+
11+
echo "=== RUST PRE-COMMIT CHECKS ==="
12+
13+
if ! command -v cargo &> /dev/null; then
14+
if [ -f "$HOME/.cargo/env" ]; then
15+
# shellcheck disable=SC1091
16+
source "$HOME/.cargo/env"
17+
else
18+
echo "FAIL: cargo not found in PATH" >&2
19+
exit 1
20+
fi
21+
fi
22+
23+
echo ""
24+
echo "--- Step 1: cargo fmt --all ---"
25+
if ! cargo fmt --all; then
26+
echo "FAIL: cargo fmt failed." >&2
27+
exit 1
28+
fi
29+
echo "PASS: Formatting complete."
30+
31+
echo ""
32+
echo "--- Step 2: cargo clippy --all -- -D warnings ---"
33+
if ! cargo clippy --all -- -D warnings; then
34+
echo "FAIL: clippy reported errors. Fix them, then re-run." >&2
35+
exit 1
36+
fi
37+
echo "PASS: Clippy clean."
38+
39+
echo ""
40+
echo "=== ALL PRE-COMMIT CHECKS PASSED ==="
41+
echo "Safe to commit."

0 commit comments

Comments
 (0)