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: Code completion in gopls is not working, unable to find the packages to import #71462

Open
tyanxie opened this issue Jan 28, 2025 · 5 comments
Labels
gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. ToolProposal Issues describing a requested change to a Go tool or command-line program. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@tyanxie
Copy link

tyanxie commented Jan 28, 2025

What did you do?

I have enabled gopls in Neovim through configuring lspconfig. When I am working in a new project and type http, gopls does not provide code completion suggestions for net/http. And this issue is not limited to the http module, it affects all modules.

I have noticed that when I manually add import net/http to the import block, the code completion starts to work properly, and it also begins to suggest other modules correctly.

I guess that maybe gopls only indexes the modules in the import block and does not index the standard library or the modules listed in go.mod by default. I believe this is not the expected behavior. I think indexing the standard library and the modules in go.mod should be considered an expected behavior.

What did you expect to see?

Neovim should provide suggestions for packages that have not yet been imported, such as directly using http in a new project as I mentioned earlier.

Image

What did you see instead?

Neovim does not suggest packages that have not yet been imported.

Image

Build info

{
        "GoVersion": "go1.23.5",
        "Path": "golang.org/x/tools/gopls",
        "Main": {
                "Path": "golang.org/x/tools/gopls",
                "Version": "v0.17.1",
                "Sum": "h1:Mt/DSfnnSe3dyf6MH/dZZ0iww+viHNhAFc4rEYDiOAw=",
                "Replace": null
        },
        "Deps": [
                {
                        "Path": "github.com/BurntSushi/toml",
                        "Version": "v1.4.1-0.20240526193622-a339e1f7089c",
                        "Sum": "h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=",
                        "Replace": null
                },
                {
                        "Path": "github.com/google/go-cmp",
                        "Version": "v0.6.0",
                        "Sum": "h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/exp/typeparams",
                        "Version": "v0.0.0-20231108232855-2478ac86f678",
                        "Sum": "h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/mod",
                        "Version": "v0.22.0",
                        "Sum": "h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/sync",
                        "Version": "v0.9.0",
                        "Sum": "h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/telemetry",
                        "Version": "v0.0.0-20241106142447-58a1122356f5",
                        "Sum": "h1:TCDqnvbBsFapViksHcHySl/sW4+rTGNIAoJJesHRuMM=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/text",
                        "Version": "v0.20.0",
                        "Sum": "h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/tools",
                        "Version": "v0.27.1-0.20241219162658-575221bfbda3",
                        "Sum": "h1:kgwdasJRsdDWYgWcEgMF424DiXwwXHSb3V8xVTi//i8=",
                        "Replace": null
                },
                {
                        "Path": "golang.org/x/vuln",
                        "Version": "v1.0.4",
                        "Sum": "h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=",
                        "Replace": null
                },
                {
                        "Path": "honnef.co/go/tools",
                        "Version": "v0.5.1",
                        "Sum": "h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=",
                        "Replace": null
                },
                {
                        "Path": "mvdan.cc/gofumpt",
                        "Version": "v0.7.0",
                        "Sum": "h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=",
                        "Replace": null
                },
                {
                        "Path": "mvdan.cc/xurls/v2",
                        "Version": "v2.5.0",
                        "Sum": "h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=",
                        "Replace": null
                }
        ],
        "Settings": [
                {
                        "Key": "-buildmode",
                        "Value": "exe"
                },
                {
                        "Key": "-compiler",
                        "Value": "gc"
                },
                {
                        "Key": "CGO_ENABLED",
                        "Value": "1"
                },
                {
                        "Key": "CGO_CFLAGS",
                        "Value": ""
                },
                {
                        "Key": "CGO_CPPFLAGS",
                        "Value": ""
                },
                {
                        "Key": "CGO_CXXFLAGS",
                        "Value": ""
                },
                {
                        "Key": "CGO_LDFLAGS",
                        "Value": ""
                },
                {
                        "Key": "GOARCH",
                        "Value": "arm64"
                },
                {
                        "Key": "GOOS",
                        "Value": "darwin"
                },
                {
                        "Key": "GOARM64",
                        "Value": "v8.0"
                }
        ],
        "Version": "v0.17.1"
}
@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 Jan 28, 2025
@gopherbot gopherbot added this to the Unreleased milestone Jan 28, 2025
@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Jan 28, 2025
@ansaba ansaba added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 30, 2025
@findleyr
Copy link
Member

findleyr commented Feb 6, 2025

Gopls does index the standard library as well as module cache and any workspace dependencies, and should offer unimported completion in this case. I'm not able to reproduce using nvim+coc.nvim (my default editor setup). Can you try in VS Code?

@tyanxie
Copy link
Author

tyanxie commented Feb 7, 2025

@findleyr Thanks for your reply! The above problem was alleviated after I deleted the cache file of gopls (~/.cache/gopls/), but it seems that there is still a problem.

After I go get a new module in some repositories, gopls does not seem to index this module normally and give a prompt. For example, after I executed go get github.com/dustin/go-humanize@latest in one of my code repositories, I typed human in the code, and the code completion prompt did not pop up normally. I tested it with both vscode and neovim.

vscode:
Image

#neovim:
Image

Unfortunately, I cannot provide the code repository where I reproduced this problem. Maybe you need some more information. I can try to investigate whether it can be provided.

@tttoad
Copy link

tttoad commented Feb 10, 2025

@tyanxie This is probably caused by gopls not listening for changes to files under the $GOPATH path. You can use :LspRestart in neovim to reload.

@tyanxie
Copy link
Author

tyanxie commented Feb 10, 2025

@tyanxie This is probably caused by gopls not listening for changes to files under the $GOPATH path. You can use :LspRestart in neovim to reload.

Thanks for your solution, but unfortunately it doesn't work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. ToolProposal Issues describing a requested change to a Go tool or command-line program. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants