-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
cmd/gofmt: formatting should sort and cluster imports in a standard way #48552
Comments
100% agree on the idea that the imports should be forcefully formatted to the One True Way. I don't know that I actually care about what way that is; only that I don't want to argue over it. This exact issue is why our team has a different formatter, that wraps the go fmt library and then reformats the imports to the team standard. |
Based on your code examples, it seems like you're implicitly proposing a particular algorithm but haven't stated exactly what it is. Here's how I understand the algorithm implied by your example; please correct me if this doesn't match what you intended.
I expect that in the above the definition of "in the standard library" is probably the most debatable sub-question, though presumably the Go toolchain already has some sense of that in order to avoid attempting external module package installation for those, and so I'd expect it to follow the same rules. (FWIW, I don't have a strong opinion on whether or not to do this, just wanted to attempt to clarify what exactly the proposal is.) |
I'm the same as @jfesler I chatted a bit on a Gopher slack and people definitely use different libraries to fix this problem. So maybe look at what is being done in these libraries. |
Fair enough! I guess then my previous comment is an example of one possible algorithm that Some other questions that come to my mind when I think about this are:
Again, I don't have a strong opinion either way on whether to do this, but it seems like whether it would be accepted depends at least in part on finding reasonable answers to these questions that can produce reasonable results for most existing source files, and so I'm thinking here about the sorts of edge cases I've seen which seem to have motivated "non-standard" import ordering in source files I regularly interact with. Unfortunately the burden of introducing "one true way" is having to decide what that way is, even if you don't actually care very much! (which is also true for me) 😀 |
It is sometimes necessary to order them "the way the dev wants" in order to make sure the init() functions of the imported packages are called in the right way with the right order. This is for example needed for Gui libraries in which drawing is only allowed on the main OS thread, and where the GUI llbrary has to call LockOsThread() in init() to be able to execute on that thread, before any other package, especially if that package also uses LocksOsThread(). That's why go fmt currently leaves separate blocks separate, to keep this possible. |
@beoran This is quite interesting what you bring up here. I hope this doesn't prevent this issues to go forward anyway.
Then format, you get
But if you had ordered "fmt" prior because you "think?!" you have an
You still get
So now, being able to order ONLY within an As mentioned, I'm happy you bring this up so that we are aware of what is going on, but I hope a consensus is reached. |
I also don't care how this is solved but there must remain a way to allow manual ordering as well. And init() can sometiles be an anti-pattern. But init() is essential in some specific cases, such as the example of a GUI or game library, that must lock its goroutine to the primary thread, before main is called. |
May be import blocks should be processed separately by the tool without merging blocks together, so order of initialization can be defined by order of import blocks. While imports inside block should be reordered, empty lines added/removed, so any order with any number of empty lines would turn into some canonical representation (for example ordered by import path with single empty line between different sites seems what most find beautiful, but any representation will be great. If block has only single import, then parenthesis must be either always removed or always added) For comments inside import block, if they are not in the form of single line comment after import path, I'd move them out of import block below closing ")". If comment is absolutely required for some import and does not fit on single line with it, then this import can be separated into separate import block and commented outside that block. Or simply comment can be written with package mentioned in the text. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
The order of the imports is not something that "matters" to developers, yet the developers will comment during code reviews to order them the right way. Why? There is literally no reason for having them in a specific order other than the "look" of them.
Furthermore, by having them manually fiddled with, it causes avoidable merge issues.
Currently, the imports are ordered but by block only. If I separate my imports in different blocks, they will be all left untouched. It will take the solo import lines and sort them correctly. So there is some kind of logic applied so it "looks" good, but it should go all the way opiniated and not allow the dev to order them the way they want.
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: