Quick discussion re Rust import sorting: Currently...
# development
h
Quick discussion re Rust import sorting: Currently
fmt
does only very basic import sorting within a single
use
or a single blank-line-delimited block, but it doesn't organize by group (std vs third party crate vs this crate). More powerful import sorting is an unstable feature, so requires use of Rust nightly. (cont. in thread)
It is possible to use this just for formatting by explicitly calling
./cargo +nightly fmt ...
(and if you forgot the
+nightly
part you'd get errors because there would be unrecognized directives in rustfmt.toml.)
We could also modify the
./cargo
script to detect
fmt
and add the
+nightly
for us
And there is a simple way to get vscode to use this as well
So some options are:
1. Keep things the way they are, and don't worry about import sorting in Rust for now, at least until these features become stable. 2. Use
+nightly
for
fmt
alone, and hack it in to
./cargo
.
I am deliberately omitting the option of "have a required or preferred import ordering style that is manually enforced by code review". This sort of style convention should be enforced mechanically or not at all.
I am agnostic, but I will insist that if 1 is chosen then we accept that import order isn't a thing we comment on in code reviews...
f
My vote would be (2) but with the caveat that we pin a particular version of nightly in
rust-toolchain.toml
.
This could also be a nice-to-have and I could just submit a
cargo +nightly fmt
PR every now and then.
h
Also true
There is a way to just run the nightly thing in vscode
or manually, of course
important to note that if you do choose to do so, the regular, stable
fmt
will accept the results
But the issue is that
+nightly
also requires some settings in
rustfmt.toml
that we can't leave in there for regular use
So you'd have to have a separate rustfmt.toml somewhere and point to it
In vscode, add this in
settings.json
to get the new behavior:
Copy code
"rust-analyzer.rustfmt.extraArgs": [ "+nightly", "--config", "imports_granularity=Module,group_imports=StdExternalCrate,reorder_modules=true" ],
w
As someone who would prefer the nightly sorting, I'm a strict (1) - as adding another toolchain just to support import formatting is over the top. Our CI is flaky enough without needing to worry about another Rust toolchain on top
h
Yeah, so adding this to CI is premature, but individual developers can choose to opt in to it on desktop, since from my testing whatever it comes up with is accepted as valid by stable
fmt
Anyway, on the cmd line this will do the thing:
Copy code
$ ./cargo +nightly fmt -- --config imports_granularity=Module,group_imports=StdExternalCrate,reorder_modules=true
If folks want to play with it
and if/when this becomes stable we can add it to CI
f
w
Might be worth doing a one-off format with those configs (if they're substantially out of date), and then leaving "+nightly" as local to dev configs and not discussing in PR. Not a lot of people work on rust, so it doesn't see as much import churn anyways. Definitely not to the point of another toolchain in CI. And as Benjy alluded to, I don't think regular fmt undoes anything
Ditto for scie-pants whenever it's next updated