golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.21k stars 17.7k forks source link

x/tools/gopls: gopls doc viewer failed with "package not found" error #70453

Closed hyangah closed 1 day ago

hyangah commented 2 days ago

gopls version

Build info

golang.org/x/tools/gopls v0.17.0-pre.2 golang.org/x/tools/gopls@v0.17.0-pre.2 h1:y6wuA4lucYh1D/La5Rs6/aaR1YDLuT1UPygLfmKyBuI= github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs= github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ= golang.org/x/mod@v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= golang.org/x/sync@v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= golang.org/x/telemetry@v0.0.0-20241106142447-58a1122356f5 h1:TCDqnvbBsFapViksHcHySl/sW4+rTGNIAoJJesHRuMM= golang.org/x/text@v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= golang.org/x/tools@v0.27.1-0.20241114151729-21afbf487e5c h1:3ufSKdb6bs9yNyF0YIdrRQjzlObIct/11gv96ANGmZA= golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I= honnef.co/go/tools@v0.5.1 h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I= mvdan.cc/gofumpt@v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU= mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8= go: go1.23.3

go env

go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/hakim/Library/Caches/go-build'
GOENV='/Users/hakim/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/hakim/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/hakim/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.6'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/hakim/projects/build/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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/5p/zn7ykc111kn3lm09h_47mz2w001py5/T/go-build2749575289=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

"gopls": {
        "ui.documentation.linksInHover": "gopls",
}

What did you see happen?

"package not found" on browser

What did you expect to see?

Package doc

Editor and settings

"gopls": {
        "ui.documentation.linksInHover": "gopls",
}

VS Code

Logs

No response

gabyhelp commented 2 days ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

hyangah commented 2 days ago

It's possible that #68223 can be the same issue. The issue is present in gopls v0.16 too.

adonovan commented 2 days ago

Thanks, I can reproduce the problem. The URL is lacking the final /github segment of the package path. If you instead use Source Action > Browse Documentation, it computes the correct URL. Will investigate.

adonovan commented 2 days ago

Ah, the logic in golang.formatLink is to blame:

    if pkgURL != nil { // LinksInHover == "gopls"
        path, _, _ := strings.Cut(h.LinkPath, "@") // remove optional module version suffix
        url = pkgURL(PackagePath(path), h.LinkAnchor)

LinkPath is github.com/google/go-github/v48@v48.1.0/github, and the Cut yields github.com/google/go-github/v48.

It should probably use something like https://pkg.go.dev/golang.org/x/mod/module#SplitPathVersion to parse the path.

findleyr commented 2 days ago

Seems like an easy fix, thanks Alan for investigating. I'll put this in the v0.17.0 milestone, but we can of course fix it later.

gopherbot commented 1 day ago

Change https://go.dev/cl/630077 mentions this issue: gopls/internal/golang: fix gopls hover doc link