internal/lsp/cache: handle nil pointer exception in missing module error

Fixes golang/go#42349

Change-Id: I171495005298b8f55ae75ded5c2f40e457748082
Reviewed-on: https://go-review.googlesource.com/c/tools/+/267118
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Rebecca Stambler 2020-11-02 15:52:24 -05:00
parent 3f6de07740
commit f46e424521
1 changed files with 9 additions and 4 deletions

View File

@ -415,10 +415,15 @@ func directnessError(m *protocol.ColumnMapper, req *modfile.Require, computeEdit
}
func missingModuleError(snapshot source.Snapshot, pm *source.ParsedModule, req *modfile.Require) (source.Error, error) {
start, end := pm.File.Module.Syntax.Span()
rng, err := rangeFromPositions(pm.Mapper, start, end)
if err != nil {
return source.Error{}, err
var rng protocol.Range
// Default to the start of the file if there is no module declaration.
if pm.File != nil && pm.File.Module != nil && pm.File.Module.Syntax != nil {
start, end := pm.File.Module.Syntax.Span()
var err error
rng, err = rangeFromPositions(pm.Mapper, start, end)
if err != nil {
return source.Error{}, err
}
}
args, err := source.MarshalArgs(pm.Mapper.URI, []string{req.Mod.Path + "@" + req.Mod.Version})
if err != nil {