Skip to content

x/tools/gopls: organizeImports chooses wrong import with 20+ peer package imports #67022

@guettli

Description

@guettli

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/[email protected] h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o=
    github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/[email protected] h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
    golang.org/x/[email protected] h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
    golang.org/x/[email protected] h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4=
    golang.org/x/[email protected] h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
    golang.org/x/[email protected] h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak=
    golang.org/x/[email protected] h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU=
    honnef.co/go/[email protected] h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8=
    mvdan.cc/[email protected] h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
    mvdan.cc/xurls/[email protected] 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.ToolsThis label describes issues relating to any tools in the x/tools repository.gopls/imports

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions