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: fix quickfix to add missing import when package name does not match directory name #70755

Open
Seb-C opened this issue Dec 10, 2024 · 5 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

@Seb-C
Copy link

Seb-C commented Dec 10, 2024

gopls version

Build info

golang.org/x/tools/gopls v0.16.1
golang.org/x/tools/gopls@v0.16.1 h1:1hO/dCeUvjEYx3V0rVvCtOkwnpEpqS29paE+Jw4dcAc=
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.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/sync@v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/telemetry@v0.0.0-20240607193123-221703e18637 h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
golang.org/x/text@v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/tools@v0.22.1-0.20240628205440-9c895dd76b34 h1:Kd+Z5Pm6uwYx3T2KEkeHMHUMZxDPb/q6b1m+zEcy62c=
golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
honnef.co/go/tools@v0.4.7 h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.23.0

go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/sebastien/.cache/go-build'
GOENV='/home/sebastien/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/sebastien/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/sebastien/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.23'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.23/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/sebastien/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/sebastien/dev/test-gopls/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-build2151940005=/tmp/go-build -gno-record-gcc-switches'

What did you do?

bar/bar.go:

package notbar
type NotBar struct {}

baz/baz.go:

package baz
type Baz struct {}

foo/foo.go:

package foo
type foo struct {
	bar notbar.NotBar
	baz baz.Baz
}

Position the cursor in foo/foo.go, on the bar property and open the code actions.

What did you see happen?

The Add import (quickfix) option is missing.
screenshot_20241210_225239

What did you expect to see?

The Add import (quickfix) option is available, like it is for baz.
screenshot_20241210_225252

Editor and settings

I am using vim-lsp with those settings:

au User lsp_setup call lsp#register_server({
	\ 'name': 'go',
	\ 'cmd': {server_info->['gopls']},
	\ 'allowlist': ['go'],
\ })

(note: the issue also exists when using coc.nvim).

Logs

* 23:02:01.740 event (+191.518216ms) label= status.code="OK"
* 23:02:01.549 start queued
* 23:02:01.549 end queued (+87.685µs)
* 23:02:01.549 start lsp.Server.codeAction
* 23:02:01.740 end lsp.Server.codeAction (+191.032031ms)
    * 23:02:01.549 start golang.allImportsFixes
    * 23:02:01.740 end golang.allImportsFixes (+190.861599ms)
        * 23:02:01.549 start cache.importsState.runProcessEnvFunc
        * 23:02:01.740 end cache.importsState.runProcessEnvFunc (+190.849156ms)
            * 23:02:01.549 start imports.FixImports
            * 23:02:01.740 end imports.FixImports (+190.777661ms)
                * 23:02:01.558 start imports.addExternalCandidates
                * 23:02:01.740 end imports.addExternalCandidates (+181.587673ms)
                    * 23:02:01.558 start imports.ModuleResolver.scan
                    * 23:02:01.740 end imports.ModuleResolver.scan (+181.493396ms)
    * 23:02:01.740 start cache.forEachPackage packages=0
    * 23:02:01.740 end cache.forEachPackage (+11.221µs) packages=0
    * 23:02:01.740 start cache.ParseGoSrc file="/home/sebastien/dev/test-gopls/foo/foo.go"
    * 23:02:01.740 end cache.ParseGoSrc (+22.131µs) file="/home/sebastien/dev/test-gopls/foo/foo.go"```
@Seb-C Seb-C 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 Dec 10, 2024
@gopherbot gopherbot added this to the Unreleased milestone Dec 10, 2024
@Seb-C
Copy link
Author

Seb-C commented Dec 10, 2024

Also noteworthy: even when the code action is missing, the autocompletion actually knows the symbol:
screenshot_20241210_230842

@findleyr findleyr changed the title x/tools/gopls: no Add import quickfix action when the package and folder names are different x/tools/gopls: add quickfix to add missing import when the package and folder names are different Dec 12, 2024
@findleyr findleyr changed the title x/tools/gopls: add quickfix to add missing import when the package and folder names are different x/tools/gopls: add quickfix to add missing import Dec 12, 2024
@findleyr
Copy link
Member

Does the mismatch between package name and directory actually matter here? I don't think we offer a quick fix to add a missing import, though we could.

This sounds like the inverse operation of #54362, and is related to the manual management of imports, which is not something we support well (we assume folks will rely on automatic import organization).

@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Dec 12, 2024
@Seb-C
Copy link
Author

Seb-C commented Dec 13, 2024

Does the mismatch between package name and directory actually matter here?

Yes, it does, as I showed in my screenshots. This issue is not a request to add a feature, it's a request to fix an inconsistency in an existing feature.

I don't think we offer a quick fix to add a missing import, though we could.

But there is already one. If you search in the source code, you can see that it is implemented:

@findleyr findleyr changed the title x/tools/gopls: add quickfix to add missing import x/tools/gopls: fix quickfix to add missing import when package name does not match directory name Dec 13, 2024
@findleyr
Copy link
Member

Indeed, @Seb-C, sorry for misunderstanding (FWIW, I did search the codebase, in haste, and missed it).

Retitled accordingly.

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

4 participants