-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
enforce groups of third-party and current module imports #38
Comments
Have you seen #37? This is essentially a very hard problem to solve; see my comment there. |
Thanks. Do you have any other suggestion on how to solve this? Currently if not controlled by a human reviewer it can grow up to several groups.
|
I don't think we can enforce one way to lay out the imports. Some people use two groups, some people use three groups, some people use many more. For example, I've seen underscore imports being a separate fourth group too. Solving the "too many empty lines" problem is generally hard. It's the same with statements and declarations. For now, we're not solving that problem. I guess we could group imports by prefix, for cases like the one you pasted just now. But the problem then is - what prefix? If we use the domain, we'd always join all of If someone has a specific suggestion in mind, I'm happy to discuss it, but I can't think of a simple rule right now. |
perhaps make it configurable. |
@marcelloh it should only ever join imports if they belong in the standard library. If that's not the case, please share a piece of code for me to reproduce the bug. That's different to this issue, though. This issue is purely an enhancement, not a bug. |
You can test this, by having a library that doesn't exist on an external website. |
@marcelloh that's #22, which in turn follows golang/go#32819. See #22 for the reason behind why it works like it does. If you disagree, you can comment there. Please don't comment here though, as this issue is entirely separate. |
I still think there's no clear direction we could take to improve the situation. Like I mentioned earlier, different projects have different setups with different numbers of import groups, and this is without mentioning I'm happy to hear proposals for specific changes or improvements we could make to that logic, such as "force the second group to be X", or "force underscore imports to be grouped together", or "force imports sharing a common path prefix to be in the same group". Note that I'm not endorsing any of these ideas, they are just examples of specific ideas we could look at. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
See golang/go#37641, which was accepted a few months ago. Packages like internal/foo are also somewhat common, and we can know for sure that they don't belong in the standard library, as they couldn't be imported then. Updates #38.
This is now done; see the commit above. |
I want to think about this problem again. Here's a new proposal: We enforce three groups of imports. Standard library (already enforced), third party, current module. Extra groups after these three are only allowed if they are preceded by a comment, which would normally explain why the extra import group is wanted. Otherwise, any extra import groups would be joined into the main three ones. Here's an example of how this could look:
|
Looks like the template above is the most popular one:
However 2 and 3 are often are swapped (also I prefer this style, 'cause internal project related things are more important) |
Just so I understand you - are you agreeing with my proposal above? There wouldn't be an option to swap the second and third groups. If too many people use them in reverse order, we could always allow both orders, but I would rather not. |
Would it still support the |
Sorry for a late reply @mvdan but yeah, totally agree. Also agree that having an option on changing groups is not stable (I mean there is no 1 constant output, as However I prefer having own package right after stdlib, if it still matters ;D |
@zeebo brings up a good point on Slack; sometimes "local imports" are more than just the current module. Imagine a large company at So the mechanism proposed above isn't enough. We also can't rely on any file in the parent directory shared by all the modules, because the modules might live in different git repositories and be cloned in different places. I also don't want to go back to requiring an explicit So here's a suggested way to fix this:
The list of well-known code hosting sites would actually be pretty short, because we don't need to include an endless array of self-hosted gitlabs, giteas, etc. For example, if a module's path is During a transition period of a few months, the tool would support a |
@ryancurrah I think my comment above should answer your question now :) |
BTW, why not to check golang/go to see what is more popular 2-3 or 3-2 ? Quick regex shows that import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/search"
"cmd/go/internal/str"
"cmd/go/internal/vcs"
"cmd/go/internal/web"
"cmd/go/internal/work"
"golang.org/x/mod/module"
) |
Hi again, any plans on this? The question of import sorting popped-up in 1 chat, curious do you @mdvan have a stronger opinion on that or basically what are the open questions. Thanks in advance. |
@mvdan is there a plan to address this? Thanks |
It is on my radar. Please keep in mind that there are dozens of these. I appreciate the enthusiasm, but asking for updates doesn't help - it just adds stress on my end. If you want to help, you can send good PRs or sponsor me so that I can make more free time. |
Sorry for that, I was asking in general, just to understand are there any blockers or open questions. Nothing more.
thanks for the reminder, I forgot that I have changed my card but haven't updated sponsorship! |
Not aimed at anyone in particular, just in general :) No harm done. Only want to give my perspective so that others don't feel like I'm actively ignoring them. |
@mvdan I was more curious if this is planned versus something you don't wish to incorporate. The tool is fantastic, and the only thing blocking us from using it is the fact that we can't group |
Understood. The issue remains open, meaning I mean to get it done at some point, or would accept PRs for it. I'd close it otherwise. |
Hi, thanks for this and for your contributions to Golang.
I am trying this package with the hope of finding a solution to the imports order in go files. I have this:
Original code:
And after running
gofumpt
andgofumports
:"My" expected behaviour:
std
imports must be in a separate group at the topstd
imports in a single separate group at the bottomExample:
This is something that also happens with the official
goimports
.Did you had the intention to fix this as well? I mean, all non
std
imports in a single separate group at the bottom and not in different groups?Thanks.
The text was updated successfully, but these errors were encountered: