-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
x/tools/cmd/goimports: support repairing import grouping/ordering #20818
Comments
Improving goimports is welcome. |
Hey can I please pick this issue ? I would need a bit guidance though. |
Here's what I did before: https://github.com/vasi-stripe/gogroup . Note that this is not exactly what we want in goimports, it still has a number of issues. |
So it turns out that goimports already does this, but only in limited cases. If a file is missing an import, and has multiple import decls, they get all merged together and then sorted. The merging seems to lose associated comments, but otherwise this should be possible to repurpose. See https://github.com/golang/tools/blob/master/go/ast/astutil/imports.go#L151-L173 |
Hi @vasi-stripe, in my opinion the goimports should have an option to remove all blank spaces before formatting. Today, if I have imports unordered and separated by spaces the tool doesn't work as expected. Example (goimports will not do anything): import (
"strings"
"github.com/pkg/errors"
"net/http"
) Expected: import (
"net/http"
"strings"
"github.com/pkg/errors"
) |
@wedneyyuri, I disagree with two things there: (1) "goimports should have an option". We don't like options in code formatters. I agree that the original bug report at the top is a bug, though. It should try to pack things nicely. |
I'd like to extend this proposal with a specific grouping of imports: stdlib, VCS repo-local, non-local. For example,
I have written a tool, importsort, that achieves this by finding the VCS root directory for the file that it is processing, then it looks at the imports in the file and checks if a path is prefixed by the VCS root directory import path. |
I just learned that goimports does allow grouping imports with the My proposal is determining local/non-local programmatically by looking at the VCS root directory. |
I think ideally ordering should only depend on contents of the
Why not something simple like this
I would really like |
Is anyone working on this? The algos for groupings is one thing but repairing holes in the simples possible stdlib/otherlibs can perhaps be done first? |
@dahankzter, I don't know of anybody working on this. They would generally announce so here. |
Change https://golang.org/cl/116795 mentions this issue: |
@bradfitz Is anything blocking the above "pull request"? |
@lhecker, vacation is. I'm back in a week. I'll get to it at some point after then. |
@bradfitz Oh I'm sorry. Don't bother with my silly question - Please do enjoy your vacation instead. 😄 |
Thanks for taking this on, @lhecker - I was just looking at how to integrate exactly this functionality into |
I'm using this too. There were some cases when it doesn't work properly, so it's better to use the last patch. |
I'm +1 on being more prescriptive and making goimports more aggressively normalize import blocks. There should be one true way to do it, including whitespace. If we really need to add a knob, I'd want it to be IN the codebase (per file or per module) rather than a flag to the binary. IMO, running |
As a workaround, I am using https://github.com/rinchsan/gosimports as an alternative goimports to make an import block style more consistent, but I am so happy if goimports gets more prescriptive and say goodbye to https://github.com/rinchsan/gosimports . |
I've never seen so many engineers agree with each other! We at CircleCI are also planning to switch away from this tool to |
golang/go#20818 のように空行が入る問題を回避するために github.com/rinchsan/gosimports へ乗り換える。
golang/go#20818 のように空行が入る問題を回避するために github.com/rinchsan/gosimports へ乗り換える。
golang/go#20818 のように空行が入る問題を回避するために github.com/rinchsan/gosimports へ乗り換える。
GCI can do this and it is built-in in |
@daixiang0 But GCI does not support multiple prefixes in one group daixiang0/gci#107 |
@yjc567 you can implement it in GCI. |
I have recently implemented a tool and framework for programmatically managing imports ordering that is
|
I'm working on a tool to fix up non-conventional or incorrect imports grouping/ordering, but I'd much rather have this be part of goimports. Would it be welcome?
What goimports does now
When goimports adds or removes an import, it heuristically attempts to find a good place for it. So if I add an
os.Open()
call, and my imports already look like this:Then goimports will put "os" in the proper spot, beween "context" and "testing". That's great!
What goimports does not do
If my imports are not grouped and sorted in the conventional way, goimports will not usually fix the problem. For example, if I was using the old context package, my imports might look like this:
When I edit the context import to the new package, "context", I have this:
Running goimports will notice that logrus and context don't make a proper group, so it will separate them:
But this isn't what we really want. Ideally, it would just re-group and re-order all the imports, to yield:
Work so far
I have a tool that will globally reorder imports according to the grouping rules, including the associated doc-comments; see below. I could also develop similar funcitonality inside goimports.
My tool is not nearly perfect: It doesn't handle multiple import declarations; it doesn't know about
import "C"
. It's not even clear what should happen to random non-doc-comments inside the import block, eg:Automatically grouping and ordering would also be a behavioral change for goimports, so it would have to be behind a flag for now.
The text was updated successfully, but these errors were encountered: