What version of Go are you using (go version)?

$ go version
go version go1.20.5 darwin/arm64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/andig/Library/Caches/go-build"
GOENV="/Users/andig/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/andig/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/andig/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.20.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.20.5/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/Users/andig/htdocs/evcc/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sv/rs_453y57xj86xsbz3kw1mbc0000gn/T/go-build1707488893=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.20.5 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.20.5
uname -v: Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:41 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8103
ProductName:        macOS
ProductVersion:     13.3.1
ProductVersionExtra:    (a)
BuildVersion:       22E772610a
lldb --version: lldb-1403.0.17.64
Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)

What did you do?

  1. Create an application e.g. importing "github.com/coreos/v3/go-oidc/oidc"
  2. Write/tidy the go.mod
  3. Create new Go file in Vscode and try to use the oidc.ScopeOpenID constant

What did you expect to see?

Package "github.com/coreos/v3/go-oidc/oidc" imported by gopls

What did you see instead?

Package "github.com/coreos/go-oidc" imported by gopls

I would have expected the already-imported package to be used, instead gopls seems to pick any package that it might find suitable.

/cc @adonovan

Comment From: peterldowns

Related to this — is it possible to configure the import-suggester to never suggest certain packages? I read the settings documentation and could not find a relevant setting, but I am not confident I'm looking for the right term. Between vscode settings, lsp settings, and the formatter (gofumpt is what I think I'm using?) I also don't know where this setting should exist.

Comment From: findleyr

Thanks for the report.

I'm collecting goimports related issues into a gopls/imports label. There are numerous such issues, and I think we need to revisit them holistically.

Comment From: pjweinb

Hi. I failed to reproduce this behavior. I did 'git clone https://github.com/coreos/go-oidc.git', created a file in the same directory as the .git file with 'package main; const y = idc.ScopeOpenID' and saved the file. gopls inserted 'import "github.com/coreos/go-oidc/v3/oidc"' which I think is the right answer. Running go mod tidy before or after didn't seem to make any difference. I'm using gopls v0.15.2.

Does the error still happen for you?

Comment From: andig

I can still repro this:

mkdir test && cd test
go mod init foo
go get github.com/coreos/go-oidc/v3

then create main.go and add:

package main

func main() {
    _ = oidc.ScopeOpenID
}

then save. Imports:

import "github.com/coreos/go-oidc"

Running:

Build info
----------
golang.org/x/tools/gopls v0.15.2
    golang.org/x/tools/gopls@v0.15.2 h1:4JKt4inO8JaFW3l/Fh9X1k/5JQn+iUOpdc4/Lpi0mOs=
    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.20240311201521-78fbdeb61842 h1:No0LMXYFkp3j4oEsPdtY8LUQz33gu79Rm9DE+izMeGQ=
    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

Screenshot 2024-03-22 at 19 33 37

Comment From: pjweinb

Thank you. I see it too.

Comment From: andig

Would be really great to get this fixed. Quite annoying when it strikes.

Comment From: pjweinb

I see what is going wrong. But I don't yet know how to fix it without breaking other things.

On Fri, Mar 22, 2024 at 4:29 PM andig @.***> wrote:

Would be really great to get this fixed. Quite annoying when it strikes.

— Reply to this email directly, view it on GitHub https://github.com/golang/go/issues/60663#issuecomment-2015859325, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJIAI7XEQZVP3W5IIMLCVTYZSIBNAVCNFSM6AAAAAAY6DV642VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJVHA2TSMZSGU . You are receiving this because you commented.Message ID: @.***>

Comment From: gopherbot

Change https://go.dev/cl/582557 mentions this issue: internal/imports: recognize major versions and use go.mod

Comment From: peterldowns

Thank you @pjweinb !

Comment From: pjweinb

I believe this is fixed in the next gopls release.

Comment From: pjweinb

I propose closing this issue.

Comment From: andig

Both examples above are working now, seems fixed- thank you!