Cargo Build
Overview
Standardize cargo build runs with consistent flags, colorless output, and format-first workflow. Ensures reproducible builds across environments.
Quick Start
- Run
skills/cargo-build/scripts/run_build.shfrom repository root. - Fix formatting errors first, then address compiler errors.
- Rerun script until build succeeds.
Workflow
- Format first:
cargo fmt --allensures code is formatted before building. - Build command:
cargo build --all-features --workspace --all-targets --message-format=short - Environment:
NO_COLOR=1- Disables color outputCARGO_TERM_COLOR=never- Ensures cargo output is colorless
- Scope: Builds all targets (lib, bin, tests, examples, benches) across entire workspace with all features enabled.
- Output format:
--message-format=shortprovides concise, parseable output.
Best Practices
- Always disable colors: Use
cargo --color=neveror setCARGO_TERM_COLOR=neverfor consistent, parseable output. - Format before build: Run
cargo fmt --allto catch formatting issues early. - Use workspace builds: Build entire workspace with
--workspaceunless targeting specific packages. - Enable all features: Use
--all-featuresto catch feature-gated compilation errors. - Run from root: Execute from repository root to ensure proper workspace resolution.
- Avoid ad hoc flags: Use consistent, documented build commands rather than one-off invocations.
Common Variations
Target specific package
cargo --color=never build -p <package-name>
Release build
cargo --color=never build --release --workspace --all-targets
Check without building
cargo --color=never check --all-features --workspace --all-targets
Failure Handling
- Formatting failures: Fix code formatting issues reported by
cargo fmt. - Compilation errors: Address errors in the order reported by the compiler.
- Rerun full script: Don't run partial commands; always use the complete workflow.