Skip to content
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/gopls: prefer modules in go.mod when importing or ranking completion items #38461

Closed
hyangah opened this issue Apr 15, 2020 · 4 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@hyangah
Copy link
Contributor

hyangah commented Apr 15, 2020

go version: 1.14
gopls version: 0.4.0

When there are multiple matching packages with similar APIs (e.g. v1 vs v2)
and users already specified the preference in their go.mod, I think, choosing
the one listed in the go.mod makes more sense.


Let's assume an incomplete go file:

$ cat main.go
package main

func main() {
        gax.Sleep(context.Background(), 1*time.Second)
}

When only one version exists in the module cache, everything works fine.

$ export GOPATH=`mktemp -d`
$ go get github.com/googleapis/gax-go/v2
go: downloading github.com/googleapis/gax-go/v2 v2.0.5
...
$ gopls imports main.go
package main

import (
	"context"
	"time"

	"github.com/googleapis/gax-go/v2"
)
...

Let's add the version 1 in the module cache, but leave the original go.mod file untouched.
(So, only the v2 module is listed)

$ cp go.mod fake.mod
$ go get -modfile=fake.mod github.com/googleapis/gax-go
go: downloading github.com/googleapis/gax-go v1.0.3
...
$ gopls imports main.go
package main

import (
	"context"
	"time"

	"github.com/googleapis/gax-go"
)
...

Same idea can apply to the auto-completion. In the below image, the completion item lists include candidates from both v1 and v2. The VS Code UI is not great at telling where the candidate is from. The first APICall is from v1.

Screen Shot 2020-04-15 at 10 15 15 AM

@gopherbot gopherbot added this to the Unreleased milestone Apr 15, 2020
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Apr 15, 2020
@stamblerre stamblerre modified the milestones: Unreleased, gopls/v0.5.0 Apr 15, 2020
@heschi
Copy link
Contributor

heschi commented Apr 29, 2020

Dupe of #38104, AFAICT. For what it's worth, because loaded packages are fully scanned and sorted, this is likely to be less of a problem in a real project rather than a trivial main program.

@hyangah
Copy link
Contributor Author

hyangah commented Apr 29, 2020

FYI - I filed this issue based on a real scenario - that involved an attempt to migrate a repo from v1 to v2 api.

@heschi
Copy link
Contributor

heschi commented Apr 29, 2020

If you had only v2 in the go.mod, then it should have been selected.

@gopherbot
Copy link

Change https://golang.org/cl/231619 mentions this issue: internal/lsp/source: sort cached package completions by relevance

@stamblerre stamblerre modified the milestones: gopls/v0.5.0, gopls/v0.4.2 Jun 18, 2020
@golang golang locked and limited conversation to collaborators Jun 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants