From 7be0a674c9fc78b6627a534052066b112b66be5d Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 6 Jan 2020 13:14:29 -0500 Subject: [PATCH] internal/lsp: fix minor bug in the PackageHandle function Test variants and test mains can result in multiple packages being loaded for a single ID. Handle this case in the PackageHandle function instead of returning an error. Change-Id: Ic0024c5bded162a3e78a9cdcb9566449f3683e35 Reviewed-on: https://go-review.googlesource.com/c/tools/+/213457 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick TryBot-Result: Gobot Gobot --- internal/lsp/cache/snapshot.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 2d984fa3c85..b2d8e128bcb 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -95,10 +95,19 @@ func (s *snapshot) PackageHandle(ctx context.Context, pkgID string) (source.Pack if err != nil { return nil, err } - if len(phs) > 1 { - return nil, errors.Errorf("more than one package for %s", id) + var result *packageHandle + for _, ph := range phs { + if ph.m.id == id { + if result != nil { + return nil, errors.Errorf("multiple package handles for the same ID: %s", id) + } + result = ph + } } - return phs[0], nil + if result == nil { + return nil, errors.Errorf("no PackageHandle for %s", id) + } + return result, nil } func (s *snapshot) packageHandles(ctx context.Context, scope interface{}, meta []*metadata) ([]*packageHandle, error) {