Skip to content

Commit

Permalink
gopls/internal/golang: avoid crash in lookupDocLinkSymbol
Browse files Browse the repository at this point in the history
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 <adonovan@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr authored and gopherbot committed Dec 2, 2024
1 parent ef3d603 commit 2f73c61
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gopls/internal/golang/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
"go/doc/comment"
"go/token"
"go/types"
"slices"
"strings"

"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/cache/parsego"
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/settings"
"golang.org/x/tools/gopls/internal/util/astutil"
"golang.org/x/tools/gopls/internal/util/bug"
"golang.org/x/tools/gopls/internal/util/safetoken"
)

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

0 comments on commit 2f73c61

Please sign in to comment.