Implementation:Nautechsystems Nautilus trader Check Cargo Conventions
| Knowledge Sources | |
|---|---|
| Domains | Build_Tooling, Rust |
| Last Updated | 2026-02-10 08:00 GMT |
Overview
Shell script that enforces nine Cargo.toml conventions across all workspace crates to prevent dependency drift and manifest inconsistency.
Description
The check_cargo_conventions.sh script is the most comprehensive pre-commit hook in the project, running nine distinct checks against all Cargo.toml files in the workspace. It uses awk and ripgrep to verify: (1) Alphabetical dependency ordering within groups. (2) Standard section ordering (package, lints, lib, features, dependencies, dev-dependencies, build-dependencies, bench, bin, example, test). (3) [lints] workspace = true required for crates with [lib] or bin. (4) doc = false on all bin and example sections. (5) [package] field ordering and required fields. (6) [lib] crate-type ordering (rlib, staticlib, cdylib). (7) Unused workspace dependencies. (8) Related dependency version alignment (capnp/capnpc, arrow/parquet, datafusion/object_store, dydx-proto/prost/tonic). (9) Adapter-only dependencies not used by core crates.
Usage
Runs automatically as a pre-commit hook on any staged Cargo.toml file. Can also be executed manually for checking all workspace manifests.
Code Reference
Source Location
- Repository: Nautechsystems_Nautilus_trader
- File: .pre-commit-hooks/check_cargo_conventions.sh
- Lines: 1-501
Signature
#!/usr/bin/env bash
# Enforces Cargo.toml conventions:
# 1. Dependencies within groups must be alphabetically ordered
# 2. Sections must be in standard order
# 3. Crates with [lib] or [[bin]] must have [lints] workspace = true
# 4. All [[bin]] and [[example]] must have doc = false
# 5. [package] section must have required fields in correct order
# 6. [lib] crate-type must use order: rlib, staticlib, cdylib
# 7. All [workspace.dependencies] must be used by at least one crate
# 8. Related dependency versions must be aligned
# 9. Adapter dependencies should only contain deps used exclusively by adapters
set -euo pipefail
Import
# Run directly
.pre-commit-hooks/check_cargo_conventions.sh
# Or via pre-commit
pre-commit run check-cargo-conventions --all-files
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Cargo.toml files | Files | Yes | All Cargo.toml files found via ripgrep (excluding target/) |
Outputs
| Name | Type | Description |
|---|---|---|
| exit code | Integer | 0 if all conventions pass, 1 if violations found |
| violation report | Stdout | Colored output listing each violation with file, line, and rule |
Usage Examples
# Run the convention checker directly
bash .pre-commit-hooks/check_cargo_conventions.sh
# Example output for violation:
# ❌ crates/core/Cargo.toml:15: dependencies not alphabetically ordered: 'serde' before 'anyhow'