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: organizeImports chooses wrong import with 20+ peer package imports #67022

Open
guettli opened this issue Apr 24, 2024 · 2 comments
Labels
gopls/imports NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@guettli
Copy link

guettli commented Apr 24, 2024

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.22.1 linux/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
❯ gopls -v version
Build info
----------
golang.org/x/tools/gopls v0.15.3
    golang.org/x/tools/gopls@v0.15.3 h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o=
    github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/mod@v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
    golang.org/x/sync@v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
    golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78 h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4=
    golang.org/x/text@v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
    golang.org/x/tools@v0.18.1-0.20240412183611-d92ae0781217 h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak=
    golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU=
    honnef.co/go/tools@v0.4.6 h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8=
    mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
    mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.22.1
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
❯ code -v                                                                                                     
1.88.0
5c3e652f63e798a5ac2f31ffd0d863669328dc4c
x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.41.3
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
# Tools Configuration


## Environment

GOBIN: undefined
toolsGopath: 
gopath: /home/guettli/go
GOROOT: /home/linuxbrew/.linuxbrew/Cellar/go/1.22.1/libexec
PATH: /home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/guettli/scripts:/home/guettli/.krew/bin:/home/guettli/bin:/home/guettli/go/bin:/home/guettli/syself/dotfiles/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/guettli/.local/bin:/home/guettli/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/guettli/scripts:/home/guettli/.krew/bin:/home/guettli/bin:/home/guettli/go/bin:/home/guettli/syself/dotfiles/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/guettli/.local/bin:/home/guettli/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

## Tools

	go:	/home/linuxbrew/.linuxbrew/bin/go: go version go1.22.1 linux/amd64

	gopls:	/home/guettli/go/bin/gopls	(version: v0.15.3 built with go: go1.22.1)
	gotests:	/home/guettli/go/bin/gotests	(version: v1.6.0 built with go: go1.22.1)
	gomodifytags:	not installed
	impl:	not installed
	goplay:	not installed
	dlv:	/home/guettli/go/bin/dlv	(version: v1.22.1 built with go: go1.22.1)
	golangci-lint:	/home/linuxbrew/.linuxbrew/bin/golangci-lint	(version: unknown - )

## Go env

Workspace Folder (csi-topolvm): /home/guettli/syself/csi-topolvm

	GO111MODULE=''
	GOARCH='amd64'
	GOBIN=''
	GOCACHE='/home/guettli/.cache/go-build'
	GOENV='/home/guettli/.config/go/env'
	GOEXE=''
	GOEXPERIMENT=''
	GOFLAGS=''
	GOHOSTARCH='amd64'
	GOHOSTOS='linux'
	GOINSECURE=''
	GOMODCACHE='/home/guettli/go/pkg/mod'
	GONOPROXY=''
	GONOSUMDB=''
	GOOS='linux'
	GOPATH='/home/guettli/go'
	GOPRIVATE=''
	GOPROXY='https://proxy.golang.org,direct'
	GOROOT='/home/linuxbrew/.linuxbrew/Cellar/go/1.22.1/libexec'
	GOSUMDB='sum.golang.org'
	GOTMPDIR=''
	GOTOOLCHAIN='auto'
	GOTOOLDIR='/home/linuxbrew/.linuxbrew/Cellar/go/1.22.1/libexec/pkg/tool/linux_amd64'
	GOVCS=''
	GOVERSION='go1.22.1'
	GCCGO='gccgo'
	GOAMD64='v1'
	AR='ar'
	CC='cc'
	CXX='c++'
	CGO_ENABLED='1'
	GOMOD='/home/guettli/syself/csi-topolvm/go.mod'
	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-build2362649701=/tmp/go-build -gno-record-gcc-switches'
	

Share the Go related settings you have added/edited

  "[go]": {
        "breadcrumbs.showEnums": true,
        "editor.insertSpaces": false,
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        },
        "editor.suggest.snippetsPreventQuickSuggestions": false,
    },
    "go.lintTool": "golangci-lint",
    "go.lintFlags": [
        "--fast"
    ],

    "gopls": {
        "importShortcut": "Definition",
        "formatting.gofumpt": true,
    },  

Describe the bug

image

I type client.Client and "client" is up to now not known.

I hit ctrl-s, and somehow this import gets added:

"github.com/docker/docker/client"

This makes no sense. Nowhere in my code base do I import that.

I want that:

"sigs.k8s.io/controller-runtime/pkg/client"

There are more than 20 imports of that package.

Why does the "auto-add import on save" feature not check the list of common imports of my code?

@findleyr
Copy link
Contributor

Thanks, this is (yet another) problem with goimports heuristics.

We are working on goimports now, and I'll add this to the queue of known issues.

(I was just talking with @pjweinb this morning about how we need to guarantee that goimports prefers existing imports, something it surprisingly doesn't do correctly, as you've noticed).

@findleyr findleyr changed the title Auto-add imports on save guess without overview x/tools/gopls: organizeImports chooses wrong import with 20+ peer package imports Apr 24, 2024
@findleyr findleyr transferred this issue from golang/vscode-go Apr 24, 2024
@findleyr findleyr added this to the gopls/v0.17.0 milestone Apr 24, 2024
@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 24, 2024
@findleyr findleyr added gopls/imports and removed gopls Issues related to the Go language server, gopls. labels Apr 24, 2024
@meling
Copy link

meling commented Apr 25, 2024

I think this is a duplicate of #61208.

@joedian joedian added the NeedsFix The path to resolution is known, but the work has not been done. label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/imports NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants