Skip to content

Commit

Permalink
deps: update x/tools and gopls to f46e4245 (#985)
Browse files Browse the repository at this point in the history
* internal/lsp/cache: handle nil pointer exception in missing module error f46e4245
* internal/lsp: make Diagnostics.CodeDescription a pointer 3f6de077
* go/analysis: add frame pointer check for vet 3288bc1e
* internal/lsp/cache: check for symlinks when checking "isSubdirectory" b53d4cbd
* internal/lsp/cache: set a 15 minute deadline on calls to packages.Load 8860a70d
* internal/lsp: move initialization entirely into the snapshot 51cde522
* internal/lsp: change `go mod vendor` warning into a diagnostic 1f28ee68
* go/analysis/singlechecker: fix whitespace in package documentation 582c62ec
* internal/lsp/cache: keep a cached workspace module dir 4fc0492b
* internal/lsp: delay longer in TestDebouncer 3734b819
* internal/memoize: add a final argument to Bind for cleaning up d36b6f68
* internal/lsp/cache: consider gopls.mod when finding workspace root f239dba4
* internal/lsp/cache: introduce a workspace abstraction d463eb0e
* Revert "internal/lsp: move initialization entirely into the snapshot" 443cd81a
* internal/lsp: move initialization entirely into the snapshot deb1282f
* internal/lsp/source/completion: remove "completion_" prefix from files 8da1a626
* cmd/fiximports,cmd/present,cmd/stringer: update links to pkg.go.dev 589136c8
  • Loading branch information
leitzler committed Nov 3, 2020
1 parent b0af0a3 commit 5ecd028
Show file tree
Hide file tree
Showing 32 changed files with 1,049 additions and 684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (s *snapshot) actionHandle(ctx context.Context, id packageID, a *analysis.A
}
}
return runAnalysis(ctx, snapshot, a, pkg, results)
})
}, nil)
act.handle = h

act = s.addActionHandle(act)
Expand Down
23 changes: 13 additions & 10 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,10 @@ func (c *Cache) getFile(ctx context.Context, uri span.URI) (*fileHandle, error)
return fh, nil
}

select {
case ioLimit <- struct{}{}:
case <-ctx.Done():
return nil, ctx.Err()
fh, err := readFile(ctx, uri, fi.ModTime())
if err != nil {
return nil, err
}
defer func() { <-ioLimit }()

fh = readFile(ctx, uri, fi.ModTime())
c.fileMu.Lock()
c.fileContent[uri] = fh
c.fileMu.Unlock()
Expand All @@ -96,7 +92,14 @@ func (c *Cache) getFile(ctx context.Context, uri span.URI) (*fileHandle, error)
// ioLimit limits the number of parallel file reads per process.
var ioLimit = make(chan struct{}, 128)

func readFile(ctx context.Context, uri span.URI, modTime time.Time) *fileHandle {
func readFile(ctx context.Context, uri span.URI, modTime time.Time) (*fileHandle, error) {
select {
case ioLimit <- struct{}{}:
case <-ctx.Done():
return nil, ctx.Err()
}
defer func() { <-ioLimit }()

ctx, done := event.Start(ctx, "cache.readFile", tag.File.Of(uri.Filename()))
_ = ctx
defer done()
Expand All @@ -106,14 +109,14 @@ func readFile(ctx context.Context, uri span.URI, modTime time.Time) *fileHandle
return &fileHandle{
modTime: modTime,
err: err,
}
}, nil
}
return &fileHandle{
modTime: modTime,
uri: uri,
bytes: data,
hash: hashContents(data),
}
}, nil
}

func (c *Cache) NewSession(ctx context.Context) *Session {
Expand Down
2 changes: 1 addition & 1 deletion cmd/govim/internal/golang_org_x_tools/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *snapshot) buildPackageHandle(ctx context.Context, id packageID, mode so
wg.Wait()

return data
})
}, nil)
ph.handle = h

// Cache the handle in the snapshot. If a package handle has already
Expand Down
31 changes: 14 additions & 17 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,31 @@ func (s *importsState) runProcessEnvFunc(ctx context.Context, snapshot *snapshot
// Use temporary go.mod files, but always go to disk for the contents.
// Rebuilding the cache is expensive, and we don't want to do it for
// transient changes.
var modFH, sumFH source.FileHandle
var modFH source.FileHandle
var gosum []byte
var modFileIdentifier string
var err error
// TODO(heschik): Change the goimports logic to use a persistent workspace
// TODO(rfindley): Change the goimports logic to use a persistent workspace
// module for workspace module mode.
//
// Get the go.mod file that corresponds to this view's root URI. This is
// broken because it assumes that the view's root is a module, but this is
// not more broken than the previous state--it is a temporary hack that
// should be removed ASAP.
var match *moduleRoot
for _, m := range snapshot.modules {
if m.rootURI == snapshot.view.rootURI {
match = m
var matchURI span.URI
for modURI := range snapshot.workspace.activeModFiles() {
if dirURI(modURI) == snapshot.view.rootURI {
matchURI = modURI
}
}
if match != nil {
modFH, err = snapshot.GetFile(ctx, match.modURI)
// TODO(rFindley): should it be an error if matchURI is empty?
if matchURI != "" {
modFH, err = snapshot.GetFile(ctx, matchURI)
if err != nil {
return err
}
modFileIdentifier = modFH.FileIdentity().Hash
if match.sumURI != "" {
sumFH, err = snapshot.GetFile(ctx, match.sumURI)
if err != nil {
return err
}
}
gosum = snapshot.goSum(ctx, matchURI)
}
// v.goEnv is immutable -- changes make a new view. Options can change.
// We can't compare build flags directly because we may add -modfile.
Expand All @@ -87,7 +84,7 @@ func (s *importsState) runProcessEnvFunc(ctx context.Context, snapshot *snapshot
}
s.cachedModFileIdentifier = modFileIdentifier
s.cachedBuildFlags = currentBuildFlags
s.cleanupProcessEnv, err = s.populateProcessEnv(ctx, snapshot, modFH, sumFH)
s.cleanupProcessEnv, err = s.populateProcessEnv(ctx, snapshot, modFH, gosum)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,7 +122,7 @@ func (s *importsState) runProcessEnvFunc(ctx context.Context, snapshot *snapshot

// populateProcessEnv sets the dynamically configurable fields for the view's
// process environment. Assumes that the caller is holding the s.view.importsMu.
func (s *importsState) populateProcessEnv(ctx context.Context, snapshot *snapshot, modFH, sumFH source.FileHandle) (cleanup func(), err error) {
func (s *importsState) populateProcessEnv(ctx context.Context, snapshot *snapshot, modFH source.FileHandle, gosum []byte) (cleanup func(), err error) {
cleanup = func() {}
pe := s.processEnv

Expand Down Expand Up @@ -166,7 +163,7 @@ func (s *importsState) populateProcessEnv(ctx context.Context, snapshot *snapsho
// Add -modfile to the build flags, if we are using it.
if snapshot.workspaceMode()&tempModfile != 0 && modFH != nil {
var tmpURI span.URI
tmpURI, cleanup, err = tempModFile(modFH, sumFH)
tmpURI, cleanup, err = tempModFile(modFH, gosum)
if err != nil {
return nil, err
}
Expand Down
90 changes: 57 additions & 33 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"golang.org/x/tools/go/packages"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/event"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/gocommand"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/lsp/debug/tag"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/lsp/source"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/memoize"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/packagesinternal"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/span"
errors "golang.org/x/xerrors"
Expand Down Expand Up @@ -91,14 +93,19 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) error {
ctx, done := event.Start(ctx, "cache.view.load", tag.Query.Of(query))
defer done()

cleanup := func() {}

_, inv, cleanup, err := s.goCommandInvocation(ctx, source.ForTypeChecking, &gocommand.Invocation{
WorkingDir: s.view.rootURI.Filename(),
})
if err != nil {
return err
}

// Set a last resort deadline on packages.Load since it calls the go
// command, which may hang indefinitely if it has a bug. golang/go#42132
// and golang/go#42255 have more context.
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

cfg := s.config(ctx, inv)
pkgs, err := packages.Load(cfg, query...)
cleanup()
Expand All @@ -110,11 +117,6 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) error {
return ctx.Err()
}
if err != nil {
// Match on common error messages. This is really hacky, but I'm not sure
// of any better way. This can be removed when golang/go#39164 is resolved.
if strings.Contains(err.Error(), "inconsistent vendoring") {
return source.InconsistentVendoring
}
event.Error(ctx, "go/packages.Load", err, tag.Snapshot.Of(s.ID()), tag.Directory.Of(cfg.Dir), tag.Query.Of(query), tag.PackageCount.Of(len(pkgs)))
} else {
event.Log(ctx, "go/packages.Load", tag.Snapshot.Of(s.ID()), tag.Directory.Of(cfg.Dir), tag.Query.Of(query), tag.PackageCount.Of(len(pkgs)))
Expand Down Expand Up @@ -184,40 +186,62 @@ func (s *snapshot) parseLoadError(ctx context.Context, loadErr error) *source.Er
return srcErrs
}

// tempWorkspaceModule creates a temporary directory for use with
// packages.Loads that occur from within the workspace module.
func (s *snapshot) tempWorkspaceModule(ctx context.Context) (_ span.URI, cleanup func(), err error) {
cleanup = func() {}
if s.workspaceMode()&usesWorkspaceModule == 0 {
return "", cleanup, nil
}
wsModuleHandle, err := s.getWorkspaceModuleHandle(ctx)
if err != nil {
return "", nil, err
type workspaceDirKey string

type workspaceDirData struct {
dir string
err error
}

// getWorkspaceDir gets the URI for the workspace directory associated with
// this snapshot. The workspace directory is a temp directory containing the
// go.mod file computed from all active modules.
func (s *snapshot) getWorkspaceDir(ctx context.Context) (span.URI, error) {
s.mu.Lock()
h := s.workspaceDirHandle
s.mu.Unlock()
if h != nil {
return getWorkspaceDir(ctx, h, s.generation)
}
file, err := wsModuleHandle.build(ctx, s)
file, err := s.workspace.modFile(ctx, s)
if err != nil {
return "", nil, err
return "", err
}
content, err := file.Format()
if err != nil {
return "", cleanup, err
return "", err
}
// Create a temporary working directory for the go command that contains
// the workspace module file.
name, err := ioutil.TempDir("", "gopls-mod")
key := workspaceDirKey(hashContents(content))
s.mu.Lock()
s.workspaceDirHandle = s.generation.Bind(key, func(context.Context, memoize.Arg) interface{} {
tmpdir, err := ioutil.TempDir("", "gopls-workspace-mod")
if err != nil {
return &workspaceDirData{err: err}
}
filename := filepath.Join(tmpdir, "go.mod")
if err := ioutil.WriteFile(filename, content, 0644); err != nil {
os.RemoveAll(tmpdir)
return &workspaceDirData{err: err}
}
return &workspaceDirData{dir: tmpdir}
}, func(v interface{}) {
d := v.(*workspaceDirData)
if d.dir != "" {
if err := os.RemoveAll(d.dir); err != nil {
event.Error(context.Background(), "cleaning workspace dir", err)
}
}
})
s.mu.Unlock()
return getWorkspaceDir(ctx, s.workspaceDirHandle, s.generation)
}

func getWorkspaceDir(ctx context.Context, h *memoize.Handle, g *memoize.Generation) (span.URI, error) {
v, err := h.Get(ctx, g, nil)
if err != nil {
return "", cleanup, err
}
cleanup = func() {
os.RemoveAll(name)
}
filename := filepath.Join(name, "go.mod")
if err := ioutil.WriteFile(filename, content, 0644); err != nil {
cleanup()
return "", cleanup, err
return "", err
}
return span.URIFromPath(filepath.Dir(filename)), cleanup, nil
return span.URIFromPath(v.(*workspaceDirData).dir), nil
}

// setMetadata extracts metadata from pkg and records it in s. It
Expand Down
20 changes: 11 additions & 9 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *snapshot) ParseMod(ctx context.Context, modFH source.FileHandle) (*sour
}
}
return data
})
}, nil)

pmh := &parseModHandle{handle: h}
s.mu.Lock()
Expand All @@ -98,24 +98,26 @@ func (s *snapshot) ParseMod(ctx context.Context, modFH source.FileHandle) (*sour
return pmh.parse(ctx, s)
}

func (s *snapshot) sumFH(ctx context.Context, modFH source.FileHandle) (source.FileHandle, error) {
// goSum reads the go.sum file for the go.mod file at modURI, if it exists. If
// it doesn't exist, it returns nil.
func (s *snapshot) goSum(ctx context.Context, modURI span.URI) []byte {
// Get the go.sum file, either from the snapshot or directly from the
// cache. Avoid (*snapshot).GetFile here, as we don't want to add
// nonexistent file handles to the snapshot if the file does not exist.
sumURI := span.URIFromPath(sumFilename(modFH.URI()))
sumURI := span.URIFromPath(sumFilename(modURI))
var sumFH source.FileHandle = s.FindFile(sumURI)
if sumFH == nil {
var err error
sumFH, err = s.view.session.cache.getFile(ctx, sumURI)
if err != nil {
return nil, err
return nil
}
}
_, err := sumFH.Read()
content, err := sumFH.Read()
if err != nil {
return nil, err
return nil
}
return sumFH, nil
return content
}

func sumFilename(modURI span.URI) string {
Expand Down Expand Up @@ -250,7 +252,7 @@ func (s *snapshot) ModWhy(ctx context.Context, fh source.FileHandle) (map[string
why[req.Mod.Path] = whyList[i]
}
return &modWhyData{why: why}
})
}, nil)

mwh := &modWhyHandle{handle: h}
s.mu.Lock()
Expand Down Expand Up @@ -358,7 +360,7 @@ func (s *snapshot) ModUpgrade(ctx context.Context, fh source.FileHandle) (map[st
return &modUpgradeData{
upgrades: upgrades,
}
})
}, nil)
muh := &modUpgradeHandle{handle: h}
s.mu.Lock()
s.modUpgradeHandles[fh.URI()] = muh
Expand Down
Loading

0 comments on commit 5ecd028

Please sign in to comment.