2018-11-05 12:48:08 -07:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
package cache
|
2018-09-27 16:15:45 -06:00
|
|
|
|
|
|
|
import (
|
2018-12-18 14:18:03 -07:00
|
|
|
"context"
|
2019-07-14 21:08:10 -06:00
|
|
|
"fmt"
|
2019-04-29 19:08:16 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
2019-06-04 20:14:37 -06:00
|
|
|
"go/token"
|
2019-03-29 14:39:22 -06:00
|
|
|
"go/types"
|
2019-03-27 07:25:30 -06:00
|
|
|
"os"
|
2019-08-28 14:02:38 -06:00
|
|
|
"os/exec"
|
2019-05-23 10:08:20 -06:00
|
|
|
"path/filepath"
|
2019-07-03 13:23:05 -06:00
|
|
|
"strings"
|
2018-09-27 16:15:45 -06:00
|
|
|
"sync"
|
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-07-03 13:23:05 -06:00
|
|
|
"golang.org/x/tools/internal/imports"
|
2019-05-29 12:55:52 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
2018-12-05 15:00:36 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2018-09-27 16:15:45 -06:00
|
|
|
)
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
type view struct {
|
2019-05-15 10:24:49 -06:00
|
|
|
session *session
|
2019-05-29 12:55:52 -06:00
|
|
|
id string
|
2019-05-15 10:24:49 -06:00
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
options source.ViewOptions
|
|
|
|
|
2019-03-04 16:01:51 -07:00
|
|
|
// mu protects all mutable state of the view.
|
|
|
|
mu sync.Mutex
|
2018-11-02 14:15:31 -06:00
|
|
|
|
2019-03-29 17:04:29 -06:00
|
|
|
// baseCtx is the context handed to NewView. This is the parent of all
|
|
|
|
// background contexts created for this view.
|
|
|
|
baseCtx context.Context
|
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
// backgroundCtx is the current context used by background tasks initiated
|
|
|
|
// by the view.
|
|
|
|
backgroundCtx context.Context
|
|
|
|
|
|
|
|
// cancel is called when all action being performed by the current view
|
|
|
|
// should be stopped.
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2019-03-28 06:49:42 -06:00
|
|
|
// Name is the user visible name of this view.
|
2019-05-14 21:04:23 -06:00
|
|
|
name string
|
2019-03-28 06:49:42 -06:00
|
|
|
|
|
|
|
// Folder is the root of this view.
|
2019-05-14 21:04:23 -06:00
|
|
|
folder span.URI
|
2019-03-28 06:49:42 -06:00
|
|
|
|
2019-07-03 13:23:05 -06:00
|
|
|
// process is the process env for this view.
|
|
|
|
// Note: this contains cached module and filesystem state.
|
2019-07-12 16:54:06 -06:00
|
|
|
//
|
|
|
|
// TODO(suzmue): the state cached in the process env is specific to each view,
|
|
|
|
// however, there is state that can be shared between views that is not currently
|
|
|
|
// cached, like the module cache.
|
2019-07-03 13:23:05 -06:00
|
|
|
processEnv *imports.ProcessEnv
|
|
|
|
|
2019-07-12 16:54:06 -06:00
|
|
|
// modFileVersions stores the last seen versions of the module files that are used
|
|
|
|
// by processEnvs resolver.
|
|
|
|
// TODO(suzmue): These versions may not actually be on disk.
|
|
|
|
modFileVersions map[string]string
|
|
|
|
|
2019-03-27 07:25:30 -06:00
|
|
|
// keep track of files by uri and by basename, a single file may be mapped
|
|
|
|
// to multiple uris, and the same basename may map to multiple files
|
2019-05-03 22:04:18 -06:00
|
|
|
filesByURI map[span.URI]viewFile
|
|
|
|
filesByBase map[string][]viewFile
|
2019-02-06 16:47:00 -07:00
|
|
|
|
2019-03-04 16:01:51 -07:00
|
|
|
// mcache caches metadata for the packages of the opened files in a view.
|
|
|
|
mcache *metadataCache
|
|
|
|
|
2019-04-29 19:08:16 -06:00
|
|
|
// builtinPkg is the AST package used to resolve builtin types.
|
|
|
|
builtinPkg *ast.Package
|
2019-05-15 15:58:16 -06:00
|
|
|
|
|
|
|
// ignoredURIs is the set of URIs of files that we ignore.
|
2019-08-12 12:54:57 -06:00
|
|
|
ignoredURIsMu sync.Mutex
|
|
|
|
ignoredURIs map[span.URI]struct{}
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:01:51 -07:00
|
|
|
type metadataCache struct {
|
2019-06-11 16:06:27 -06:00
|
|
|
mu sync.Mutex // guards both maps
|
|
|
|
packages map[packageID]*metadata
|
2019-03-04 16:01:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type metadata struct {
|
2019-07-09 15:52:23 -06:00
|
|
|
id packageID
|
|
|
|
pkgPath packagePath
|
|
|
|
name string
|
|
|
|
files []span.URI
|
|
|
|
key string
|
|
|
|
typesSizes types.Sizes
|
|
|
|
parents map[packageID]bool
|
|
|
|
children map[packageID]*metadata
|
|
|
|
errors []packages.Error
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
func (v *view) Session() source.Session {
|
|
|
|
return v.session
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
// Name returns the user visible name of this view.
|
|
|
|
func (v *view) Name() string {
|
|
|
|
return v.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Folder returns the root of this view.
|
|
|
|
func (v *view) Folder() span.URI {
|
|
|
|
return v.folder
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
func (v *view) Options() source.ViewOptions {
|
|
|
|
return v.options
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
// Config returns the configuration used for the view's interaction with the
|
|
|
|
// go/packages API. It is shared across all views.
|
2019-07-14 11:59:24 -06:00
|
|
|
func (v *view) Config(ctx context.Context) *packages.Config {
|
2019-06-24 14:34:21 -06:00
|
|
|
// TODO: Should we cache the config and/or overlay somewhere?
|
2019-05-17 08:51:19 -06:00
|
|
|
return &packages.Config{
|
2019-06-06 11:51:00 -06:00
|
|
|
Dir: v.folder.Filename(),
|
2019-09-09 11:04:12 -06:00
|
|
|
Env: v.options.Env,
|
|
|
|
BuildFlags: v.options.BuildFlags,
|
2019-05-17 08:51:19 -06:00
|
|
|
Mode: packages.NeedName |
|
|
|
|
packages.NeedFiles |
|
|
|
|
packages.NeedCompiledGoFiles |
|
|
|
|
packages.NeedImports |
|
|
|
|
packages.NeedDeps |
|
|
|
|
packages.NeedTypesSizes,
|
2019-06-04 20:14:37 -06:00
|
|
|
Fset: v.session.cache.fset,
|
|
|
|
Overlay: v.session.buildOverlay(),
|
|
|
|
ParseFile: func(*token.FileSet, string, []byte) (*ast.File, error) {
|
|
|
|
panic("go/packages must not be used to parse files")
|
|
|
|
},
|
2019-07-14 11:59:24 -06:00
|
|
|
Logf: func(format string, args ...interface{}) {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Print(ctx, fmt.Sprintf(format, args...))
|
2019-07-14 11:59:24 -06:00
|
|
|
},
|
2019-06-04 20:14:37 -06:00
|
|
|
Tests: true,
|
2019-05-17 08:51:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:06 -06:00
|
|
|
func (v *view) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error {
|
2019-07-03 13:23:05 -06:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
if v.processEnv == nil {
|
2019-08-28 14:02:38 -06:00
|
|
|
var err error
|
|
|
|
if v.processEnv, err = v.buildProcessEnv(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-03 13:23:05 -06:00
|
|
|
}
|
2019-07-12 16:54:06 -06:00
|
|
|
|
|
|
|
// Before running the user provided function, clear caches in the resolver.
|
|
|
|
if v.modFilesChanged() {
|
|
|
|
if r, ok := v.processEnv.GetResolver().(*imports.ModuleResolver); ok {
|
|
|
|
// Clear the resolver cache and set Initialized to false.
|
|
|
|
r.Initialized = false
|
|
|
|
r.Main = nil
|
|
|
|
r.ModsByModPath = nil
|
|
|
|
r.ModsByDir = nil
|
|
|
|
// Reset the modFileVersions.
|
|
|
|
v.modFileVersions = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the user function.
|
|
|
|
opts.Env = v.processEnv
|
|
|
|
if err := fn(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If applicable, store the file versions of the 'go.mod' files that are
|
|
|
|
// looked at by the resolver.
|
|
|
|
v.storeModFileVersions()
|
|
|
|
|
|
|
|
return nil
|
2019-07-03 13:23:05 -06:00
|
|
|
}
|
|
|
|
|
2019-08-28 14:02:38 -06:00
|
|
|
func (v *view) buildProcessEnv(ctx context.Context) (*imports.ProcessEnv, error) {
|
2019-07-14 11:59:24 -06:00
|
|
|
cfg := v.Config(ctx)
|
2019-07-03 13:23:05 -06:00
|
|
|
env := &imports.ProcessEnv{
|
|
|
|
WorkingDir: cfg.Dir,
|
2019-07-14 21:08:10 -06:00
|
|
|
Logf: func(format string, args ...interface{}) {
|
|
|
|
log.Print(ctx, fmt.Sprintf(format, args...))
|
2019-07-03 13:23:05 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, kv := range cfg.Env {
|
|
|
|
split := strings.Split(kv, "=")
|
|
|
|
if len(split) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch split[0] {
|
|
|
|
case "GOPATH":
|
|
|
|
env.GOPATH = split[1]
|
|
|
|
case "GOROOT":
|
|
|
|
env.GOROOT = split[1]
|
|
|
|
case "GO111MODULE":
|
|
|
|
env.GO111MODULE = split[1]
|
|
|
|
case "GOPROXY":
|
2019-09-08 00:42:38 -06:00
|
|
|
env.GOPROXY = split[1]
|
2019-07-03 13:23:05 -06:00
|
|
|
case "GOFLAGS":
|
|
|
|
env.GOFLAGS = split[1]
|
|
|
|
case "GOSUMDB":
|
|
|
|
env.GOSUMDB = split[1]
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 14:02:38 -06:00
|
|
|
|
|
|
|
if env.GOPATH == "" {
|
|
|
|
cmd := exec.CommandContext(ctx, "go", "env", "GOPATH")
|
|
|
|
cmd.Env = cfg.Env
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
env.GOPATH = strings.TrimSpace(string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env, nil
|
2019-07-03 13:23:05 -06:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:06 -06:00
|
|
|
func (v *view) modFilesChanged() bool {
|
|
|
|
// Check the versions of the 'go.mod' files of the main module
|
|
|
|
// and modules included by a replace directive. Return true if
|
|
|
|
// any of these file versions do not match.
|
|
|
|
for filename, version := range v.modFileVersions {
|
|
|
|
if version != v.fileVersion(filename) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) storeModFileVersions() {
|
|
|
|
// Store the mod files versions, if we are using a ModuleResolver.
|
|
|
|
r, moduleMode := v.processEnv.GetResolver().(*imports.ModuleResolver)
|
|
|
|
if !moduleMode || !r.Initialized {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.modFileVersions = make(map[string]string)
|
|
|
|
|
|
|
|
// Get the file versions of the 'go.mod' files of the main module
|
|
|
|
// and modules included by a replace directive in the resolver.
|
|
|
|
for _, mod := range r.ModsByModPath {
|
|
|
|
if (mod.Main || mod.Replace != nil) && mod.GoMod != "" {
|
|
|
|
v.modFileVersions[mod.GoMod] = v.fileVersion(mod.GoMod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) fileVersion(filename string) string {
|
|
|
|
uri := span.FileURI(filename)
|
|
|
|
f := v.session.GetFile(uri)
|
|
|
|
return f.Identity().Version
|
|
|
|
}
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
func (v *view) Shutdown(ctx context.Context) {
|
|
|
|
v.session.removeView(ctx, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) shutdown(context.Context) {
|
2019-05-02 08:55:04 -06:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
if v.cancel != nil {
|
|
|
|
v.cancel()
|
|
|
|
v.cancel = nil
|
|
|
|
}
|
2019-05-29 12:55:52 -06:00
|
|
|
debug.DropView(debugView{v})
|
2019-05-02 08:55:04 -06:00
|
|
|
}
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
// Ignore checks if the given URI is a URI we ignore.
|
|
|
|
// As of right now, we only ignore files in the "builtin" package.
|
|
|
|
func (v *view) Ignore(uri span.URI) bool {
|
2019-08-12 12:54:57 -06:00
|
|
|
v.ignoredURIsMu.Lock()
|
|
|
|
defer v.ignoredURIsMu.Unlock()
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
_, ok := v.ignoredURIs[uri]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (v *view) BackgroundContext() context.Context {
|
2019-03-05 15:30:44 -07:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
|
|
|
|
return v.backgroundCtx
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
func (v *view) BuiltinPackage() *ast.Package {
|
2019-04-29 19:08:16 -06:00
|
|
|
return v.builtinPkg
|
|
|
|
}
|
|
|
|
|
2019-05-31 22:08:57 -06:00
|
|
|
// 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.
|
2019-07-14 11:59:24 -06:00
|
|
|
func (v *view) buildBuiltinPkg(ctx context.Context) {
|
|
|
|
cfg := *v.Config(ctx)
|
|
|
|
pkgs, err := packages.Load(&cfg, "builtin")
|
|
|
|
if err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "error getting package metadata for \"builtin\" package", err)
|
2019-07-14 11:59:24 -06:00
|
|
|
}
|
2019-04-29 19:08:16 -06:00
|
|
|
if len(pkgs) != 1 {
|
2019-05-17 08:51:19 -06:00
|
|
|
v.builtinPkg, _ = ast.NewPackage(cfg.Fset, nil, nil, nil)
|
|
|
|
return
|
2019-04-29 19:08:16 -06:00
|
|
|
}
|
|
|
|
pkg := pkgs[0]
|
|
|
|
files := make(map[string]*ast.File)
|
|
|
|
for _, filename := range pkg.GoFiles {
|
|
|
|
file, err := parser.ParseFile(cfg.Fset, filename, nil, parser.ParseComments)
|
|
|
|
if err != nil {
|
2019-05-17 08:51:19 -06:00
|
|
|
v.builtinPkg, _ = ast.NewPackage(cfg.Fset, nil, nil, nil)
|
|
|
|
return
|
2019-04-29 19:08:16 -06:00
|
|
|
}
|
|
|
|
files[filename] = file
|
2019-08-12 12:54:57 -06:00
|
|
|
|
|
|
|
v.ignoredURIsMu.Lock()
|
2019-05-17 08:51:19 -06:00
|
|
|
v.ignoredURIs[span.NewURI(filename)] = struct{}{}
|
2019-08-12 12:54:57 -06:00
|
|
|
v.ignoredURIsMu.Unlock()
|
2019-04-29 19:08:16 -06:00
|
|
|
}
|
2019-05-17 08:51:19 -06:00
|
|
|
v.builtinPkg, _ = ast.NewPackage(cfg.Fset, files, nil, nil)
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
// SetContent sets the overlay contents for a file.
|
2019-08-12 16:50:01 -06:00
|
|
|
func (v *view) SetContent(ctx context.Context, uri span.URI, content []byte) (bool, error) {
|
2019-03-05 15:30:44 -07:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
2019-04-29 17:47:54 -06:00
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
// Cancel all still-running previous requests, since they would be
|
|
|
|
// operating on stale data.
|
|
|
|
v.cancel()
|
2019-03-29 17:04:29 -06:00
|
|
|
v.backgroundCtx, v.cancel = context.WithCancel(v.baseCtx)
|
2019-03-05 15:30:44 -07:00
|
|
|
|
2019-08-12 12:54:57 -06:00
|
|
|
if !v.Ignore(uri) {
|
2019-08-12 16:50:01 -06:00
|
|
|
return v.session.SetOverlay(uri, content), nil
|
2019-08-12 12:54:57 -06:00
|
|
|
}
|
2019-08-12 16:50:01 -06:00
|
|
|
return false, nil
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 15:44:09 -06:00
|
|
|
// invalidateContent invalidates the content of a Go file,
|
|
|
|
// including any position and type information that depends on it.
|
2019-06-28 14:37:54 -06:00
|
|
|
func (f *goFile) invalidateContent(ctx context.Context) {
|
2019-07-03 10:46:23 -06:00
|
|
|
// Mutex acquisition order here is important. It must match the order
|
|
|
|
// in loadParseTypecheck to avoid deadlocks.
|
2019-06-26 12:34:36 -06:00
|
|
|
f.view.mcache.mu.Lock()
|
|
|
|
defer f.view.mcache.mu.Unlock()
|
|
|
|
|
2019-09-04 13:30:42 -06:00
|
|
|
var toDelete []packageID
|
2019-06-26 12:34:36 -06:00
|
|
|
f.mu.Lock()
|
2019-09-04 13:30:42 -06:00
|
|
|
for id, cph := range f.pkgs {
|
|
|
|
if cph != nil {
|
|
|
|
toDelete = append(toDelete, id)
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 12:34:36 -06:00
|
|
|
f.mu.Unlock()
|
2019-06-05 15:44:09 -06:00
|
|
|
|
2019-09-04 13:30:42 -06:00
|
|
|
f.handleMu.Lock()
|
|
|
|
defer f.handleMu.Unlock()
|
|
|
|
|
2019-06-05 15:44:09 -06:00
|
|
|
// Remove the package and all of its reverse dependencies from the cache.
|
2019-09-04 13:30:42 -06:00
|
|
|
for _, id := range toDelete {
|
|
|
|
f.view.remove(ctx, id, map[packageID]struct{}{})
|
2019-06-05 15:44:09 -06:00
|
|
|
}
|
2019-09-04 13:30:42 -06:00
|
|
|
|
|
|
|
f.handle = nil
|
2019-06-05 15:44:09 -06:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:14:36 -06:00
|
|
|
// invalidateMeta invalidates package metadata for all files in f's
|
2019-09-03 14:07:13 -06:00
|
|
|
// package. This forces f's package's metadata to be reloaded next
|
|
|
|
// time the package is checked.
|
|
|
|
func (f *goFile) invalidateMeta(ctx context.Context) {
|
|
|
|
pkgs, err := f.GetPackages(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "invalidateMeta: GetPackages", err, telemetry.File.Of(f.URI()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
for _, pgh := range pkg.GetHandles() {
|
|
|
|
uri := pgh.File().Identity().URI
|
|
|
|
if gof, _ := f.view.FindFile(ctx, uri).(*goFile); gof != nil {
|
|
|
|
gof.mu.Lock()
|
|
|
|
gof.meta = nil
|
|
|
|
gof.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.view.mcache.mu.Lock()
|
|
|
|
delete(f.view.mcache.packages, packageID(pkg.ID()))
|
|
|
|
f.view.mcache.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
// remove invalidates a package and its reverse dependencies in the view's
|
|
|
|
// package cache. It is assumed that the caller has locked both the mutexes
|
|
|
|
// of both the mcache and the pcache.
|
2019-06-28 14:37:54 -06:00
|
|
|
func (v *view) remove(ctx context.Context, id packageID, seen map[packageID]struct{}) {
|
2019-06-11 16:06:27 -06:00
|
|
|
if _, ok := seen[id]; ok {
|
2019-04-01 12:25:45 -06:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
m, ok := v.mcache.packages[id]
|
2019-03-05 15:30:44 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-11 16:06:27 -06:00
|
|
|
seen[id] = struct{}{}
|
|
|
|
for parentID := range m.parents {
|
2019-06-28 14:37:54 -06:00
|
|
|
v.remove(ctx, parentID, seen)
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
|
|
|
// All of the files in the package may also be holding a pointer to the
|
|
|
|
// invalidated package.
|
2019-07-09 15:52:23 -06:00
|
|
|
for _, uri := range m.files {
|
|
|
|
f, err := v.findFile(uri)
|
2019-06-28 14:37:54 -06:00
|
|
|
if err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "cannot find file", err, telemetry.File.Of(f.URI()))
|
2019-06-28 14:37:54 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
gof, ok := f.(*goFile)
|
|
|
|
if !ok {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "non-Go file", nil, telemetry.File.Of(f.URI()))
|
2019-06-28 14:37:54 -06:00
|
|
|
continue
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
gof.mu.Lock()
|
2019-07-09 15:52:23 -06:00
|
|
|
// TODO: Ultimately, we shouldn't need this.
|
|
|
|
if cph, ok := gof.pkgs[id]; ok {
|
|
|
|
// Delete the package handle from the store.
|
|
|
|
v.session.cache.store.Delete(checkPackageKey{
|
2019-09-10 15:33:35 -06:00
|
|
|
id: cph.ID(),
|
2019-07-09 15:52:23 -06:00
|
|
|
files: hashParseKeys(cph.Files()),
|
|
|
|
config: hashConfig(cph.Config()),
|
|
|
|
})
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
delete(gof.pkgs, id)
|
|
|
|
gof.mu.Unlock()
|
2019-03-05 15:30:44 -07:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
return
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:05:59 -06:00
|
|
|
// FindFile returns the file if the given URI is already a part of the view.
|
2019-05-03 22:04:18 -06:00
|
|
|
func (v *view) FindFile(ctx context.Context, uri span.URI) source.File {
|
2019-03-29 14:05:59 -06:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
f, err := v.findFile(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:46:14 -07:00
|
|
|
// GetFile returns a File for the given URI. It will always succeed because it
|
|
|
|
// adds the file to the managed set if needed.
|
2019-05-14 21:04:23 -06:00
|
|
|
func (v *view) GetFile(ctx context.Context, uri span.URI) (source.File, error) {
|
2018-11-02 16:10:49 -06:00
|
|
|
v.mu.Lock()
|
2019-03-05 15:30:44 -07:00
|
|
|
defer v.mu.Unlock()
|
|
|
|
|
2019-06-28 14:37:54 -06:00
|
|
|
return v.getFile(ctx, uri)
|
2018-11-05 15:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// getFile is the unlocked internal implementation of GetFile.
|
2019-06-28 14:37:54 -06:00
|
|
|
func (v *view) getFile(ctx context.Context, uri span.URI) (viewFile, error) {
|
2019-03-29 14:14:24 -06:00
|
|
|
if f, err := v.findFile(uri); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if f != nil {
|
2019-03-27 07:25:30 -06:00
|
|
|
return f, nil
|
|
|
|
}
|
2019-06-06 11:51:00 -06:00
|
|
|
filename := uri.Filename()
|
2019-05-23 13:03:11 -06:00
|
|
|
var f viewFile
|
2019-05-23 10:08:20 -06:00
|
|
|
switch ext := filepath.Ext(filename); ext {
|
|
|
|
case ".mod":
|
2019-05-23 13:03:11 -06:00
|
|
|
f = &modFile{
|
|
|
|
fileBase: fileBase{
|
|
|
|
view: v,
|
|
|
|
fname: filename,
|
2019-06-21 15:00:02 -06:00
|
|
|
kind: source.Mod,
|
2019-05-23 13:03:11 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case ".sum":
|
|
|
|
f = &sumFile{
|
|
|
|
fileBase: fileBase{
|
|
|
|
view: v,
|
|
|
|
fname: filename,
|
2019-06-21 15:00:02 -06:00
|
|
|
kind: source.Sum,
|
2019-05-23 13:03:11 -06:00
|
|
|
},
|
|
|
|
}
|
2019-05-23 10:08:20 -06:00
|
|
|
default:
|
2019-06-10 12:02:28 -06:00
|
|
|
// Assume that all other files are Go files, regardless of extension.
|
|
|
|
f = &goFile{
|
|
|
|
fileBase: fileBase{
|
|
|
|
view: v,
|
|
|
|
fname: filename,
|
2019-06-21 15:00:02 -06:00
|
|
|
kind: source.Go,
|
2019-06-10 12:02:28 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
v.session.filesWatchMap.Watch(uri, func() {
|
2019-06-26 12:34:36 -06:00
|
|
|
gof, ok := f.(*goFile)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
gof.invalidateContent(ctx)
|
2019-06-10 12:02:28 -06:00
|
|
|
})
|
2019-03-27 07:25:30 -06:00
|
|
|
}
|
|
|
|
v.mapFile(uri, f)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:14:24 -06:00
|
|
|
// findFile checks the cache for any file matching the given uri.
|
|
|
|
//
|
|
|
|
// An error is only returned for an irreparable failure, for example, if the
|
|
|
|
// filename in question does not exist.
|
2019-05-03 22:04:18 -06:00
|
|
|
func (v *view) findFile(uri span.URI) (viewFile, error) {
|
2019-03-27 07:25:30 -06:00
|
|
|
if f := v.filesByURI[uri]; f != nil {
|
|
|
|
// a perfect match
|
2019-03-29 14:14:24 -06:00
|
|
|
return f, nil
|
2019-03-27 07:25:30 -06:00
|
|
|
}
|
|
|
|
// no exact match stored, time to do some real work
|
|
|
|
// check for any files with the same basename
|
2019-06-06 11:51:00 -06:00
|
|
|
fname := uri.Filename()
|
2019-03-27 07:25:30 -06:00
|
|
|
basename := basename(fname)
|
|
|
|
if candidates := v.filesByBase[basename]; candidates != nil {
|
|
|
|
pathStat, err := os.Stat(fname)
|
2019-03-29 14:14:24 -06:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, err
|
2019-08-08 10:18:43 -06:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-03-29 14:14:24 -06:00
|
|
|
return nil, nil // the file may exist, return without an error
|
2019-03-27 07:25:30 -06:00
|
|
|
}
|
|
|
|
for _, c := range candidates {
|
2019-05-03 22:04:18 -06:00
|
|
|
if cStat, err := os.Stat(c.filename()); err == nil {
|
2019-03-27 07:25:30 -06:00
|
|
|
if os.SameFile(pathStat, cStat) {
|
|
|
|
// same file, map it
|
|
|
|
v.mapFile(uri, c)
|
2019-03-29 14:14:24 -06:00
|
|
|
return c, nil
|
2019-03-27 07:25:30 -06:00
|
|
|
}
|
|
|
|
}
|
2018-11-05 19:23:02 -07:00
|
|
|
}
|
2018-10-19 14:03:29 -06:00
|
|
|
}
|
2019-03-29 14:14:24 -06:00
|
|
|
// no file with a matching name was found, it wasn't in our cache
|
|
|
|
return nil, nil
|
2019-03-27 07:25:30 -06:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) addURI(uri span.URI) int {
|
2019-03-27 07:25:30 -06:00
|
|
|
f.uris = append(f.uris, uri)
|
2019-05-03 22:04:18 -06:00
|
|
|
return len(f.uris)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) mapFile(uri span.URI, f viewFile) {
|
|
|
|
v.filesByURI[uri] = f
|
|
|
|
if f.addURI(uri) == 1 {
|
|
|
|
basename := basename(f.filename())
|
|
|
|
v.filesByBase[basename] = append(v.filesByBase[basename], f)
|
2019-03-27 07:25:30 -06:00
|
|
|
}
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
2019-05-29 12:55:52 -06:00
|
|
|
|
|
|
|
type debugView struct{ *view }
|
|
|
|
|
|
|
|
func (v debugView) ID() string { return v.id }
|
|
|
|
func (v debugView) Session() debug.Session { return debugSession{v.session} }
|