Open SteelPhase opened 4 years ago
Thank you for filing a gopls issue! Please take a look at the Troubleshooting guide, and make sure that you have provided all of the relevant information here.
@gopherbot FeatureRequest
Partly a followup to https://golang.org/cl/204203 -- we should prefer direct deps more than transitive. Also, maybe look into having imports on save do the same sorting.
Change https://golang.org/cl/212021 mentions this issue: internal/imports: consider direct mod deps more relevant
Retitling to reflect the remaining work.
Unimported completions are missing the "sibling imports" trick. This would be a nice feature. I'm not sure whether to reuse the implementation from the imports package or rewrite it from scratch. Sharing code would be nice, but gopls already has all the parsing done. Just pass all the sibling files to GetPackageExports?
Organize Imports is missing the sorting by relevance. This should be easy to plug in to addExternalCandidates, as long as we want it to affect goimports. Sibling imports will take precedence, but that's probably appropriate.
...except that addExternalCandidates goes to great lengths to return the first usable candidate it finds for speed reasons. It would be very nice to prioritize the scan in the right order, rather than sorting afterward.
It would be nice if gopls could scan the entire workspace for import aliasing patterns rather than only sibling files.
I also imagine it would be convenient if the aliased unimported packages were addressable by both the alias and the package name since the user may or may not anticipate the alias.
Any movement on this? It's quite an annoying issue.
Please be specific about what you're seeing and why it's annoying.
Sorry, I wasn't under the impression that the issue covered more than one bug. What I'm seeing is that import-organization-on-save adds imports that aren't direct dependencies (in go.mod
) or even transitive deps (in go.sum
).
For example, assert
often pulls in gotest.tools/assert
, which is in my local mod cache but nowhere else, instead of github.com/stretchr/testify/assert
, which is in go.mod
.
It's very annoying when writing tests, because I prefer to just type a bunch of tests and have imports be resolved on save, instead of tediously autocompleting every import.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Can't test on latest release, will update when brew makes go1.13.5 available
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Pasted in some code that referenced a dependency that needed to be imported.
What did you expect to see?
import to be autocompleted for direct dependency
go.company.org/org/lib/v4/errors
What did you see instead?
import autocompleted to transitive dependency
github.com/pkg/errors
Additional Details
This is the
go.mod
of the module I'm working inoutput of
go mod why