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: importing wrong package "golang.org/x/exp/slices" #63641

Closed
jgardona opened this issue Oct 20, 2023 · 10 comments
Closed

x/tools/gopls: importing wrong package "golang.org/x/exp/slices" #63641

jgardona opened this issue Oct 20, 2023 · 10 comments
Labels
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

@jgardona
Copy link

I don't know exactly if this is an issue of gopls, but I think its good to post it here.
When using slices package, the lsp will always import the wrong go package
"golang.org/x/exp/slices", instead of the correct package "slices".

gopls version

Build info
----------
golang.org/x/tools/gopls v0.13.2
    golang.org/x/tools/gopls@v0.13.2 h1:Pyvx6MKvatbX3zzZmdGiFRfQZl0ohPlt2sFxO/5j6Ro=
    github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp@v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
    golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/mod@v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
    golang.org/x/sync@v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
    golang.org/x/sys@v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
    golang.org/x/text@v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
    golang.org/x/tools@v0.11.2-0.20230810185051-cc6b5804b8cf h1:Oush7UwPamr2/iNeNFBuNFj89YyHn0YY69EKDdvANnk=
    golang.org/x/vuln@v0.0.0-20230110180137-6ad3e3d07815 h1:A9kONVi4+AnuOr1dopsibH6hLi1Huy54cbeJxnq4vmU=
    honnef.co/go/tools@v0.4.2 h1:6qXr+R5w+ktL5UkwEbPp+fEvfyoMPche6GkOpGHZcLc=
    mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.21.3

go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jgardona/snap/alacritty/common/.cache/go-build'
GOENV='/home/jgardona/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jgardona/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jgardona/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/jgardona/Bin/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/jgardona/Bin/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build251818100=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Use slices package and wait for the gopls autocomplete works

What did you expect to see?

Import "slices" package

What did you see instead?

It imports "golang.org/x/exp/slices" package

Editor and settings

  • vscode info
    Version: 1.83.1
    Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc
    Date: 2023-10-10T23:45:31.402Z
    Electron: 25.8.4
    ElectronBuildId: 24154031
    Chromium: 114.0.5735.289
    Node.js: 18.15.0
    V8: 11.4.183.29-electron.0
    OS: Linux x64 6.5.0-9-generic snap

  • no settings

image

@jgardona jgardona added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Oct 20, 2023
@gopherbot gopherbot added this to the Unreleased milestone Oct 20, 2023
@jgardona jgardona changed the title x/tools/gopls: importing wrong package slinex x/tools/gopls: importing wrong package "golang.org/x/exp/slices" Oct 20, 2023
@seankhliao
Copy link
Member

what's in your go.mod?

@jgardona
Copy link
Author

jgardona commented Oct 20, 2023

what's in your go.mod?

❯ cat ../../go/example/go.mod
module example

go 1.21.3

@danp
Copy link
Contributor

danp commented Oct 20, 2023

@jgardona did you end up with exp/slices as part of a completion or as part of "organize imports" (possibly on save)?

In other words, was it from doing something like:

func main() {
  slic<> // cursor at <> and golang.org/x/exp/slices suggested
}

or:

package main

func main() {
	slices.Sort([]int{5, 4, 3}) // hit save after typing this and `import "golang.org/x/exp/slices"` appears above
}

@jgardona
Copy link
Author

@danp Hello. Good afternoon.

Exactly. I typed "slices.", then hit enter and "golang.org/x/exp/slices" was imported.

@danp
Copy link
Contributor

danp commented Oct 22, 2023

I see you're using golang.org/x/tools/gopls v0.13.2.

I believe the issue is that gopls embeds a list of stdlib packages and their exported symbols. That list is used by the completion bits to understand what stdlib packages are available. You can see the version of that list gopls v0.13.2 uses here (which, because gopls is its own module and requires tools, is dependent on what go.mod says here). There's no entry for the stdlib slices package since list was generated using info from Go 1.20 (as mentioned in the commit message).

To get a version of gopls that uses the more recent list generated from Go 1.21 (which does include slices) you'd need to try a gopls v0.14.0 prerelease, such as gopls/v0.14.0-pre.4.

You should be able to try it with the example go install command there. If you're unable to try it, I think you'll need to wait for a proper v0.14.0 release.

And hopefully someone from the gopls team will correct me if I've gotten something wrong!

@jgardona
Copy link
Author

I'll wait gopls 0.14.0 and see what will be the output.

@hyangah
Copy link
Contributor

hyangah commented Oct 24, 2023

gopls v0.14.0-pre.5 is available for testing. At least, it seems to work for me and suggests slices as the very first one. (the second one is the one in golang.org/x/exp/slices, but my project already depends on golang.org/x/exp). And file save imports "slices" automatically.

Screenshot 2023-10-24 at 5 00 18 PM

@findleyr
Copy link
Contributor

gopls v0.14.0 is out now. @jgardona please let us know if this resolves your problem.

@jgardona
Copy link
Author

gopls v0.14.0 is out now. @jgardona please let us know if this resolves your problem.

confirmed here it was fixed. Thank you all 🙂

@jgardona
Copy link
Author

will close as it was fixed in 0.14

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. 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