2019-12-11 11:44:39 -07:00
|
|
|
// 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"
|
2020-01-16 12:32:09 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-06-10 23:11:52 -06:00
|
|
|
"path/filepath"
|
2019-12-17 14:13:33 -07:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-12-11 11:44:39 -07:00
|
|
|
|
|
|
|
"golang.org/x/mod/modfile"
|
2020-01-16 12:32:09 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-12-17 14:13:33 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-12-11 11:44:39 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/memoize"
|
2020-03-23 06:35:36 -06:00
|
|
|
"golang.org/x/tools/internal/packagesinternal"
|
2020-01-14 09:10:54 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-12-11 11:44:39 -07:00
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2020-02-06 12:50:26 -07:00
|
|
|
const (
|
|
|
|
ModTidyError = "go mod tidy"
|
|
|
|
SyntaxError = "syntax"
|
|
|
|
)
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
type modKey struct {
|
2020-03-04 11:55:41 -07:00
|
|
|
sessionID string
|
|
|
|
cfg string
|
|
|
|
gomod string
|
|
|
|
view string
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type modTidyKey struct {
|
2020-03-04 11:55:41 -07:00
|
|
|
sessionID string
|
2020-02-14 09:33:11 -07:00
|
|
|
cfg string
|
|
|
|
gomod string
|
|
|
|
imports string
|
|
|
|
unsavedOverlays string
|
|
|
|
view string
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
type modHandle struct {
|
2019-12-11 11:44:39 -07:00
|
|
|
handle *memoize.Handle
|
|
|
|
file source.FileHandle
|
2020-01-16 12:32:09 -07:00
|
|
|
cfg *packages.Config
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
type modData struct {
|
2019-12-11 11:44:39 -07:00
|
|
|
memoize.NoCopy
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// origfh is the file handle for the original go.mod file.
|
|
|
|
origfh source.FileHandle
|
|
|
|
|
|
|
|
// origParsedFile contains the parsed contents that are used to diff with
|
|
|
|
// the ideal contents.
|
|
|
|
origParsedFile *modfile.File
|
|
|
|
|
|
|
|
// origMapper is the column mapper for the original go.mod file.
|
|
|
|
origMapper *protocol.ColumnMapper
|
|
|
|
|
|
|
|
// idealParsedFile contains the parsed contents for the go.mod file
|
|
|
|
// after it has been "tidied".
|
|
|
|
idealParsedFile *modfile.File
|
2020-01-14 14:53:48 -07:00
|
|
|
|
|
|
|
// unusedDeps is the map containing the dependencies that are left after
|
|
|
|
// removing the ones that are identical in the original and ideal go.mods.
|
|
|
|
unusedDeps map[string]*modfile.Require
|
|
|
|
|
|
|
|
// missingDeps is the map containing the dependencies that are left after
|
|
|
|
// removing the ones that are identical in the original and ideal go.mods.
|
|
|
|
missingDeps map[string]*modfile.Require
|
|
|
|
|
2020-02-06 12:50:26 -07:00
|
|
|
// upgrades is a map of path->version that contains any upgrades for the go.mod.
|
|
|
|
upgrades map[string]string
|
|
|
|
|
2020-02-20 09:17:54 -07:00
|
|
|
// why is a map of path->explanation that contains all the "go mod why" contents
|
|
|
|
// for each require statement.
|
|
|
|
why map[string]string
|
|
|
|
|
2020-01-14 14:53:48 -07:00
|
|
|
// parseErrors are the errors that arise when we diff between a user's go.mod
|
|
|
|
// and the "tidied" go.mod.
|
|
|
|
parseErrors []source.Error
|
|
|
|
|
|
|
|
// err is any error that occurs while we are calculating the parseErrors.
|
|
|
|
err error
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
func (mh *modHandle) String() string {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
return mh.File().URI().Filename()
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
func (mh *modHandle) File() source.FileHandle {
|
|
|
|
return mh.file
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
func (mh *modHandle) Parse(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, error) {
|
|
|
|
v := mh.handle.Get(ctx)
|
2020-02-06 12:50:26 -07:00
|
|
|
if v == nil {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
return nil, nil, errors.Errorf("no parsed file for %s", mh.File().URI())
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
data := v.(*modData)
|
|
|
|
return data.origParsedFile, data.origMapper, data.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *modHandle) Upgrades(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]string, error) {
|
|
|
|
v := mh.handle.Get(ctx)
|
|
|
|
if v == nil {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
return nil, nil, nil, errors.Errorf("no parsed file for %s", mh.File().URI())
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
|
|
|
data := v.(*modData)
|
2020-02-06 12:50:26 -07:00
|
|
|
return data.origParsedFile, data.origMapper, data.upgrades, data.err
|
|
|
|
}
|
|
|
|
|
2020-02-20 09:17:54 -07:00
|
|
|
func (mh *modHandle) Why(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]string, error) {
|
|
|
|
v := mh.handle.Get(ctx)
|
|
|
|
if v == nil {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
return nil, nil, nil, errors.Errorf("no parsed file for %s", mh.File().URI())
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
|
|
|
data := v.(*modData)
|
|
|
|
return data.origParsedFile, data.origMapper, data.why, data.err
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
func (s *snapshot) ModHandle(ctx context.Context, modFH source.FileHandle) (source.ModHandle, error) {
|
2020-06-10 14:13:25 -06:00
|
|
|
if err := s.awaitLoaded(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
var sumFH source.FileHandle
|
|
|
|
if s.view.sumURI != "" {
|
|
|
|
var err error
|
|
|
|
sumFH, err = s.GetFile(ctx, s.view.sumURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
cfg = s.config(ctx)
|
|
|
|
modURI = s.view.modURI
|
|
|
|
tmpMod = s.view.tmpMod
|
|
|
|
)
|
|
|
|
if handle := s.getModHandle(modFH.URI()); handle != nil {
|
2020-06-10 14:13:25 -06:00
|
|
|
return handle, nil
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
|
|
|
key := modKey{
|
2020-03-04 11:55:41 -07:00
|
|
|
sessionID: s.view.session.id,
|
|
|
|
cfg: hashConfig(cfg),
|
2020-06-10 23:11:52 -06:00
|
|
|
gomod: modFH.Identity().String(),
|
|
|
|
view: s.view.folder.Filename(),
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
h := s.view.session.cache.store.Bind(key, func(ctx context.Context) interface{} {
|
2020-06-10 23:11:52 -06:00
|
|
|
ctx, done := event.Start(ctx, "cache.ModHandle", tag.URI.Of(modFH.URI()))
|
2020-02-06 12:50:26 -07:00
|
|
|
defer done()
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
contents, err := modFH.Read()
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
2020-02-18 13:47:38 -07:00
|
|
|
return &modData{
|
|
|
|
err: err,
|
|
|
|
}
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
parsedFile, err := modfile.Parse(modFH.URI().Filename(), contents, nil)
|
2020-02-14 11:34:48 -07:00
|
|
|
if err != nil {
|
2020-02-18 13:47:38 -07:00
|
|
|
return &modData{
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := &modData{
|
2020-06-10 23:11:52 -06:00
|
|
|
origfh: modFH,
|
2020-02-18 13:47:38 -07:00
|
|
|
origParsedFile: parsedFile,
|
|
|
|
origMapper: &protocol.ColumnMapper{
|
2020-06-10 23:11:52 -06:00
|
|
|
URI: modFH.URI(),
|
|
|
|
Converter: span.NewContentConverter(modFH.URI().Filename(), contents),
|
2020-02-18 13:47:38 -07:00
|
|
|
Content: contents,
|
|
|
|
},
|
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
|
|
|
|
// If this go.mod file is not the view's go.mod file, or if the
|
|
|
|
// -modfile flag is not supported, then we just want to parse.
|
|
|
|
if modFH.URI() != modURI {
|
2020-02-14 11:34:48 -07:00
|
|
|
return data
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
|
|
|
|
// Only get dependency upgrades if the go.mod file is the same as the view's.
|
2020-06-10 23:11:52 -06:00
|
|
|
if err := dependencyUpgrades(ctx, cfg, modFH, sumFH, tmpMod, data); err != nil {
|
|
|
|
return &modData{err: err}
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
|
|
|
// Only run "go mod why" if the go.mod file is the same as the view's.
|
2020-06-10 23:11:52 -06:00
|
|
|
if err := goModWhy(ctx, cfg, modFH, sumFH, tmpMod, data); err != nil {
|
|
|
|
return &modData{err: err}
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-02-06 12:50:26 -07:00
|
|
|
return data
|
|
|
|
})
|
2020-02-18 13:47:38 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2020-06-10 23:11:52 -06:00
|
|
|
s.modHandles[modFH.URI()] = &modHandle{
|
2020-02-06 12:50:26 -07:00
|
|
|
handle: h,
|
2020-06-10 23:11:52 -06:00
|
|
|
file: modFH,
|
2020-02-06 12:50:26 -07:00
|
|
|
cfg: cfg,
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
return s.modHandles[modFH.URI()], nil
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
func goModWhy(ctx context.Context, cfg *packages.Config, modFH, sumFH source.FileHandle, tmpMod bool, data *modData) error {
|
2020-02-20 09:17:54 -07:00
|
|
|
if len(data.origParsedFile.Require) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
// Run "go mod why" on all the dependencies.
|
|
|
|
args := []string{"why", "-m"}
|
2020-02-20 09:17:54 -07:00
|
|
|
for _, req := range data.origParsedFile.Require {
|
2020-06-10 23:11:52 -06:00
|
|
|
args = append(args, req.Mod.Path)
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
// If the -modfile flag is disabled, don't pass in a go.mod URI.
|
|
|
|
if !tmpMod {
|
|
|
|
modFH = nil
|
|
|
|
}
|
|
|
|
_, stdout, err := runGoCommand(ctx, cfg, modFH, sumFH, "mod", args)
|
2020-02-20 09:17:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
whyList := strings.Split(stdout.String(), "\n\n")
|
2020-03-23 21:12:50 -06:00
|
|
|
if len(whyList) <= 1 || len(whyList) != len(data.origParsedFile.Require) {
|
2020-02-20 09:17:54 -07:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-23 21:12:50 -06:00
|
|
|
data.why = make(map[string]string, len(data.origParsedFile.Require))
|
2020-02-20 09:17:54 -07:00
|
|
|
for i, req := range data.origParsedFile.Require {
|
|
|
|
data.why[req.Mod.Path] = whyList[i]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
func dependencyUpgrades(ctx context.Context, cfg *packages.Config, modFH, sumFH source.FileHandle, tmpMod bool, data *modData) error {
|
2020-02-18 13:47:38 -07:00
|
|
|
if len(data.origParsedFile.Require) == 0 {
|
2020-02-20 09:17:54 -07:00
|
|
|
return nil
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-05-06 04:05:41 -06:00
|
|
|
// Run "go list -mod readonly -u -m all" to be able to see which deps can be
|
|
|
|
// upgraded without modifying mod file.
|
2020-06-10 23:11:52 -06:00
|
|
|
args := []string{"-u", "-m", "all"}
|
|
|
|
if !tmpMod || containsVendor(modFH.URI()) {
|
|
|
|
// Use -mod=readonly if the module contains a vendor directory
|
|
|
|
// (see golang/go#38711).
|
|
|
|
args = append([]string{"-mod", "readonly"}, args...)
|
|
|
|
}
|
|
|
|
// If the -modfile flag is disabled, don't pass in a go.mod URI.
|
|
|
|
if !tmpMod {
|
|
|
|
modFH = nil
|
2020-02-24 14:46:08 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
_, stdout, err := runGoCommand(ctx, cfg, modFH, sumFH, "list", args)
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
2020-02-20 09:17:54 -07:00
|
|
|
return err
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
upgradesList := strings.Split(stdout.String(), "\n")
|
|
|
|
if len(upgradesList) <= 1 {
|
2020-02-20 09:17:54 -07:00
|
|
|
return nil
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-02-20 09:17:54 -07:00
|
|
|
data.upgrades = make(map[string]string)
|
2020-02-06 12:50:26 -07:00
|
|
|
for _, upgrade := range upgradesList[1:] {
|
|
|
|
// Example: "github.com/x/tools v1.1.0 [v1.2.0]"
|
|
|
|
info := strings.Split(upgrade, " ")
|
|
|
|
if len(info) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dep, version := info[0], info[2]
|
|
|
|
latest := version[1:] // remove the "["
|
|
|
|
latest = strings.TrimSuffix(latest, "]") // remove the "]"
|
2020-02-20 09:17:54 -07:00
|
|
|
data.upgrades[dep] = latest
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-02-20 09:17:54 -07:00
|
|
|
return nil
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
// containsVendor reports whether the module has a vendor folder.
|
|
|
|
func containsVendor(modURI span.URI) bool {
|
|
|
|
dir := filepath.Dir(modURI.Filename())
|
|
|
|
f, err := os.Stat(filepath.Join(dir, "vendor"))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return f.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
func (mh *modHandle) Tidy(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]*modfile.Require, []source.Error, error) {
|
|
|
|
v := mh.handle.Get(ctx)
|
2020-01-16 12:32:09 -07:00
|
|
|
if v == nil {
|
2020-06-10 23:11:52 -06:00
|
|
|
return nil, nil, nil, nil, errors.Errorf("no tidied file for %s", mh.File().URI())
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
data := v.(*modData)
|
2020-01-14 14:53:48 -07:00
|
|
|
return data.origParsedFile, data.origMapper, data.missingDeps, data.parseErrors, data.err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
func (s *snapshot) ModTidyHandle(ctx context.Context, modFH source.FileHandle) (source.ModTidyHandle, error) {
|
2020-02-14 09:33:11 -07:00
|
|
|
if handle := s.getModTidyHandle(); handle != nil {
|
|
|
|
return handle, nil
|
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
var sumFH source.FileHandle
|
|
|
|
if s.view.sumURI != "" {
|
|
|
|
var err error
|
|
|
|
sumFH, err = s.GetFile(ctx, s.view.sumURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 11:59:27 -07:00
|
|
|
wsPackages, err := s.WorkspacePackages(ctx)
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
imports, err := hashImports(ctx, wsPackages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
|
2020-03-04 11:55:41 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
overlayHash := hashUnsavedOverlays(s.files)
|
|
|
|
s.mu.Unlock()
|
2020-06-10 23:11:52 -06:00
|
|
|
|
|
|
|
var (
|
|
|
|
options = s.View().Options()
|
|
|
|
folder = s.View().Folder()
|
|
|
|
modURI = s.view.modURI
|
|
|
|
tmpMod = s.view.tmpMod
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg := s.config(ctx)
|
2020-02-06 12:50:26 -07:00
|
|
|
key := modTidyKey{
|
2020-03-04 11:55:41 -07:00
|
|
|
sessionID: s.view.session.id,
|
2020-06-10 23:11:52 -06:00
|
|
|
view: folder.Filename(),
|
2020-02-14 09:33:11 -07:00
|
|
|
imports: imports,
|
2020-03-04 11:55:41 -07:00
|
|
|
unsavedOverlays: overlayHash,
|
2020-06-10 23:11:52 -06:00
|
|
|
gomod: modFH.Identity().String(),
|
2020-02-14 09:33:11 -07:00
|
|
|
cfg: hashConfig(cfg),
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
h := s.view.session.cache.store.Bind(key, func(ctx context.Context) interface{} {
|
2020-06-10 23:11:52 -06:00
|
|
|
ctx, done := event.Start(ctx, "cache.ModTidyHandle", tag.URI.Of(modURI))
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
// Do nothing if the -modfile flag is disabled or if the given go.mod
|
|
|
|
// is outside of our view.
|
|
|
|
if modURI != modFH.URI() || !tmpMod {
|
2020-03-05 13:40:08 -07:00
|
|
|
return &modData{}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
contents, err := modFH.Read()
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-06-10 23:11:52 -06:00
|
|
|
return &modData{err: err}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
realMapper := &protocol.ColumnMapper{
|
2020-06-10 23:11:52 -06:00
|
|
|
URI: modURI,
|
|
|
|
Converter: span.NewContentConverter(modURI.Filename(), contents),
|
|
|
|
Content: contents,
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
origParsedFile, err := modfile.Parse(modURI.Filename(), contents, nil)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-06-10 23:11:52 -06:00
|
|
|
if parseErr, err := extractModParseErrors(ctx, modURI, realMapper, err, contents); err == nil {
|
|
|
|
return &modData{parseErrors: []source.Error{parseErr}}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
return &modData{err: err}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
tmpURI, inv, cleanup, err := goCommandInvocation(ctx, cfg, modFH, sumFH, "mod", []string{"tidy"})
|
|
|
|
if err != nil {
|
|
|
|
return &modData{err: err}
|
2020-02-14 11:34:48 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
// Keep the temporary go.mod file around long enough to parse it.
|
|
|
|
defer cleanup()
|
2020-02-14 11:34:48 -07:00
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
if _, err := packagesinternal.GetGoCmdRunner(cfg).Run(ctx, *inv); err != nil {
|
|
|
|
return &modData{err: err}
|
2020-02-14 11:34:48 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
// Go directly to disk to get the temporary mod file, since it is
|
|
|
|
// always on disk.
|
|
|
|
tempContents, err := ioutil.ReadFile(tmpURI.Filename())
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-06-10 23:11:52 -06:00
|
|
|
return &modData{err: err}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
idealParsedFile, err := modfile.Parse(tmpURI.Filename(), tempContents, nil)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-06-10 23:11:52 -06:00
|
|
|
// We do not need to worry about the temporary file's parse errors
|
|
|
|
// since it has been "tidied".
|
|
|
|
return &modData{err: err}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-03-05 13:40:08 -07:00
|
|
|
data := &modData{
|
2020-06-10 23:11:52 -06:00
|
|
|
origfh: modFH,
|
2020-01-16 12:32:09 -07:00
|
|
|
origParsedFile: origParsedFile,
|
|
|
|
origMapper: realMapper,
|
|
|
|
idealParsedFile: idealParsedFile,
|
2020-01-14 14:53:48 -07:00
|
|
|
unusedDeps: make(map[string]*modfile.Require, len(origParsedFile.Require)),
|
|
|
|
missingDeps: make(map[string]*modfile.Require, len(idealParsedFile.Require)),
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-06-10 23:11:52 -06:00
|
|
|
// Get the dependencies that are different between the original and
|
|
|
|
// ideal go.mod files.
|
2020-01-14 14:53:48 -07:00
|
|
|
for _, req := range origParsedFile.Require {
|
|
|
|
data.unusedDeps[req.Mod.Path] = req
|
|
|
|
}
|
|
|
|
for _, req := range idealParsedFile.Require {
|
|
|
|
origDep := data.unusedDeps[req.Mod.Path]
|
|
|
|
if origDep != nil && origDep.Indirect == req.Indirect {
|
|
|
|
delete(data.unusedDeps, req.Mod.Path)
|
|
|
|
} else {
|
|
|
|
data.missingDeps[req.Mod.Path] = req
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
data.parseErrors, data.err = modRequireErrors(options, data)
|
2020-01-14 14:53:48 -07:00
|
|
|
|
|
|
|
for _, req := range data.missingDeps {
|
|
|
|
if data.unusedDeps[req.Mod.Path] != nil {
|
|
|
|
delete(data.missingDeps, req.Mod.Path)
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
})
|
2020-02-14 09:33:11 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.modTidyHandle = &modHandle{
|
2020-01-16 12:32:09 -07:00
|
|
|
handle: h,
|
2020-06-10 23:11:52 -06:00
|
|
|
file: modFH,
|
2020-01-16 12:32:09 -07:00
|
|
|
cfg: cfg,
|
2020-02-14 09:33:11 -07:00
|
|
|
}
|
|
|
|
return s.modTidyHandle, nil
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// extractModParseErrors processes the raw errors returned by modfile.Parse,
|
|
|
|
// extracting the filenames and line numbers that correspond to the errors.
|
|
|
|
func extractModParseErrors(ctx context.Context, uri span.URI, m *protocol.ColumnMapper, parseErr error, content []byte) (source.Error, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
re := regexp.MustCompile(`.*:([\d]+): (.+)`)
|
2020-01-16 12:32:09 -07:00
|
|
|
matches := re.FindStringSubmatch(strings.TrimSpace(parseErr.Error()))
|
2020-01-16 12:32:09 -07:00
|
|
|
if len(matches) < 3 {
|
2020-03-07 19:28:21 -07:00
|
|
|
event.Error(ctx, "could not parse golang/x/mod error message", parseErr)
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, parseErr
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
line, err := strconv.Atoi(matches[1])
|
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, parseErr
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
lines := strings.Split(string(content), "\n")
|
2020-01-16 12:32:09 -07:00
|
|
|
if len(lines) <= line {
|
|
|
|
return source.Error{}, errors.Errorf("could not parse goland/x/mod error message, line number out of range")
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// The error returned from the modfile package only returns a line number,
|
|
|
|
// so we assume that the diagnostic should be for the entire line.
|
2020-01-16 12:32:09 -07:00
|
|
|
endOfLine := len(lines[line-1])
|
|
|
|
sOffset, err := m.Converter.ToOffset(line, 0)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
eOffset, err := m.Converter.ToOffset(line, endOfLine)
|
2019-12-11 11:44:39 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
spn := span.New(uri, span.NewPoint(line, 0, sOffset), span.NewPoint(line, endOfLine, eOffset))
|
|
|
|
rng, err := m.Range(spn)
|
2019-12-11 11:44:39 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
return source.Error{
|
|
|
|
Category: SyntaxError,
|
|
|
|
Message: matches[2],
|
|
|
|
Range: rng,
|
|
|
|
URI: uri,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// modRequireErrors extracts the errors that occur on the require directives.
|
|
|
|
// It checks for directness issues and unused dependencies.
|
2020-03-27 10:52:40 -06:00
|
|
|
func modRequireErrors(options source.Options, data *modData) ([]source.Error, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
var errors []source.Error
|
2020-02-18 13:47:38 -07:00
|
|
|
for dep, req := range data.unusedDeps {
|
2020-01-16 12:32:09 -07:00
|
|
|
if req.Syntax == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Handle dependencies that are incorrectly labeled indirect and vice versa.
|
2020-02-18 13:47:38 -07:00
|
|
|
if data.missingDeps[dep] != nil && req.Indirect != data.missingDeps[dep].Indirect {
|
2020-03-27 10:52:40 -06:00
|
|
|
directErr, err := modDirectnessErrors(options, data, req)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, directErr)
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// Handle unused dependencies.
|
2020-02-18 13:47:38 -07:00
|
|
|
if data.missingDeps[dep] == nil {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
rng, err := rangeFromPositions(data.origfh.URI(), data.origMapper, req.Syntax.Start, req.Syntax.End)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
edits, err := dropDependencyEdits(options, data, req)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, source.Error{
|
|
|
|
Category: ModTidyError,
|
|
|
|
Message: fmt.Sprintf("%s is not used in this module.", dep),
|
|
|
|
Range: rng,
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
URI: data.origfh.URI(),
|
2020-01-14 14:53:48 -07:00
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Remove dependency: %s", dep),
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
Edits: map[span.URI][]protocol.TextEdit{data.origfh.URI(): edits},
|
2020-01-14 14:53:48 -07:00
|
|
|
}},
|
2020-01-16 12:32:09 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// modDirectnessErrors extracts errors when a dependency is labeled indirect when it should be direct and vice versa.
|
2020-03-27 10:52:40 -06:00
|
|
|
func modDirectnessErrors(options source.Options, data *modData, req *modfile.Require) (source.Error, error) {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
rng, err := rangeFromPositions(data.origfh.URI(), data.origMapper, req.Syntax.Start, req.Syntax.End)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
if req.Indirect {
|
|
|
|
// If the dependency should be direct, just highlight the // indirect.
|
|
|
|
if comments := req.Syntax.Comment(); comments != nil && len(comments.Suffix) > 0 {
|
|
|
|
end := comments.Suffix[0].Start
|
|
|
|
end.LineRune += len(comments.Suffix[0].Token)
|
|
|
|
end.Byte += len([]byte(comments.Suffix[0].Token))
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
rng, err = rangeFromPositions(data.origfh.URI(), data.origMapper, comments.Suffix[0].Start, end)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
edits, err := changeDirectnessEdits(options, data, req, false)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{
|
|
|
|
Category: ModTidyError,
|
|
|
|
Message: fmt.Sprintf("%s should be a direct dependency.", req.Mod.Path),
|
|
|
|
Range: rng,
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
URI: data.origfh.URI(),
|
2020-01-16 12:32:09 -07:00
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Make %s direct", req.Mod.Path),
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
Edits: map[span.URI][]protocol.TextEdit{data.origfh.URI(): edits},
|
2020-01-16 12:32:09 -07:00
|
|
|
}},
|
2020-01-16 12:32:09 -07:00
|
|
|
}, nil
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// If the dependency should be indirect, add the // indirect.
|
2020-03-27 10:52:40 -06:00
|
|
|
edits, err := changeDirectnessEdits(options, data, req, true)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{
|
|
|
|
Category: ModTidyError,
|
|
|
|
Message: fmt.Sprintf("%s should be an indirect dependency.", req.Mod.Path),
|
|
|
|
Range: rng,
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
URI: data.origfh.URI(),
|
2020-01-16 12:32:09 -07:00
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Make %s indirect", req.Mod.Path),
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
Edits: map[span.URI][]protocol.TextEdit{data.origfh.URI(): edits},
|
2020-01-16 12:32:09 -07:00
|
|
|
}},
|
2020-01-16 12:32:09 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// dropDependencyEdits gets the edits needed to remove the dependency from the go.mod file.
|
|
|
|
// As an example, this function will codify the edits needed to convert the before go.mod file to the after.
|
|
|
|
// Before:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
|
|
|
//
|
|
|
|
// require golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee
|
|
|
|
// After:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
2020-03-27 10:52:40 -06:00
|
|
|
func dropDependencyEdits(options source.Options, data *modData, req *modfile.Require) ([]protocol.TextEdit, error) {
|
2020-02-18 13:47:38 -07:00
|
|
|
if err := data.origParsedFile.DropRequire(req.Mod.Path); err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
data.origParsedFile.Cleanup()
|
|
|
|
newContents, err := data.origParsedFile.Format()
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return nil, err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// Reset the *modfile.File back to before we dropped the dependency.
|
2020-02-18 13:47:38 -07:00
|
|
|
data.origParsedFile.AddNewRequire(req.Mod.Path, req.Mod.Version, req.Indirect)
|
2020-01-16 12:32:09 -07:00
|
|
|
// Calculate the edits to be made due to the change.
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
diff := options.ComputeEdits(data.origfh.URI(), string(data.origMapper.Content), string(newContents))
|
2020-02-18 13:47:38 -07:00
|
|
|
edits, err := source.ToProtocolEdits(data.origMapper, diff)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return edits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeDirectnessEdits gets the edits needed to change an indirect dependency to direct and vice versa.
|
|
|
|
// As an example, this function will codify the edits needed to convert the before go.mod file to the after.
|
|
|
|
// Before:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
|
|
|
//
|
|
|
|
// require golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee
|
|
|
|
// After:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
|
|
|
//
|
|
|
|
// require golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee // indirect
|
2020-03-27 10:52:40 -06:00
|
|
|
func changeDirectnessEdits(options source.Options, data *modData, req *modfile.Require, indirect bool) ([]protocol.TextEdit, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
var newReq []*modfile.Require
|
|
|
|
prevIndirect := false
|
|
|
|
// Change the directness in the matching require statement.
|
2020-02-18 13:47:38 -07:00
|
|
|
for _, r := range data.origParsedFile.Require {
|
2020-01-16 12:32:09 -07:00
|
|
|
if req.Mod.Path == r.Mod.Path {
|
|
|
|
prevIndirect = req.Indirect
|
|
|
|
req.Indirect = indirect
|
|
|
|
}
|
|
|
|
newReq = append(newReq, r)
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
data.origParsedFile.SetRequire(newReq)
|
|
|
|
data.origParsedFile.Cleanup()
|
|
|
|
newContents, err := data.origParsedFile.Format()
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Change the dependency back to the way it was before we got the newContents.
|
2020-02-18 13:47:38 -07:00
|
|
|
for _, r := range data.origParsedFile.Require {
|
2020-01-16 12:32:09 -07:00
|
|
|
if req.Mod.Path == r.Mod.Path {
|
|
|
|
req.Indirect = prevIndirect
|
|
|
|
}
|
|
|
|
newReq = append(newReq, r)
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
data.origParsedFile.SetRequire(newReq)
|
2020-01-16 12:32:09 -07:00
|
|
|
// Calculate the edits to be made due to the change.
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
diff := options.ComputeEdits(data.origfh.URI(), string(data.origMapper.Content), string(newContents))
|
2020-02-18 13:47:38 -07:00
|
|
|
edits, err := source.ToProtocolEdits(data.origMapper, diff)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return nil, err
|
2020-01-14 09:10:54 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return edits, nil
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
func rangeFromPositions(uri span.URI, m *protocol.ColumnMapper, s, e modfile.Position) (protocol.Range, error) {
|
|
|
|
line, col, err := m.Converter.ToPosition(s.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
start := span.NewPoint(line, col, s.Byte)
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
line, col, err = m.Converter.ToPosition(e.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
end := span.NewPoint(line, col, e.Byte)
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
spn := span.New(uri, start, end)
|
|
|
|
rng, err := m.Range(spn)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return rng, nil
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|