Skip to content

Commit 155320d

Browse files
committed
gopls/internal/lsp/work: don't crash when hovering over broken mod dir
Guard against modfiles without a module directive. Fixes golang/go#60821 Change-Id: I91661b2abea916db55a81323fc26dcd4f2f0516c Reviewed-on: https://go-review.googlesource.com/c/tools/+/503436 gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent b71392a commit 155320d

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

gopls/internal/lsp/cache/mod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func (s *snapshot) matchErrorToModule(ctx context.Context, pm *source.ParsedModu
400400
// Show the error on the module declaration, if one exists, or
401401
// just the first line of the file.
402402
var start, end int
403-
if pm.File.Module != nil {
403+
if pm.File.Module != nil && pm.File.Module.Syntax != nil {
404404
syntax := pm.File.Module.Syntax
405405
start, end = syntax.Start.Byte, syntax.End.Byte
406406
}

gopls/internal/lsp/mod/diagnostics.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,13 @@ func ModVulnerabilityDiagnostics(ctx context.Context, snapshot source.Snapshot,
331331
// TODO(hyangah): place this diagnostic on the `go` directive or `toolchain` directive
332332
// after https://go.dev/issue/57001.
333333
const diagnoseStdLib = false
334-
if diagnoseStdLib {
334+
335+
// If diagnosing the stdlib, add standard library vulnerability diagnostics
336+
// on the module declaration.
337+
//
338+
// Only proceed if we have a valid module declaration on which to position
339+
// the diagnostics.
340+
if diagnoseStdLib && pm.File.Module != nil && pm.File.Module.Syntax != nil {
335341
// Add standard library vulnerabilities.
336342
stdlibVulns := vulnsByModule["stdlib"]
337343
if len(stdlibVulns) == 0 {

gopls/internal/lsp/work/hover.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func Hover(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle,
5252
if err != nil {
5353
return nil, fmt.Errorf("getting modfile handle: %w", err)
5454
}
55+
if pm.File.Module == nil {
56+
return nil, fmt.Errorf("modfile has no module declaration")
57+
}
5558
mod := pm.File.Module.Mod
5659

5760
// Get the range to highlight for the hover.

gopls/internal/regtest/misc/hover_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,23 @@ func TestHoverLinknameDirective(t *testing.T) {
415415
}
416416
})
417417
}
418+
419+
func TestHoverGoWork_Issue60821(t *testing.T) {
420+
const files = `
421+
-- go.work --
422+
go 1.19
423+
424+
use (
425+
moda
426+
modb
427+
)
428+
-- moda/go.mod --
429+
430+
`
431+
Run(t, files, func(t *testing.T, env *Env) {
432+
env.OpenFile("go.work")
433+
// Neither of the requests below should crash gopls.
434+
_, _, _ = env.Editor.Hover(env.Ctx, env.RegexpSearch("go.work", "moda"))
435+
_, _, _ = env.Editor.Hover(env.Ctx, env.RegexpSearch("go.work", "modb"))
436+
})
437+
}

0 commit comments

Comments
 (0)