Skip to content

Commit 537c689

Browse files
committed
gopls/internal/lsp/cache: don't record edges to invalid packages
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 <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> (cherry picked from commit c8278fe) Reviewed-on: https://go-review.googlesource.com/c/tools/+/505222
1 parent 5056bc8 commit 537c689

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

go/packages/golist.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
671671
// Temporary work-around for golang/go#39986. Parse filenames out of
672672
// error messages. This happens if there are unrecoverable syntax
673673
// errors in the source, so we can't match on a specific error message.
674+
//
675+
// TODO(rfindley): remove this heuristic, in favor of considering
676+
// InvalidGoFiles from the list driver.
674677
if err := p.Error; err != nil && state.shouldAddFilenameFromError(p) {
675678
addFilenameFromPos := func(pos string) bool {
676679
split := strings.Split(pos, ":")

gopls/internal/lsp/cache/load.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,21 @@ func buildMetadata(updates map[PackageID]*source.Metadata, pkg *packages.Package
528528
continue
529529
}
530530

531+
buildMetadata(updates, imported, loadDir, false) // only top level packages can be standalone
532+
533+
// Don't record edges to packages with no name, as they cause trouble for
534+
// the importer (golang/go#60952).
535+
//
536+
// However, we do want to insert these packages into the update map
537+
// (buildMetadata above), so that we get type-checking diagnostics for the
538+
// invalid packages.
539+
if imported.Name == "" {
540+
depsByImpPath[importPath] = "" // missing
541+
continue
542+
}
543+
531544
depsByImpPath[importPath] = PackageID(imported.ID)
532545
depsByPkgPath[PackagePath(imported.PkgPath)] = PackageID(imported.ID)
533-
buildMetadata(updates, imported, loadDir, false) // only top level packages can be standalone
534546
}
535547
m.DepsByImpPath = depsByImpPath
536548
m.DepsByPkgPath = depsByPkgPath

0 commit comments

Comments
 (0)