Group imports in workspace source files as:
- Module imports and declarations
- Standard library
- External crates
- Workspace crates
- Crate modules
For example (see also the before/after files in test-data
):
mod module;
use module::Client;
use std::sync::Arc;
use tokio::sync::Mutex;
use other_crate::Flags;
use crate::Options;
This roughly corresponds to the group_imports
unstable rustfmt option, with the difference
that rustfmt
does not distinguish workspace crates from external ones.
For the purpose of grouping imports, test and binaries are considered to be in the same crate, although they technically are different crates. This can for example result in the following grouping:
use anyhow::Context;
use mycrate::**; // the binary belongs to `mycrate`.
use crate::test_utils;
This is driven by implementation simplicity rather than a true design choice.
$ cargo install --git https://github.com/cpg314/cargo-group-imports
Alternatively, see the binaries on the Releases page.
cargo group-imports [OPTIONS] [WORKSPACE]
Arguments:
[WORKSPACE] [default: current folder]
Options:
--fix Apply changes
--color <COLOR> [default: auto] [possible values: auto, always, never]
-h, --help Print help
-V, --version Print version
By default, the tool checks that the imports are correctly grouped and displays a diff otherwise. The --fix
flag applies the necessary changes, if any. This matches the behaviour of cargo clippy
.
$ cargo group-imports
$ cargo group-imports --fix
The return code is 0
when no changes are necessary, 1
otherwise. This can be used in CI checks.