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: automatic imports sometimes pick non-optimal modules #60663

Open
andig opened this issue Jun 7, 2023 · 9 comments
Open

x/tools/gopls: automatic imports sometimes pick non-optimal modules #60663

andig opened this issue Jun 7, 2023 · 9 comments
Labels
gopls/imports 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

@andig
Copy link
Contributor

andig commented Jun 7, 2023

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

@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Jun 7, 2023
@peterldowns
Copy link

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.

@seankhliao seankhliao changed the title gopls: automatic imports sometimes pick non-optimal modules x/tools/gopls: automatic imports sometimes pick non-optimal modules Jun 10, 2023
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jun 10, 2023
@gopherbot gopherbot added this to the Unreleased milestone Jun 10, 2023
@findleyr
Copy link
Contributor

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.

@suzmue suzmue modified the milestones: Unreleased, gopls/later Jun 29, 2023
@pjweinb
Copy link

pjweinb commented Mar 22, 2024

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?

@pjweinb pjweinb added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 22, 2024
@andig
Copy link
Contributor Author

andig commented Mar 22, 2024

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

@pjweinb pjweinb removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 22, 2024
@pjweinb
Copy link

pjweinb commented Mar 22, 2024

Thank you. I see it too.

@andig
Copy link
Contributor Author

andig commented Mar 22, 2024

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

@pjweinb
Copy link

pjweinb commented Mar 28, 2024 via email

@gopherbot
Copy link

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

@peterldowns
Copy link

Thank you @pjweinb !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/imports 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