cargo-fmt-toml
Cargo subcommand that opinionatedly formats and normalizes Cargo.toml
layout: top-level table order, [package] field order, sorted dependency
keys, and nested-table collapse.
This crate vs cargo-propagate-features
| Expect from cargo-fmt-toml | Expect from cargo-propagate-features |
|---|---|
| Structural changes only: where sections and keys appear, alphabetical dependency names, inline-table collapse. | Semantic changes to [features]: when crate A has feature X and depends on B with the same X, A’s feature list gains B/X (and similar wiring) so enabling X on A turns on the matching features on deps. |
Stable, review-friendly diffs for manifest shape; does not change versions, add/remove dependencies, or rewrite specs to workspace = true. |
Focuses on feature graphs across workspace/path dependencies, not on enforcing a canonical TOML layout. |
Run when you want every member Cargo.toml to follow the same ordering rules. |
Run when you want feature flags to propagate correctly across crates with shared feature names. |
The two tools complement each other: format first (or last) for consistent layout; use propagate-features when you need automatic dep/feature entries in [features].
Only manifests for workspace members are considered, and only if they lie
under the canonical workspace root you pass (default: current directory).
If that directory is inside a git work tree, manifests must also stay
under git rev-parse --show-toplevel (so crates.io checkouts under
~/.cargo/registry are never touched).
Opinionated layout
The formatter is meant to be a policy enforcer, not a minimal pretty-printer.
- Top-level tables — A fixed order for standard Cargo roots (
package, target definitions, dependency tables,features,target,workspace,patch,replace,profile,lints,badges,cargo-features). Any other top-level key (extensions, tooling) is sorted lexicographically after that list so output is deterministic. [package]fields — A fixed order for common keys (name,version,description, … throughmetadata). Remaining keys sort lexicographically at the end of[package].- Dependencies — Keys in
dependencies,dev-dependencies,build-dependencies, and target-specific dependency tables are sorted alphabetically (crate names only; values are not rewritten). - TOML shape — Nested tables in
[package]and dependency sections may be collapsed to inline tables where the formatter applies that transform.
The exact lists live in src/main.rs as TOP_LEVEL_SECTION_ORDER and
PACKAGE_FIELD_ORDER; change them there when the policy evolves.
Installation
Using cargo-binstall (recommended)
Pre-built binaries from GitHub Releases (see [package.metadata.binstall] in
Cargo.toml):
Using cargo install (crates.io)
If the crate is not yet on crates.io, install from git:
Network / offline Cargo config
If the project you run cargo install from sets net.offline = true in
.cargo/config.toml, either run the install from a directory without that
setting or override for one invocation, for example:
CARGO_NET_OFFLINE=false
Features
- Canonical layout — Opinionated top-level and
[package]ordering; unknown keys sorted for stable diffs - Sorted dependency tables — Alphabetical keys in dependency sections
(no version or
workspace = truerewriting) [package]field normalization — Consistent key order and inline-table collapse where applied- Member-only, repo-scoped — Formats workspace member manifests under the chosen root and git work tree (see introduction above)
Workspace dependency policy (centralizing versions, workspace = true refactors)
and feature propagation are out of scope here; see the table in
This crate vs cargo-propagate-features
and cargo-propagate-features.
Usage
# Format all Cargo.toml files in the workspace
# Preview changes without modifying files
# Check if files need formatting (returns non-zero if changes
# needed)
# Explicit workspace root
Package section example
[package] keys are ordered per PACKAGE_FIELD_ORDER in src/main.rs
(identity and metadata first, then metadata and any other keys sorted at the
end). The formatter does not insert workspace = true; below is a typical
hand-written member-crate style:
[]
= "crate-name"
= { = true }
= "Brief description"
= { = true }
= { = true }
= { = true }
= { = true }
= { = true }
Dependency Sorting
All dependency sections are sorted alphabetically:
[dependencies][dev-dependencies][build-dependencies][target.'cfg(...)'.dependencies]
Integration
After cargo install cargo-fmt-toml (or cargo binstall):
: :
: :