-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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/imports: use named imports for all import path/package name mismatches #28428
Comments
I feel like this was discussed elsewhere. I'll try to find it later. I'm not entirely opposed to it. One additional benefit is that it kinda nudges people towards making them match, since their callers' imports will be uglier otherwise. But I'm a little concerned that you might be proposing that the move to go/packages will make this path slower, at least on the first run (before it adds the proposed alias hint). I've spent a fair bit of time making sure goimports is fast (the directory fastwalk stuff, the heuristics on which order we parse packages in to get a match sooner than later to minimize I/O and parsing, etc). I don't want to throw those speed benefits out with some move to an abstraction layer. If anything, can't go/packages cache aggressively somewhere? disk/memory? |
Sorry, hopefully not too redundant. Yeah, moving it to go/packages is a big slowdown today, something like a factor of 20. It's not good. Russ has mentioned some thoughts about adding an on-disk cache to We wouldn't merge go/packages into goimports until the performance hit is lower, but shelling out to Basically, while this isn't a magic bullet, it simplifies the goimports code, makes the module version a lot faster in the relevant cases, the non-module version a tiny bit faster in all cases, and IMO makes the code more readable. So I think it's worth doing.
For the record, we do lose this. I don't think there's any way to avoid it without calling |
This worries me. Before goimports moves to a new in-development abstraction layer, I think the new abstraction layer needs to consider the needs of one of its early users and be designed (& implemented) accordingly. 20x or even 2x performance loss is pretty sad. 2x would be more palatable, though. |
I haven't been in the strategic discussions, but my understanding is that Russ wants Given that, I don't think it's helpful to blame the slowdown on Again, nobody is saying that this is going to be merged until the performance is better. We are taking the same experimental approach with many other tools, though admittedly most of them are less performance sensitive or run as a server with some form of in-memory caching. See #24661. |
I'm not blaming any specific piece. I haven't been following closely enough to even be able to do that. When I say "go/packages" I just mean "all the new stuff", whatever that may be. I just want the new stuff to coordinate with its own pieces to be able to do what goimports needs, quickly. If we have to go through |
Okay, but that's not what this bug was asking about. |
I'm fine with always adding the aliases in any/all versions of goimports, and doing so at any time. Sorry for the tangent. This bug just made me (perhaps rightfully) scared of upcoming performance problems. |
Change https://golang.org/cl/145699 mentions this issue: |
I wonder if common mismatches should be ignored? There are a lot of repos out there with a prefix of |
I'm open to the idea, but IMO we need some principle behind what we do and don't make exceptions for. Without that, this feels like a slippery slope to me. I would rather add a few spurious aliases than confuse people. That said, dashes are a relatively easy case, since they're not valid in package names. There's currently some logic that handles them by removing the dashes altogether and doing a substring match; I think I'd rather do something like check every combination of fragments. It'll complicate the code a bit, but it wouldn't be the end of the world. |
I'm looking to submit CL 145699 soon. I'm guessing from the lack of comments that there aren't strong feelings here, so my inclination is to try the simplest thing and see how it goes. Please speak now if you disagree. |
@heschik Did you mean to close this issue? |
Reopening. |
It's been a week with no comment so I'm going to go ahead and submit. (@magical, thanks for pointing out that I'd closed the issue, I had no idea that would happen and didn't get email about it) |
Rolled back for #28645. |
Change https://golang.org/cl/148878 mentions this issue: |
Change https://golang.org/cl/152000 mentions this issue: |
goimports has been changed to add named imports when the imported package path does not match the package name: golang/go#28428. We did not use named imports in such cases, so this broke the lint check in CI. Apply "goimports -w" to make it happy. Change-Id: I7f55aa34db9742cb778f0d91ea6a43fc5d4799c6 Partial-Bug: #1795683
Recent version of goimports committed a few days ago now adds an explicit package name tag for imports whose last path component differ from the package name (see golang/go#28428 and https://go-review.googlesource.com/c/tools/+/152000).
goimports checks import lines, adding missing ones and removing unreferenced ones: https://godoc.org/golang.org/x/tools/cmd/goimports It also requires named imports for packages whose import paths don't match their package names: - golang/go#28428 - https://go-review.googlesource.com/c/tools/+/145699/ Also standardized named imports of common Kubernetes packaages. Part of #217 Signed-off-by: Andrew Seigner <siggy@buoyant.io>
goimports checks import lines, adding missing ones and removing unreferenced ones: https://godoc.org/golang.org/x/tools/cmd/goimports It also requires named imports for packages whose import paths don't match their package names: - golang/go#28428 - https://go-review.googlesource.com/c/tools/+/145699/ Also standardized named imports of common Kubernetes packaages. Part of #217 Signed-off-by: Andrew Seigner <siggy@buoyant.io>
goimports checks import lines, adding missing ones and removing unreferenced ones: https://godoc.org/golang.org/x/tools/cmd/goimports It also requires named imports for packages whose import paths don't match their package names: - golang/go#28428 - https://go-review.googlesource.com/c/tools/+/145699/ Also standardized named imports of common Kubernetes packaages. Part of #217 Signed-off-by: Andrew Seigner <siggy@buoyant.io>
Main change is the import alias: golang/go#28428 Quite annoying but nothing much we can do. Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
Main change is the import alias: golang/go#28428 Quite annoying but nothing much we can do. Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
Main change is the import alias: golang/go#28428 Quite annoying but nothing much we can do. Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
Some packages' import paths don't match their package name. The only way to know for sure what package an import path contains is to parse it a little. That means that you can't tell just by looking at a Go file whether all its imports are satisfied, because you don't know for sure which import statements provide which package names.
goimports
does its parsing in theimportPathToNameGoPathParse
function. As the name suggests, it's GOPATH-specific. In module mode, mapping an import path to a package is much more costly -- you have to rungo list
, perhaps via go/packages. Having that call in the middle of the fast path of goimports is not ideal, especially today where it can take a good chunk of a second to run.After talking about this with @ianthehat, our proposal is that
goimports
add an import alias for all mismatched packages. (Version suffixes would be okay.) That would make the fast path of goimports purely syntactic, so it can be even faster than today. We think it's also clearer for readers.@bradfitz, does this sound good to you? It may be pretty noticeable to some users but hopefully they'll like it.
The text was updated successfully, but these errors were encountered: