Skip to content

Commit 76e6044

Browse files
adonovangopherbot
authored andcommitted
[gopls-release-branch.0.16] gopls/internal/golang: strip @v1.2.3 suffix from pkgdoc URLs
The package path in a /pkg URL does not want a module version suffix: the view specifies the versions of all packages. Remove them. Fixes golang/go#68116 Change-Id: Icbe7a6e7346d12456724d005fe8f755872657055 Reviewed-on: https://go-review.googlesource.com/c/tools/+/594556 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> (cherry picked from commit 1e6c1e2) Reviewed-on: https://go-review.googlesource.com/c/tools/+/595563 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 911cda3 commit 76e6044

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

gopls/internal/golang/hover.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type hoverJSON struct {
7070

7171
// LinkPath is the pkg.go.dev link for the given symbol.
7272
// For example, the "go/ast" part of "pkg.go.dev/go/ast#Node".
73+
// It may have a module version suffix "@v1.2.3".
7374
LinkPath string `json:"linkPath"`
7475

7576
// LinkAnchor is the pkg.go.dev link anchor for the given symbol.
@@ -98,6 +99,7 @@ type hoverJSON struct {
9899
}
99100

100101
// Hover implements the "textDocument/hover" RPC for Go files.
102+
// It may return nil even on success.
101103
//
102104
// If pkgURL is non-nil, it should be used to generate doc links.
103105
func Hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position, pkgURL func(path PackagePath, fragment string) protocol.URI) (*protocol.Hover, error) {
@@ -200,7 +202,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
200202
if hoverRange == nil {
201203
hoverRange = &rng
202204
}
203-
return *hoverRange, hoverJSON, nil
205+
return *hoverRange, hoverJSON, nil // (hoverJSON may be nil)
204206
}
205207
}
206208
// Handle hovering over (non-import-path) literals.
@@ -1159,7 +1161,8 @@ func formatLink(h *hoverJSON, options *settings.Options, pkgURL func(path Packag
11591161
var url protocol.URI
11601162
var caption string
11611163
if pkgURL != nil { // LinksInHover == "gopls"
1162-
url = pkgURL(PackagePath(h.LinkPath), h.LinkAnchor)
1164+
path, _, _ := strings.Cut(h.LinkPath, "@") // remove optional module version suffix
1165+
url = pkgURL(PackagePath(path), h.LinkAnchor)
11631166
caption = "in gopls doc viewer"
11641167
} else {
11651168
if options.LinkTarget == "" {

gopls/internal/test/integration/fake/editor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ func (e *Editor) EditResolveSupport() (bool, error) {
15951595
}
15961596

15971597
// Hover triggers a hover at the given position in an open buffer.
1598+
// It may return (nil, zero) if no symbol was selected.
15981599
func (e *Editor) Hover(ctx context.Context, loc protocol.Location) (*protocol.MarkupContent, protocol.Location, error) {
15991600
if err := e.checkBufferLocation(loc); err != nil {
16001601
return nil, protocol.Location{}, err
@@ -1608,7 +1609,7 @@ func (e *Editor) Hover(ctx context.Context, loc protocol.Location) (*protocol.Ma
16081609
return nil, protocol.Location{}, fmt.Errorf("hover: %w", err)
16091610
}
16101611
if resp == nil {
1611-
return nil, protocol.Location{}, nil
1612+
return nil, protocol.Location{}, nil // e.g. no selected symbol
16121613
}
16131614
return &resp.Contents, protocol.Location{URI: loc.URI, Range: resp.Range}, nil
16141615
}

gopls/internal/test/integration/misc/hover_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,53 @@ func main() {
553553
})
554554
}
555555
}
556+
557+
func TestHoverInternalLinksIssue68116(t *testing.T) {
558+
// Links for the internal viewer should not include a module version suffix:
559+
// the package path and the view are an unambiguous key; see #68116.
560+
561+
const proxy = `
562+
-- [email protected]/go.mod --
563+
module example.com
564+
565+
go 1.12
566+
567+
-- [email protected]/a/a.go --
568+
package a
569+
570+
// F is a function.
571+
func F()
572+
`
573+
574+
const mod = `
575+
-- go.mod --
576+
module main
577+
578+
go 1.12
579+
580+
require example.com v1.2.3
581+
582+
-- main.go --
583+
package main
584+
585+
import "example.com/a"
586+
587+
func main() {
588+
a.F()
589+
}
590+
`
591+
WithOptions(
592+
ProxyFiles(proxy),
593+
Settings{"linksInHover": "gopls"},
594+
WriteGoSum("."),
595+
).Run(t, mod, func(t *testing.T, env *Env) {
596+
env.OpenFile("main.go")
597+
got, _ := env.Hover(env.RegexpSearch("main.go", "F"))
598+
const wantRE = "\\[`a.F` in gopls doc viewer\\]\\(http://127.0.0.1:[0-9]+/gopls/[^/]+/pkg/example.com\\?view=[0-9]+#F\\)" // no version
599+
if m, err := regexp.MatchString(wantRE, got.Value); err != nil {
600+
t.Fatalf("bad regexp in test: %v", err)
601+
} else if !m {
602+
t.Fatalf("hover output does not match %q; got:\n\n%s", wantRE, got.Value)
603+
}
604+
})
605+
}

gopls/internal/test/integration/wrappers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func (e *Env) GetQuickFixes(path string, diagnostics []protocol.Diagnostic) []pr
233233
}
234234

235235
// Hover in the editor, calling t.Fatal on any error.
236+
// It may return (nil, zero) even on success.
236237
func (e *Env) Hover(loc protocol.Location) (*protocol.MarkupContent, protocol.Location) {
237238
e.T.Helper()
238239
c, loc, err := e.Editor.Hover(e.Ctx, loc)

0 commit comments

Comments
 (0)