Skip to content

Commit

Permalink
deps: update x/tools and gopls to d647fc25 (#955)
Browse files Browse the repository at this point in the history
* cmd/benchcmp: mention deprecation in docs d647fc25
* internal/lsp: handle modifications to the workspace module 78fed78f
* internal/lsp/source/completion: improve import suggestion labels e94ab728
* internal/lsp/cache: set GO111MODULE=auto in 1.16 d56e4e40
* all: fix tests in preparation for GO111MODULE=on by default 3791637d
* internal/lsp/source/completion: convert deep completion to bfs d148ae1e
* internal/imports: use ProcessEnv to filter files 0511c4cc
* gopls/internal/regtest: add expected ranges in completion tests 75ebdcb7
* internal/lsp: always show errors from running commands 60aba8ac
  • Loading branch information
myitcv committed Sep 20, 2020
1 parent 2adbf3d commit 00139d7
Show file tree
Hide file tree
Showing 16 changed files with 763 additions and 524 deletions.
17 changes: 16 additions & 1 deletion cmd/govim/internal/golang_org_x_tools/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,11 @@ func (e *ProcessEnv) goEnv() (map[string]string, error) {
}

func (e *ProcessEnv) matchFile(dir, name string) (bool, error) {
return build.Default.MatchFile(dir, name)
bctx, err := e.buildContext()
if err != nil {
return false, err
}
return bctx.MatchFile(dir, name)
}

// CopyConfig copies the env's configuration into a new env.
Expand Down Expand Up @@ -924,6 +928,17 @@ func (e *ProcessEnv) buildContext() (*build.Context, error) {
dir.SetString(e.WorkingDir)
}

// Since Go 1.11, go/build.Context.Import may invoke 'go list' depending on
// the value in GO111MODULE in the process's environment. We always want to
// run in GOPATH mode when calling Import, so we need to prevent this from
// happening. In Go 1.16, GO111MODULE defaults to "on", so this problem comes
// up more frequently.
//
// HACK: setting any of the Context I/O hooks prevents Import from invoking
// 'go list', regardless of GO111MODULE. This is undocumented, but it's
// unlikely to change before GOPATH support is removed.
ctx.ReadDir = ioutil.ReadDir

return &ctx, nil
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ func checkPackageKey(ctx context.Context, id packageID, pghs []*parseGoHandle, c
b.WriteString(string(dep))
}
for _, cgf := range pghs {
b.WriteString(string(cgf.file.URI()))
b.WriteString(cgf.file.FileIdentity().Hash)
b.WriteString(cgf.file.FileIdentity().String())
}
return packageHandleKey(hashContents(b.Bytes()))
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,18 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) error {
// 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 len(s.view.modules) == 0 {
if len(s.modules) == 0 {
return "", cleanup, nil
}
if s.view.workspaceModule == nil {
return "", cleanup, nil
wsModuleHandle, err := s.getWorkspaceModuleHandle(ctx)
if err != nil {
return "", nil, err
}
file, err := wsModuleHandle.build(ctx, s)
if err != nil {
return "", nil, err
}
content, err := s.view.workspaceModule.Format()
content, err := file.Format()
if err != nil {
return "", cleanup, err
}
Expand Down
98 changes: 6 additions & 92 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package cache
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -173,7 +171,6 @@ func (s *Session) createView(ctx context.Context, name string, folder span.URI,
name: name,
folder: folder,
root: folder,
modules: make(map[span.URI]*moduleRoot),
filesByURI: make(map[span.URI]*fileBase),
filesByBase: make(map[string][]*fileBase),
}
Expand All @@ -194,6 +191,7 @@ func (s *Session) createView(ctx context.Context, name string, folder span.URI,
modTidyHandles: make(map[span.URI]*modTidyHandle),
modUpgradeHandles: make(map[span.URI]*modUpgradeHandle),
modWhyHandles: make(map[span.URI]*modWhyHandle),
modules: make(map[span.URI]*moduleRoot),
}

if v.session.cache.options != nil {
Expand All @@ -206,19 +204,17 @@ func (s *Session) createView(ctx context.Context, name string, folder span.URI,
}

// Find all of the modules in the workspace.
if err := v.findWorkspaceModules(ctx, options); err != nil {
if err := v.snapshot.findWorkspaceModules(ctx, options); err != nil {
return nil, nil, func() {}, err
}

// Now that we have set all required fields,
// check if the view has a valid build configuration.
v.setBuildConfiguration()

// Build the workspace module, if needed.
if options.ExperimentalWorkspaceModule {
if err := v.buildWorkspaceModule(ctx); err != nil {
return nil, nil, func() {}, err
}
// Decide if we should use the workspace module.
if v.determineWorkspaceModuleLocked() {
v.workspaceMode |= usesWorkspaceModule | moduleMode
}

// We have v.goEnv now.
Expand All @@ -242,94 +238,12 @@ func (s *Session) createView(ctx context.Context, name string, folder span.URI,
snapshot := v.snapshot
release := snapshot.generation.Acquire(initCtx)
go func() {
v.initialize(initCtx, snapshot, true)
snapshot.initialize(initCtx, true)
release()
}()
return v, snapshot, snapshot.generation.Acquire(ctx), nil
}

// findWorkspaceModules walks the view's root folder, looking for go.mod files.
// Any that are found are added to the view's set of modules, which are then
// used to construct the workspace module.
//
// It assumes that the caller has not yet created the view, and therefore does
// not lock any of the internal data structures before accessing them.
func (v *View) findWorkspaceModules(ctx context.Context, options *source.Options) error {
// If the user is intentionally limiting their workspace scope, add their
// folder to the roots and return early.
if !options.ExpandWorkspaceToModule {
return nil
}
// The workspace module has been disabled by the user.
if !options.ExperimentalWorkspaceModule {
return nil
}

// Walk the view's folder to find all modules in the view.
root := v.root.Filename()
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// For any path that is not the workspace folder, check if the path
// would be ignored by the go command. Vendor directories also do not
// contain workspace modules.
if info.IsDir() && path != root {
suffix := strings.TrimPrefix(path, root)
switch {
case checkIgnored(suffix),
strings.Contains(filepath.ToSlash(suffix), "/vendor/"):
return filepath.SkipDir
}
}
// We're only interested in go.mod files.
if filepath.Base(path) != "go.mod" {
return nil
}
// At this point, we definitely have a go.mod file in the workspace,
// so add it to the view.
modURI := span.URIFromPath(path)
rootURI := span.URIFromPath(filepath.Dir(path))
v.modules[rootURI] = &moduleRoot{
rootURI: rootURI,
modURI: modURI,
sumURI: span.URIFromPath(sumFilename(modURI)),
}
return nil
})
}

func (v *View) buildWorkspaceModule(ctx context.Context) error {
// If the view has an invalid configuration, don't build the workspace
// module.
if !v.hasValidBuildConfiguration {
return nil
}
// If the view is not in a module and contains no modules, but still has a
// valid workspace configuration, do not create the workspace module.
// It could be using GOPATH or a different build system entirely.
if v.modURI == "" && len(v.modules) == 0 && v.hasValidBuildConfiguration {
return nil
}
v.workspaceMode |= moduleMode

// Don't default to multi-workspace mode if one of the modules contains a
// vendor directory. We still have to decide how to handle vendoring.
for _, mod := range v.modules {
if info, _ := os.Stat(filepath.Join(mod.rootURI.Filename(), "vendor")); info != nil {
return nil
}
}

v.workspaceMode |= usesWorkspaceModule

// If the user does not have a gopls.mod, we need to create one, based on
// modules we found in the user's workspace.
var err error
v.workspaceModule, err = v.snapshot.buildWorkspaceModule(ctx)
return err
}

// View returns the view by name.
func (s *Session) View(name string) source.View {
s.viewMu.Lock()
Expand Down

0 comments on commit 00139d7

Please sign in to comment.