Repository: go
Issue: x/tools/gopls: fix quickfix to add missing import when package name does not match directory name
Labels: gopls,Tools,gopls/imports

### 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

```shell
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:
```go
package notbar
type NotBar struct {}
```

baz/baz.go:
```go
package baz
type Baz struct {}
```

foo/foo.go:
```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](https://github.com/user-attachments/assets/5672a6aa-021c-43f4-ae33-fa478cb6b51c)


### What did you expect to see?

The `Add import` (quickfix) option is available, like it is for `baz`.
![screenshot_20241210_225252](https://github.com/user-attachments/assets/e16acdd9-3801-450a-9859-e1124cba47d3)


### Editor and settings

I am using `vim-lsp` with those settings:
```vimscript
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 end textDocument/codeAction (+191.519598ms) method="textDocument/codeAction" direction="in" id="#6"
* 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"```