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.
|
|
|
|
|
2019-09-19 14:53:53 -06:00
|
|
|
// Package cache implements the caching layer for gopls.
|
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"
|
2020-01-11 18:29:13 -07:00
|
|
|
"encoding/json"
|
2019-07-14 21:08:10 -06:00
|
|
|
"fmt"
|
2019-04-29 19:08:16 -06:00
|
|
|
"go/ast"
|
2019-06-04 20:14:37 -06:00
|
|
|
"go/token"
|
2019-12-18 17:31:48 -07:00
|
|
|
stdlog "log"
|
2019-03-27 07:25:30 -06:00
|
|
|
"os"
|
2019-12-17 14:53:57 -07:00
|
|
|
"path/filepath"
|
2019-11-08 11:25:29 -07:00
|
|
|
"reflect"
|
2019-07-03 13:23:05 -06:00
|
|
|
"strings"
|
2018-09-27 16:15:45 -06:00
|
|
|
"sync"
|
internal/lsp/cache: only refresh imports cache every 30 seconds
Loading completion suggestions can be slow, especially in GOPATH mode
where basically anything can change at any time. As a compromise, cache
everything for 30 seconds. Specifically, after a completion operation
finishes, if the cache is more than 30 seconds old, refresh it
asynchronously. That keeps user-facing latency consistent, without
chewing up CPU when the editor isn't in use. It does mean that if you
walk away for an hour and come back, the first completion may be stale.
In module mode this is relatively benign. The only things the
longer caching affects are the main module and replace targets, and
relevant packages in those will generally be loaded by gopls, so they'll
have full, up-to-date type information regardless.
In GOPATH mode this may be more troublesome, since it affects
everything. In particular, go get -u of a package that isn't imported
yet won't be reflected until the cache period expires. I think that's a
rare enough case not to worry about.
Change-Id: Iaadfd0ff647cda2b1dcdead9254b5492b397e86e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/205163
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-11-04 12:05:41 -07:00
|
|
|
"time"
|
2018-09-27 16:15:45 -06:00
|
|
|
|
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"
|
2020-01-14 22:22:42 -07:00
|
|
|
"golang.org/x/tools/internal/memoize"
|
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"
|
2019-12-19 13:47:07 -07:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2019-09-16 16:17:51 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
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-11 11:13:44 -06:00
|
|
|
options source.Options
|
2019-09-05 22:17:36 -06:00
|
|
|
|
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
|
|
|
|
2020-01-11 18:29:13 -07:00
|
|
|
// mod is the module information for this view.
|
|
|
|
mod *moduleInformation
|
|
|
|
|
2020-01-21 11:57:40 -07:00
|
|
|
// importsMu guards imports-related state, particularly the ProcessEnv.
|
|
|
|
importsMu sync.Mutex
|
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-12-18 17:31:48 -07:00
|
|
|
processEnv *imports.ProcessEnv
|
|
|
|
cacheRefreshDuration time.Duration
|
|
|
|
cacheRefreshTimer *time.Timer
|
2020-01-17 13:33:57 -07:00
|
|
|
cachedModFileVersion source.FileIdentity
|
2019-07-12 16:54:06 -06:00
|
|
|
|
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-12-17 16:57:54 -07:00
|
|
|
filesByURI map[span.URI]*fileBase
|
|
|
|
filesByBase map[string][]*fileBase
|
2019-02-06 16:47:00 -07:00
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
snapshotMu sync.Mutex
|
|
|
|
snapshot *snapshot
|
2019-03-04 16:01:51 -07:00
|
|
|
|
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{}
|
2019-12-19 12:31:39 -07:00
|
|
|
|
2020-01-11 18:29:13 -07:00
|
|
|
// `go env` variables that need to be tracked by the view.
|
|
|
|
gopath, gomod, gocache string
|
|
|
|
|
|
|
|
// initialized is closed when the view has been fully initialized.
|
|
|
|
// On initialization, the view's workspace packages are loaded.
|
|
|
|
// All of the fields below are set as part of initialization.
|
|
|
|
// If we failed to load, we don't re-try to avoid too many go/packages calls.
|
2019-12-19 12:31:39 -07:00
|
|
|
initializeOnce sync.Once
|
|
|
|
initialized chan struct{}
|
|
|
|
initializationError error
|
2020-01-07 19:37:41 -07:00
|
|
|
|
2020-01-14 22:22:42 -07:00
|
|
|
// builtin pins the AST and package for builtin.go in memory.
|
|
|
|
builtin *builtinPackageHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
type builtinPackageHandle struct {
|
|
|
|
handle *memoize.Handle
|
|
|
|
file source.ParseGoHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
type builtinPackageData struct {
|
|
|
|
memoize.NoCopy
|
|
|
|
|
|
|
|
pkg *ast.Package
|
|
|
|
err error
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
|
2020-01-06 16:08:39 -07:00
|
|
|
// fileBase holds the common functionality for all files.
|
|
|
|
// It is intended to be embedded in the file implementations
|
|
|
|
type fileBase struct {
|
|
|
|
uris []span.URI
|
|
|
|
fname string
|
|
|
|
|
|
|
|
view *view
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fileBase) URI() span.URI {
|
|
|
|
return f.uris[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fileBase) filename() string {
|
|
|
|
return f.fname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fileBase) addURI(uri span.URI) int {
|
|
|
|
f.uris = append(f.uris, uri)
|
|
|
|
return len(f.uris)
|
|
|
|
}
|
|
|
|
|
2020-01-11 18:29:13 -07:00
|
|
|
// moduleInformation holds the real and temporary go.mod files
|
|
|
|
// that are attributed to a view.
|
|
|
|
type moduleInformation struct {
|
|
|
|
realMod, tempMod span.URI
|
2019-12-16 13:40:24 -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-11 11:13:44 -06:00
|
|
|
func (v *view) Options() source.Options {
|
2019-09-05 22:17:36 -06:00
|
|
|
return v.options
|
|
|
|
}
|
|
|
|
|
2019-11-08 11:25:29 -07:00
|
|
|
func minorOptionsChange(a, b source.Options) bool {
|
|
|
|
// Check if any of the settings that modify our understanding of files have been changed
|
|
|
|
if !reflect.DeepEqual(a.Env, b.Env) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.BuildFlags, b.BuildFlags) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// the rest of the options are benign
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) SetOptions(ctx context.Context, options source.Options) (source.View, error) {
|
|
|
|
// no need to rebuild the view if the options were not materially changed
|
|
|
|
if minorOptionsChange(v.options, options) {
|
|
|
|
v.options = options
|
|
|
|
return v, nil
|
|
|
|
}
|
2019-11-15 12:47:29 -07:00
|
|
|
newView, _, err := v.session.updateView(ctx, v, options)
|
2019-11-08 11:25:29 -07:00
|
|
|
return newView, err
|
2019-09-11 16:21:01 -06:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:11:40 -07:00
|
|
|
func (v *view) LookupBuiltin(ctx context.Context, name string) (*ast.Object, error) {
|
|
|
|
if err := v.awaitInitialized(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := v.builtin.handle.Get(ctx)
|
2020-01-14 22:22:42 -07:00
|
|
|
if data == nil {
|
|
|
|
return nil, errors.Errorf("unexpected nil builtin package")
|
|
|
|
}
|
|
|
|
d, ok := data.(*builtinPackageData)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unexpected type %T", data)
|
|
|
|
}
|
|
|
|
if d.err != nil {
|
|
|
|
return nil, d.err
|
|
|
|
}
|
|
|
|
if d.pkg == nil || d.pkg.Scope == nil {
|
|
|
|
return nil, errors.Errorf("no builtin package")
|
|
|
|
}
|
|
|
|
astObj := d.pkg.Scope.Lookup(name)
|
|
|
|
if astObj == nil {
|
|
|
|
return nil, errors.Errorf("no builtin object for %s", name)
|
|
|
|
}
|
|
|
|
return astObj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) buildBuiltinPackage(ctx context.Context, m *metadata) error {
|
|
|
|
if len(m.goFiles) != 1 {
|
|
|
|
return errors.Errorf("only expected 1 file, got %v", len(m.goFiles))
|
|
|
|
}
|
|
|
|
uri := m.goFiles[0]
|
|
|
|
v.addIgnoredFile(uri) // to avoid showing diagnostics for builtin.go
|
|
|
|
|
|
|
|
// Get the FileHandle through the session to avoid adding it to the snapshot.
|
|
|
|
pgh := v.session.cache.ParseGoHandle(v.session.GetFile(uri), source.ParseFull)
|
|
|
|
fset := v.session.cache.fset
|
|
|
|
h := v.session.cache.store.Bind(pgh.File().Identity(), func(ctx context.Context) interface{} {
|
|
|
|
data := &builtinPackageData{}
|
|
|
|
file, _, _, err := pgh.Parse(ctx)
|
|
|
|
if err != nil {
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
data.pkg, data.err = ast.NewPackage(fset, map[string]*ast.File{
|
|
|
|
pgh.File().Identity().URI.Filename(): file,
|
|
|
|
}, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
v.builtin = &builtinPackageHandle{
|
|
|
|
handle: h,
|
|
|
|
file: pgh,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-12-16 13:40:24 -07:00
|
|
|
|
|
|
|
// We want to run the go commands with the -modfile flag if the version of go
|
|
|
|
// that we are using supports it.
|
|
|
|
buildFlags := v.options.BuildFlags
|
2020-01-11 18:29:13 -07:00
|
|
|
if v.mod != nil {
|
|
|
|
buildFlags = append(buildFlags, fmt.Sprintf("-modfile=%s", v.mod.tempMod.Filename()))
|
2019-12-16 13:40:24 -07:00
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
cfg := &packages.Config{
|
2019-06-06 11:51:00 -06:00
|
|
|
Dir: v.folder.Filename(),
|
2019-10-09 12:11:22 -06:00
|
|
|
Context: ctx,
|
2019-12-16 13:40:24 -07:00
|
|
|
BuildFlags: 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-11-03 19:09:58 -07:00
|
|
|
if v.options.VerboseOutput {
|
|
|
|
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
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
cfg.Env = append(cfg.Env, fmt.Sprintf("GOPATH=%s", v.gopath))
|
|
|
|
cfg.Env = append(cfg.Env, v.options.Env...)
|
|
|
|
return cfg
|
2019-05-17 08:51:19 -06:00
|
|
|
}
|
|
|
|
|
2019-12-27 12:07:33 -07:00
|
|
|
func (v *view) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error) error {
|
2020-01-21 11:57:40 -07:00
|
|
|
v.importsMu.Lock()
|
|
|
|
defer v.importsMu.Unlock()
|
|
|
|
|
2019-07-03 13:23:05 -06:00
|
|
|
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
|
|
|
|
2020-01-17 13:33:57 -07:00
|
|
|
// In module mode, check if the mod file has changed.
|
2020-01-21 11:57:40 -07:00
|
|
|
if v.gomod != "" && v.gomod != os.DevNull {
|
|
|
|
mod, err := v.Snapshot().GetFile(span.FileURI(v.gomod))
|
|
|
|
if err == nil && mod.Identity() != v.cachedModFileVersion {
|
2020-01-17 13:33:57 -07:00
|
|
|
v.processEnv.GetResolver().(*imports.ModuleResolver).ClearForNewMod()
|
2020-01-21 11:57:40 -07:00
|
|
|
v.cachedModFileVersion = mod.Identity()
|
2020-01-17 13:33:57 -07:00
|
|
|
}
|
2019-07-12 16:54:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the user function.
|
2019-12-27 12:07:33 -07:00
|
|
|
opts := &imports.Options{
|
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
Env: v.processEnv,
|
|
|
|
}
|
|
|
|
|
2019-07-12 16:54:06 -06:00
|
|
|
if err := fn(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:31:48 -07:00
|
|
|
if v.cacheRefreshTimer == nil {
|
|
|
|
// Don't refresh more than twice per minute.
|
|
|
|
delay := 30 * time.Second
|
|
|
|
// Don't spend more than a couple percent of the time refreshing.
|
|
|
|
if adaptive := 50 * v.cacheRefreshDuration; adaptive > delay {
|
|
|
|
delay = adaptive
|
|
|
|
}
|
|
|
|
v.cacheRefreshTimer = time.AfterFunc(delay, v.refreshProcessEnv)
|
internal/lsp/cache: only refresh imports cache every 30 seconds
Loading completion suggestions can be slow, especially in GOPATH mode
where basically anything can change at any time. As a compromise, cache
everything for 30 seconds. Specifically, after a completion operation
finishes, if the cache is more than 30 seconds old, refresh it
asynchronously. That keeps user-facing latency consistent, without
chewing up CPU when the editor isn't in use. It does mean that if you
walk away for an hour and come back, the first completion may be stale.
In module mode this is relatively benign. The only things the
longer caching affects are the main module and replace targets, and
relevant packages in those will generally be loaded by gopls, so they'll
have full, up-to-date type information regardless.
In GOPATH mode this may be more troublesome, since it affects
everything. In particular, go get -u of a package that isn't imported
yet won't be reflected until the cache period expires. I think that's a
rare enough case not to worry about.
Change-Id: Iaadfd0ff647cda2b1dcdead9254b5492b397e86e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/205163
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-11-04 12:05:41 -07:00
|
|
|
}
|
2019-12-18 17:31:48 -07:00
|
|
|
|
2019-07-12 16:54:06 -06:00
|
|
|
return nil
|
2019-07-03 13:23:05 -06:00
|
|
|
}
|
|
|
|
|
2019-12-18 17:31:48 -07:00
|
|
|
func (v *view) refreshProcessEnv() {
|
|
|
|
start := time.Now()
|
|
|
|
|
2020-01-21 11:57:40 -07:00
|
|
|
v.importsMu.Lock()
|
2019-12-18 17:31:48 -07:00
|
|
|
env := v.processEnv
|
|
|
|
env.GetResolver().ClearForNewScan()
|
2020-01-21 11:57:40 -07:00
|
|
|
v.importsMu.Unlock()
|
2019-12-18 17:31:48 -07:00
|
|
|
|
|
|
|
// We don't have a context handy to use for logging, so use the stdlib for now.
|
|
|
|
stdlog.Printf("background imports cache refresh starting")
|
|
|
|
err := imports.PrimeCache(context.Background(), env)
|
|
|
|
stdlog.Printf("background refresh finished after %v with err: %v", time.Since(start), err)
|
|
|
|
|
2020-01-21 11:57:40 -07:00
|
|
|
v.importsMu.Lock()
|
2019-12-18 17:31:48 -07:00
|
|
|
v.cacheRefreshDuration = time.Since(start)
|
|
|
|
v.cacheRefreshTimer = nil
|
2020-01-21 11:57:40 -07:00
|
|
|
v.importsMu.Unlock()
|
2019-12-18 17:31:48 -07: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
|
|
|
},
|
2019-11-01 14:19:19 -06:00
|
|
|
LocalPrefix: v.options.LocalPrefix,
|
2019-11-03 19:09:58 -07:00
|
|
|
Debug: v.options.VerboseOutput,
|
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]
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 08:49:38 -07:00
|
|
|
if len(cfg.BuildFlags) > 0 {
|
|
|
|
if env.GOFLAGS != "" {
|
|
|
|
env.GOFLAGS += " "
|
|
|
|
}
|
|
|
|
env.GOFLAGS += strings.Join(cfg.BuildFlags, " ")
|
|
|
|
}
|
2019-08-28 14:02:38 -06:00
|
|
|
return env, nil
|
2019-07-03 13:23:05 -06:00
|
|
|
}
|
|
|
|
|
2020-01-06 16:08:39 -07:00
|
|
|
func (v *view) fileVersion(filename string) string {
|
2019-07-12 16:54:06 -06:00
|
|
|
uri := span.FileURI(filename)
|
2020-01-06 16:08:39 -07:00
|
|
|
fh := v.session.GetFile(uri)
|
2019-11-07 15:52:35 -07:00
|
|
|
return fh.Identity().String()
|
2019-07-12 16:54:06 -06:00
|
|
|
}
|
|
|
|
|
2020-01-06 16:08:39 -07:00
|
|
|
func (v *view) mapFile(uri span.URI, f *fileBase) {
|
|
|
|
v.filesByURI[uri] = f
|
|
|
|
if f.addURI(uri) == 1 {
|
|
|
|
basename := basename(f.filename())
|
|
|
|
v.filesByBase[basename] = append(v.filesByBase[basename], f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func basename(filename string) string {
|
|
|
|
return strings.ToLower(filepath.Base(filename))
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:45:06 -07:00
|
|
|
// knownFile returns true if the given URI is already a part of the view.
|
|
|
|
func (v *view) knownFile(uri span.URI) bool {
|
2020-01-06 16:08:39 -07:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
|
2020-01-09 20:45:06 -07:00
|
|
|
f, err := v.findFile(uri)
|
|
|
|
return f != nil && err == nil
|
2020-01-06 16:08:39 -07:00
|
|
|
}
|
|
|
|
|
2020-01-21 13:36:50 -07:00
|
|
|
// getFile returns a file for the given URI. It will always succeed because it
|
2020-01-06 16:08:39 -07:00
|
|
|
// adds the file to the managed set if needed.
|
2020-01-21 13:36:50 -07:00
|
|
|
func (v *view) getFile(uri span.URI) (*fileBase, error) {
|
2020-01-06 16:08:39 -07:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
|
|
|
|
f, err := v.findFile(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if f != nil {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
f = &fileBase{
|
|
|
|
view: v,
|
|
|
|
fname: uri.Filename(),
|
|
|
|
}
|
|
|
|
v.mapFile(uri, f)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (v *view) findFile(uri span.URI) (*fileBase, error) {
|
|
|
|
if f := v.filesByURI[uri]; f != nil {
|
|
|
|
// a perfect match
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
// no exact match stored, time to do some real work
|
|
|
|
// check for any files with the same basename
|
|
|
|
fname := uri.Filename()
|
|
|
|
basename := basename(fname)
|
|
|
|
if candidates := v.filesByBase[basename]; candidates != nil {
|
|
|
|
pathStat, err := os.Stat(fname)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil // the file may exist, return without an error
|
|
|
|
}
|
|
|
|
for _, c := range candidates {
|
|
|
|
if cStat, err := os.Stat(c.filename()); err == nil {
|
|
|
|
if os.SameFile(pathStat, cStat) {
|
|
|
|
// same file, map it
|
|
|
|
v.mapFile(uri, c)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no file with a matching name was found, it wasn't in our cache
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-01-10 15:18:59 -07:00
|
|
|
// TODO: Cancel the view's initialization.
|
2019-05-02 08:55:04 -06:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
if v.cancel != nil {
|
|
|
|
v.cancel()
|
|
|
|
v.cancel = nil
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
if v.mod != nil {
|
|
|
|
os.Remove(v.mod.tempMod.Filename())
|
|
|
|
os.Remove(v.mod.tempSumFile())
|
2019-12-23 09:33:23 -07:00
|
|
|
}
|
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]
|
2019-12-17 14:53:57 -07:00
|
|
|
|
|
|
|
// Files with _ prefixes are always ignored.
|
|
|
|
if !ok && strings.HasPrefix(filepath.Base(uri.Filename()), "_") {
|
|
|
|
v.ignoredURIs[uri] = struct{}{}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-01-14 22:22:42 -07:00
|
|
|
func (v *view) addIgnoredFile(uri span.URI) {
|
|
|
|
v.ignoredURIsMu.Lock()
|
|
|
|
defer v.ignoredURIsMu.Unlock()
|
|
|
|
|
|
|
|
v.ignoredURIs[uri] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-01-17 14:09:09 -07:00
|
|
|
func (v *view) ModFile() string {
|
|
|
|
return v.gomod
|
|
|
|
}
|
|
|
|
|
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-09-27 11:17:59 -06:00
|
|
|
func (v *view) Snapshot() source.Snapshot {
|
|
|
|
return v.getSnapshot()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) getSnapshot() *snapshot {
|
|
|
|
v.snapshotMu.Lock()
|
|
|
|
defer v.snapshotMu.Unlock()
|
|
|
|
|
|
|
|
return v.snapshot
|
|
|
|
}
|
|
|
|
|
2020-01-09 23:45:57 -07:00
|
|
|
func (v *view) initialize(ctx context.Context, s *snapshot) {
|
2019-12-19 12:31:39 -07:00
|
|
|
v.initializeOnce.Do(func() {
|
|
|
|
defer close(v.initialized)
|
|
|
|
|
2020-01-14 22:22:42 -07:00
|
|
|
v.initializationError = func() error {
|
2020-01-11 18:29:13 -07:00
|
|
|
// Do not cancel the call to go/packages.Load for the entire workspace.
|
2020-01-14 22:22:42 -07:00
|
|
|
meta, err := s.load(ctx, directoryURI(v.folder), packagePath("builtin"))
|
2020-01-11 18:29:13 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-14 22:22:42 -07:00
|
|
|
// Keep track of the workspace packages.
|
2020-01-11 18:29:13 -07:00
|
|
|
for _, m := range meta {
|
2020-01-14 22:22:42 -07:00
|
|
|
// Make sure to handle the builtin package separately
|
|
|
|
// Don't set it as a workspace package.
|
|
|
|
if m.pkgPath == "builtin" {
|
|
|
|
if err := s.view.buildBuiltinPackage(ctx, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// A test variant of a package can only be loaded directly by loading
|
|
|
|
// the non-test variant with -test. Track the import path of the non-test variant.
|
2020-01-16 12:32:14 -07:00
|
|
|
pkgPath := m.pkgPath
|
|
|
|
if m.forTest != "" {
|
|
|
|
pkgPath = m.forTest
|
|
|
|
}
|
|
|
|
s.setWorkspacePackage(m.id, pkgPath)
|
2020-01-14 16:25:22 -07:00
|
|
|
if _, err := s.packageHandle(ctx, m.id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
}
|
|
|
|
return nil
|
2020-01-14 22:22:42 -07:00
|
|
|
}()
|
2019-12-19 12:31:39 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
func (v *view) awaitInitialized(ctx context.Context) error {
|
2019-12-19 12:31:39 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2020-01-10 15:18:59 -07:00
|
|
|
case <-v.initialized:
|
2019-12-19 12:31:39 -07:00
|
|
|
}
|
2020-01-10 15:18:59 -07:00
|
|
|
return v.initializationError
|
2019-12-19 12:31:39 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:53:57 -07:00
|
|
|
// invalidateContent invalidates the content of a Go file,
|
|
|
|
// including any position and type information that depends on it.
|
|
|
|
// It returns true if we were already tracking the given file, false otherwise.
|
2019-12-17 16:57:54 -07:00
|
|
|
func (v *view) invalidateContent(ctx context.Context, uri span.URI, kind source.FileKind, action source.FileAction) source.Snapshot {
|
2019-12-19 13:47:07 -07:00
|
|
|
// Detach the context so that content invalidation cannot be canceled.
|
|
|
|
ctx = xcontext.Detach(ctx)
|
|
|
|
|
2019-12-17 14:53:57 -07:00
|
|
|
// Cancel all still-running previous requests, since they would be
|
|
|
|
// operating on stale data.
|
2020-01-09 20:45:06 -07:00
|
|
|
//
|
|
|
|
// TODO(rstambler): All actions should lead to cancellation,
|
|
|
|
// but this will only be possible when all text synchronization events
|
|
|
|
// trigger diagnostics.
|
2019-12-17 14:53:57 -07:00
|
|
|
switch action {
|
2020-01-09 20:45:06 -07:00
|
|
|
case source.Save:
|
|
|
|
default:
|
2019-12-17 14:53:57 -07:00
|
|
|
v.cancelBackground()
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:18:59 -07:00
|
|
|
// Do not clone a snapshot until its view has finished initializing.
|
|
|
|
_ = v.awaitInitialized(ctx)
|
2019-12-19 12:31:39 -07:00
|
|
|
|
2019-12-17 14:53:57 -07:00
|
|
|
// This should be the only time we hold the view's snapshot lock for any period of time.
|
|
|
|
v.snapshotMu.Lock()
|
|
|
|
defer v.snapshotMu.Unlock()
|
|
|
|
|
2020-01-22 17:56:26 -07:00
|
|
|
v.snapshot = v.snapshot.clone(ctx, []span.URI{uri})
|
2019-12-17 14:53:57 -07:00
|
|
|
return v.snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) cancelBackground() {
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-11 18:29:13 -07:00
|
|
|
func (v *view) setGoEnv(ctx context.Context, dir string, env []string) error {
|
|
|
|
var gocache, gopath bool
|
|
|
|
for _, e := range env {
|
|
|
|
split := strings.Split(e, "=")
|
|
|
|
if len(split) != 2 {
|
2020-01-07 19:37:41 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
switch split[0] {
|
|
|
|
case "GOCACHE":
|
|
|
|
v.gocache = split[1]
|
|
|
|
gocache = true
|
|
|
|
case "GOPATH":
|
|
|
|
v.gopath = split[1]
|
|
|
|
gopath = true
|
2020-01-07 19:37:41 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
b, err := source.InvokeGo(ctx, dir, env, "env", "-json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
envMap := make(map[string]string)
|
|
|
|
decoder := json.NewDecoder(b)
|
|
|
|
if err := decoder.Decode(&envMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !gopath {
|
|
|
|
if gopath, ok := envMap["GOPATH"]; ok {
|
|
|
|
v.gopath = gopath
|
|
|
|
} else {
|
|
|
|
return errors.New("unable to determine GOPATH")
|
2020-01-07 19:37:41 -07:00
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
}
|
|
|
|
if !gocache {
|
|
|
|
if gocache, ok := envMap["GOCACHE"]; ok {
|
|
|
|
v.gocache = gocache
|
|
|
|
} else {
|
|
|
|
return errors.New("unable to determine GOCACHE")
|
2020-01-07 19:37:41 -07:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
if gomod, ok := envMap["GOMOD"]; ok {
|
|
|
|
v.gomod = gomod
|
|
|
|
}
|
|
|
|
return nil
|
2020-01-07 19:37:41 -07:00
|
|
|
}
|