Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make imports consistent. #94

Closed
wants to merge 1 commit into from

Conversation

sunli829
Copy link
Contributor

@sunli829 sunli829 commented Aug 3, 2021

I don't know if this is a coding style standard, but I know that many popular projects use this import order. 😁

@davidpdrsn
Copy link
Member

The order of imports isn't something I'm very concerned with tbh. The one used today is the rustfmt's default. Can you configure rustfmt to use this ordering?

@davidpdrsn
Copy link
Member

It seems rustfmt has an group_imports option which when set to StdExternalCrate should match this style. That option isn't stable yet however.

Until rustfmt stable is able to automatically format imports the way we want I'm reluctant to change the formatting as it creates churn and will not be maintained unless done automatically.

Do you know of a way to maintain this style automatically?

@sunli829
Copy link
Contributor Author

sunli829 commented Aug 3, 2021

rustfmt can't do this automatically, I did it manually, but I saw other projects do it, which seems more clear. 😁

@davidpdrsn
Copy link
Member

I did it manually

In that case I think we should wait with this. Even though I'm sorry for discarding all this tedious manual work 😕

The order used now is the default ordering used by rustfmt when all the imports are in one block (no newlines between) so at least that gives some consistency.

@sunli829
Copy link
Contributor Author

sunli829 commented Aug 3, 2021

I respect your decision. 😁

@jplatte
Copy link
Member

jplatte commented Aug 3, 2021

What I've done in a bunch of projects before is have one Stable CI job that runs cargo test (and cargo check for different feature sets) and a one Nightly CI job that runs cargo fmt and cargo clippy (benefitting from new lints as they come out, with the downside that there's also false positives now and then).

@davidpdrsn
Copy link
Member

What do you do for running rustfmt locally then? Override it to use nightly somehow?

@jplatte
Copy link
Member

jplatte commented Aug 3, 2021

Well stable rustfmt still applies all non-experimental formatting options for me and with the codebase being consistently formatted I hardly ever accidentally introduce something that nightly rustfmt would format differently. Occasionally I might cargo +nightly fmt.

@ZhangHanDong
Copy link

The group_imports option doesn't solve this problem either, and you end up having to manually group the imported modules in order to improve code readability. Of course these tasks can be done after the project is stable.

@jplatte
Copy link
Member

jplatte commented Aug 10, 2021

@ZhangHanDong What do you mean by that? I'm aware of two bugs affecting group_imports, but it doesn't sound like that's what you're referring to.

@ZhangHanDong
Copy link

ZhangHanDong commented Aug 10, 2021

@jplatte

What I mean is that relying on group_imports alone doesn't achieve the desired effect either.

The only option currently available for group_imports is StdExternalCrate, which can be divided into three groups.

  1. std, core and alloc,
  2. external crates.
  3. self, super and crate imports.

https://rust-lang.github.io/rustfmt/?#group_imports

But wouldn't external crates have to be manually grouped by the developer?

@jplatte
Copy link
Member

jplatte commented Aug 10, 2021

Not sure what additional grouping of external crates you're talking about. From a quick glance at this PR it seems like it implements exactly what rustfmt would do too with group_imports = "StdExternalCrate".

@ZhangHanDong
Copy link

ZhangHanDong commented Aug 10, 2021

@jplatte You'll understand if you look at the changes in this pr.

Screen Shot 2021-08-10 at 22 34 32

group_imports = "StdExternalCrate " is useless for this kind of grouping and needs to be manually adjusted by the developer.

Does this make it clear?

@jplatte
Copy link
Member

jplatte commented Aug 10, 2021

But why do you think that the previous version would be preferred? I was under the impression that @davidpdrsn was agreeing with changing things in the way that this PR does, but that the churn is not worth it if it can't be enforced in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants