Skip to content

Commit ee35f8e

Browse files
committed
gopls/internal/lsp/source: hovering over broken packages is not an error
Fixes golang/go#64237 Change-Id: I5b326e084c31a7835684fb08491bd71af606fb1c Reviewed-on: https://go-review.googlesource.com/c/tools/+/549117 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 67611a1 commit ee35f8e

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

gopls/internal/lsp/source/hover.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
273273
{
274274
linkMeta = findFileInDeps(snapshot, pkg.Metadata(), declPGF.URI)
275275
if linkMeta == nil {
276-
return protocol.Range{}, nil, bug.Errorf("no metadata for %s", declPGF.URI)
276+
return protocol.Range{}, nil, bug.Errorf("no package data for %s", declPGF.URI)
277277
}
278278

279279
// For package names, we simply link to their imported package.
@@ -283,7 +283,10 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
283283
impID := linkMeta.DepsByPkgPath[PackagePath(pkgName.Imported().Path())]
284284
linkMeta = snapshot.Metadata(impID)
285285
if linkMeta == nil {
286-
return protocol.Range{}, nil, bug.Errorf("no metadata for %s", declPGF.URI)
286+
// Broken imports have fake package paths, so it is not a bug if we
287+
// don't have metadata. As of writing, there is no way to distinguish
288+
// broken imports from a true bug where expected metadata is missing.
289+
return protocol.Range{}, nil, fmt.Errorf("no package data for %s", declPGF.URI)
287290
}
288291
} else {
289292
// For all others, check whether the object is in the package scope, or

gopls/internal/test/marker/doc.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,18 @@ The following markers are supported within marker tests:
151151
TODO(adonovan): in the older marker framework, the annotation asserted
152152
two additional fields (source="compiler", kind="error"). Restore them?
153153
154-
- def(src, dst location): perform a textDocument/definition request at
154+
- def(src, dst location): performs a textDocument/definition request at
155155
the src location, and check the result points to the dst location.
156156
157157
- documentLink(golden): asserts that textDocument/documentLink returns
158158
links as described by the golden file.
159159
160-
- foldingrange(golden): perform a textDocument/foldingRange for the
160+
- foldingrange(golden): performs a textDocument/foldingRange for the
161161
current document, and compare with the golden content, which is the
162162
original source annotated with numbered tags delimiting the resulting
163163
ranges (e.g. <1 kind="..."> ... </1>).
164164
165-
- format(golden): perform a textDocument/format request for the enclosing
165+
- format(golden): performs a textDocument/format request for the enclosing
166166
file, and compare against the named golden file. If the formatting
167167
request succeeds, the golden file must contain the resulting formatted
168168
source. If the formatting request fails, the golden file must contain
@@ -172,9 +172,12 @@ The following markers are supported within marker tests:
172172
textDocument/highlight request at the given src location, which should
173173
highlight the provided dst locations.
174174
175-
- hover(src, dst location, sm stringMatcher): perform a
176-
textDocument/hover at the src location, and checks that the result is
177-
the dst location, with matching hover content.
175+
- hover(src, dst location, sm stringMatcher): performs a textDocument/hover
176+
at the src location, and checks that the result is the dst location, with
177+
matching hover content.
178+
179+
- hovererr(src, sm stringMatcher): performs a textDocument/hover at the src
180+
location, and checks that the error matches the given stringMatcher.
178181
179182
- implementations(src location, want ...location): makes a
180183
textDocument/implementation query at the src location and

gopls/internal/test/marker/marker_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ func (m marker) ctx() context.Context { return m.run.env.Ctx }
231231
// T returns the testing.TB for this mark.
232232
func (m marker) T() testing.TB { return m.run.env.T }
233233

234+
// server returns the LSP server for the marker test run.
235+
func (m marker) editor() *fake.Editor { return m.run.env.Editor }
236+
234237
// server returns the LSP server for the marker test run.
235238
func (m marker) server() protocol.Server { return m.run.env.Editor.Server }
236239

@@ -251,7 +254,7 @@ func (mark marker) path() string {
251254

252255
// mapper returns a *protocol.Mapper for the current file.
253256
func (mark marker) mapper() *protocol.Mapper {
254-
mapper, err := mark.run.env.Editor.Mapper(mark.path())
257+
mapper, err := mark.editor().Mapper(mark.path())
255258
if err != nil {
256259
mark.T().Fatalf("failed to get mapper for current mark: %v", err)
257260
}
@@ -418,6 +421,7 @@ var actionMarkerFuncs = map[string]func(marker){
418421
"format": actionMarkerFunc(formatMarker),
419422
"highlight": actionMarkerFunc(highlightMarker),
420423
"hover": actionMarkerFunc(hoverMarker),
424+
"hovererr": actionMarkerFunc(hoverErrMarker),
421425
"implementation": actionMarkerFunc(implementationMarker),
422426
"incomingcalls": actionMarkerFunc(incomingCallsMarker),
423427
"inlayhints": actionMarkerFunc(inlayhintsMarker),
@@ -1456,10 +1460,6 @@ func highlightMarker(mark marker, src protocol.Location, dsts ...protocol.Locati
14561460
}
14571461
}
14581462

1459-
// hoverMarker implements the @hover marker, running textDocument/hover at the
1460-
// given src location and asserting that the resulting hover is over the dst
1461-
// location (typically a span surrounding src), and that the markdown content
1462-
// matches the golden content.
14631463
func hoverMarker(mark marker, src, dst protocol.Location, sc stringMatcher) {
14641464
content, gotDst := mark.run.env.Hover(src)
14651465
if gotDst != dst {
@@ -1472,6 +1472,11 @@ func hoverMarker(mark marker, src, dst protocol.Location, sc stringMatcher) {
14721472
sc.check(mark, gotMD)
14731473
}
14741474

1475+
func hoverErrMarker(mark marker, src protocol.Location, em stringMatcher) {
1476+
_, _, err := mark.editor().Hover(mark.ctx(), src)
1477+
em.checkErr(mark, err)
1478+
}
1479+
14751480
// locMarker implements the @loc marker. It is executed before other
14761481
// markers, so that locations are available.
14771482
func locMarker(mark marker, loc protocol.Location) protocol.Location { return loc }

gopls/internal/test/marker/testdata/hover/issue64239.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
This test verifies fixes for various issues reported for hover.
2+
3+
-- go.mod --
4+
module golang.org/lsptests
5+
6+
-- issue64239/p.go --
7+
package issue64239
8+
9+
// golang/go#64239: hover fails for objects in the unsafe package.
10+
11+
import "unsafe"
12+
13+
var _ = unsafe.Sizeof(struct{}{}) //@hover("Sizeof", "Sizeof", "`Sizeof` on pkg.go.dev")
14+
15+
-- issue64237/p.go --
16+
package issue64237
17+
18+
// golang/go#64237: hover panics for broken imports.
19+
20+
import "golang.org/lsptests/nonexistant" //@diag("\"golang", re"could not import")
21+
22+
var _ = nonexistant.Value //@hovererr("nonexistant", "no package data")

0 commit comments

Comments
 (0)