Skip to content

Commit 2f73c61

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/golang: avoid crash in lookupDocLinkSymbol
Avoid a theoretical crash in lookupDocLinkSymbol (nee lookupObjectByName), with a refining bug report. For golang/go#69616 Change-Id: I5305583c541eb54bb89bed1dc680c9b0e93b90d9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/633095 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent ef3d603 commit 2f73c61

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

gopls/internal/golang/comment.go

+14
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212
"go/doc/comment"
1313
"go/token"
1414
"go/types"
15+
"slices"
1516
"strings"
1617

1718
"golang.org/x/tools/gopls/internal/cache"
1819
"golang.org/x/tools/gopls/internal/cache/parsego"
1920
"golang.org/x/tools/gopls/internal/protocol"
2021
"golang.org/x/tools/gopls/internal/settings"
2122
"golang.org/x/tools/gopls/internal/util/astutil"
23+
"golang.org/x/tools/gopls/internal/util/bug"
2224
"golang.org/x/tools/gopls/internal/util/safetoken"
2325
)
2426

@@ -154,6 +156,18 @@ func lookupDocLinkSymbol(pkg *cache.Package, pgf *parsego.File, name string) typ
154156
// Try treating the prefix as a package name,
155157
// allowing for non-renaming and renaming imports.
156158
fileScope := pkg.TypesInfo().Scopes[pgf.File]
159+
if fileScope == nil {
160+
// This is theoretically possible if pgf is a GoFile but not a
161+
// CompiledGoFile. However, we do not know how to produce such a package
162+
// without using an external GoPackagesDriver.
163+
// See if this is the source of golang/go#70635
164+
if slices.Contains(pkg.CompiledGoFiles(), pgf) {
165+
bug.Reportf("missing file scope for compiled file")
166+
} else {
167+
bug.Reportf("missing file scope for non-compiled file")
168+
}
169+
return nil
170+
}
157171
pkgname, ok := fileScope.Lookup(prefix).(*types.PkgName) // ok => prefix is imported name
158172
if !ok {
159173
// Handle renaming import, e.g.

0 commit comments

Comments
 (0)