Crates.io | cargo-sort-fix |
lib.rs | cargo-sort-fix |
version | 2.0.5 |
created_at | 2025-06-20 03:09:35.027601+00 |
updated_at | 2025-06-28 02:57:34.950731+00 |
description | Check if tables and items in a .toml file are lexically sorted |
homepage | |
repository | https://github.com/ssrlive/cargo-sort-fix |
max_upload_size | |
id | 1719078 |
size | 83,809 |
Since the original author of cargo-sort
is no longer active and the successor maintainer has a bad attitude towards contributors, I forked that project to cargo-sort-fix
.
A tool to check that your Cargo.toml dependencies are sorted alphabetically. Project created as a solution to @dtolnay's request for implementation #29. Cross platform implementation, windows compatible. Terminal coloring works on both cmd.exe and powershell. Checks/sorts by key in tables and also nested table headers (does not sort the items in a nested header, sorts the table itself). cargo sort-fix
uses toml-edit to parse the toml file into something useful.
The --format
option may result in improperly formatted toml; please file an issue.
There are three modes cargo-sort-fix can be used in:
key_value_newlines
config option needs to be true
for this to have any effect.cargo sort-fix
uses a config file when formatting called tomlfmt.toml
. This is optional and defaults will
be used if not found in the current working dir.
Here are the defaults when no tomlfmt.toml
is found
# trailing comma in arrays
always_trailing_comma = false
# trailing comma when multi-line
multiline_trailing_comma = true
# the maximum length in bytes of the string of an array object
max_array_line_len = 80
# number of spaces to indent
indent_count = 4
# space around equal sign
space_around_eq = true
# remove all the spacing inside the array
compact_arrays = false
# remove all the spacing inside the object
compact_inline_tables = false
trailing_newline = true
# is it ok to have blank lines inside of a table
# this option needs to be true for the --grouped flag
key_value_newlines = true
allowed_blank_lines = 1
# windows style line endings
crlf = false
# The user specified ordering of tables in a document.
# All unspecified tables will come after these.
table_order = []
included in sort check is:
["dependencies"]
["dev-dependencies"]
["build-dependencies"]
["workspace.members"]
["workspace.exclude"]
If you have a header to add open a PR, they are welcome.
cargo install cargo-sort-fix --git https://github.com/ssrlive/cargo-sort-fix
If you use pre-commit in your project, you can add cargo-sort-fix as hook by
adding the following entry to your .pre-commit-config.yaml
configuration:
repos:
- repo: https://github.com/ssrlive/cargo-sort-fix
rev: v2.0.2
hooks:
- id: cargo-sort-fix
Please make sure to set rev
to the latest tag of this repo as the tag shown here might not always
be updated to the latest version.
Thanks to dspicher for issue #4 you can now invoke cargo sort-fix
check as a cargo subcommand
cargo sort-fix [FLAGS] [path]
Wildcard expansion is supported so you can do this
cargo-sort-fix [FLAGS] [path/to/*/Cargo.toml | path/to/*]
or any other pattern that is supported by your terminal. This also means multiple paths work.
cargo-sort-fix [FLAGS] path/to/a path/to/b path/to/c/Cargo.toml
Finally cargo sort-fix has the --workspace flag and will sort each Cargo.toml file in a workspace
cargo-sort-fix -w/--workspace
These are all valid. File names and extensions can be used on some of the paths but not others, if left off the tool will default to Cargo.toml.
cargo sort-fix -h
cargo-sort-fix v2.0.2
ssrlive, Devin R <devin.ragotzy@gmail.com>, Andronik Ordian <write@reusable.software>
Ensure Cargo.toml dependency tables are sorted.
Usage: cargo sort-fix [OPTIONS] [CWD]...
Arguments:
[CWD]... sets cwd, must contain a Cargo.toml file
Options:
-c, --check Returns non-zero exit code if Cargo.toml is unsorted, overrides default behavior
-p, --print Prints Cargo.toml, lexically sorted, to stdout
-n, --no-format Skips formatting after sorting
--check-format Also returns non-zero exit code if formatting changes
-w, --workspace Checks every crate in a workspace
-g, --grouped Keep blank lines when sorting groups of key value pairs
-o, --order <ORDER> List the order tables should be written out (--order package,dependencies,features)
-h, --help Print help
-V, --version Print version
NOTE: formatting is applied after the check for sorting so sorted but unformatted toml will not cause a failure
Build the image:
docker build -t cargo-sort-fix .
Run the container:
docker run -it --rm -v "$(pwd)/Cargo.toml":/app/Cargo.toml cargo-sort-fix
Image is also available on Docker Hub:
docker run -it --rm -v "$(pwd)/Cargo.toml":/app/Cargo.toml ssrlive/cargo-sort-fix:latest
[dependencies]
a="0.1.1"
# comments will stay with the item
c="0.1.1"
# If --grouped is used the blank line will stay.
b="0.1.1"
[dependencies.alpha]
version="0"
[build-dependencies]
foo="0"
bar="0"
# comments will also stay with header
[dependencies.zed]
version="0"
[dependencies.beta]
version="0"
[dev-dependencies]
bar="0"
foo="0"
Will sort to, or fail until organized like so
[dependencies]
a="0.1.1"
# If --grouped is used the blank line will stay
b="0.1.1"
# comments will stay with the item
c="0.1.1"
[dependencies.alpha]
version="0"
[dependencies.beta]
version="0"
# comments will also stay with header
[dependencies.zed]
version="0"
# Tables are ordered by their appearance so
# if dev-dependencies was before build it would be
# sorted that way unless --order is specified
[build-dependencies]
bar="0"
foo="0"
[dev-dependencies]
bar="0"
foo="0"