1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00

internal/lsp/cache: fix GOPATH vendoring

We treat package IDs and import paths as semi-interchangeable, which is
wrong when GOPATH vendoring is in use. The only place it hurts us is
during import resolution, which is fixed here. We should always have the
package loaded, so it's just a matter of finding it by searching each
possible vendor location.

Fixes golang/go#36155.

Change-Id: If789092d16fa3d3294b6d8a2bcb980264506c161
Reviewed-on: https://go-review.googlesource.com/c/tools/+/215904
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-01-22 19:23:30 -05:00
parent ce9bf563f5
commit 5ecc1643ff
2 changed files with 24 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"go/ast"
"go/token"
"go/types"
"path"
"sort"
"sync"
@ -309,6 +310,24 @@ func typeCheck(ctx context.Context, fset *token.FileSet, m *metadata, mode sourc
},
Importer: importerFunc(func(pkgPath string) (*types.Package, error) {
dep := deps[packagePath(pkgPath)]
if dep == nil {
// We may be in GOPATH mode, in which case we need to check vendor dirs.
searchDir := path.Dir(pkg.PkgPath())
for {
vdir := packagePath(path.Join(searchDir, "vendor", pkgPath))
if vdep := deps[vdir]; vdep != nil {
dep = vdep
break
}
// Search until Dir doesn't take us anywhere new, e.g. "." or "/".
next := path.Dir(searchDir)
if searchDir == next {
break
}
searchDir = next
}
}
if dep == nil {
return nil, errors.Errorf("no package for import %s", pkgPath)
}

View File

@ -42,6 +42,9 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) ([]*metadata
for _, scope := range scopes {
switch scope := scope.(type) {
case []packagePath:
// The only time we pass package paths is when we're doing a
// partial workspace load. In those cases, the paths came back from
// go list and should already be GOPATH-vendorized when appropriate.
for _, p := range scope {
query = append(query, string(p))
}
@ -59,6 +62,8 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) ([]*metadata
q = "./..."
}
query = append(query, q)
default:
panic(fmt.Sprintf("unknown scope type %T", scope))
}
}
ctx, done := trace.StartSpan(ctx, "cache.view.load", telemetry.Query.Of(query))