Skip to content

Commit

Permalink
gopls/internal/lsp/cache: don't record edges to invalid packages
Browse files Browse the repository at this point in the history
The importer assumes that packages have non-empty package path and name.
Enforce this invariant when loading metadata.

Fixes golang/go#60952

Change-Id: I2f4f18e475ba306d93c8b649d19897a584e5ba19
Reviewed-on: https://go-review.googlesource.com/c/tools/+/505219
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
(cherry picked from commit c8278fe)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/505222
  • Loading branch information
findleyr committed Jun 22, 2023
1 parent 5056bc8 commit 537c689
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go/packages/golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,9 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
// Temporary work-around for golang/go#39986. Parse filenames out of
// error messages. This happens if there are unrecoverable syntax
// errors in the source, so we can't match on a specific error message.
//
// TODO(rfindley): remove this heuristic, in favor of considering
// InvalidGoFiles from the list driver.
if err := p.Error; err != nil && state.shouldAddFilenameFromError(p) {
addFilenameFromPos := func(pos string) bool {
split := strings.Split(pos, ":")
Expand Down
14 changes: 13 additions & 1 deletion gopls/internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,21 @@ func buildMetadata(updates map[PackageID]*source.Metadata, pkg *packages.Package
continue
}

buildMetadata(updates, imported, loadDir, false) // only top level packages can be standalone

// Don't record edges to packages with no name, as they cause trouble for
// the importer (golang/go#60952).
//
// However, we do want to insert these packages into the update map
// (buildMetadata above), so that we get type-checking diagnostics for the
// invalid packages.
if imported.Name == "" {
depsByImpPath[importPath] = "" // missing
continue
}

depsByImpPath[importPath] = PackageID(imported.ID)
depsByPkgPath[PackagePath(imported.PkgPath)] = PackageID(imported.ID)
buildMetadata(updates, imported, loadDir, false) // only top level packages can be standalone
}
m.DepsByImpPath = depsByImpPath
m.DepsByPkgPath = depsByPkgPath
Expand Down

0 comments on commit 537c689

Please sign in to comment.