mirror of
https://github.com/golang/go
synced 2024-11-19 00:44:40 -07:00
30cae5f2fb
This change will surface errors that come from the mod package. It will handle incorrect usages, invalid directives, and other errors that occur when parsing go.mod files. Updates golang/go#31999 Change-Id: Icd817c02a4b656b2a71914ee60be4dbe2bea062d Reviewed-on: https://go-review.googlesource.com/c/tools/+/213779 Run-TryBot: Rohan Challa <rohan@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"go/ast"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
type builtinPkg struct {
|
|
pkg *ast.Package
|
|
files []source.ParseGoHandle
|
|
}
|
|
|
|
func (b *builtinPkg) Lookup(name string) *ast.Object {
|
|
if b == nil || b.pkg == nil || b.pkg.Scope == nil {
|
|
return nil
|
|
}
|
|
return b.pkg.Scope.Lookup(name)
|
|
}
|
|
|
|
func (b *builtinPkg) CompiledGoFiles() []source.ParseGoHandle {
|
|
return b.files
|
|
}
|
|
|
|
// buildBuiltinPkg builds the view's builtin package.
|
|
// It assumes that the view is not active yet,
|
|
// i.e. it has not been added to the session's list of views.
|
|
func (v *view) buildBuiltinPackage(ctx context.Context) error {
|
|
cfg := v.Config(ctx)
|
|
pkgs, err := packages.Load(cfg, "builtin")
|
|
// If the error is related to a go.mod parse error, we want to continue loading.
|
|
if err != nil && strings.Contains(err.Error(), ".mod:") {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(pkgs) != 1 {
|
|
return err
|
|
}
|
|
pkg := pkgs[0]
|
|
files := make(map[string]*ast.File)
|
|
for _, filename := range pkg.GoFiles {
|
|
fh := v.session.GetFile(span.FileURI(filename))
|
|
ph := v.session.cache.ParseGoHandle(fh, source.ParseFull)
|
|
v.builtin.files = append(v.builtin.files, ph)
|
|
file, _, _, err := ph.Parse(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
files[filename] = file
|
|
|
|
v.ignoredURIsMu.Lock()
|
|
v.ignoredURIs[span.NewURI(filename)] = struct{}{}
|
|
v.ignoredURIsMu.Unlock()
|
|
}
|
|
v.builtin.pkg, err = ast.NewPackage(cfg.Fset, files, nil, nil)
|
|
return err
|
|
}
|