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-06-19 17:07:57 -06:00
|
|
|
"fmt"
|
2020-01-16 12:32:09 -07:00
|
|
|
"os"
|
2020-06-10 23:11:52 -06:00
|
|
|
"path/filepath"
|
2020-06-19 17:07:57 -06:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2019-12-17 14:13:33 -07:00
|
|
|
"strings"
|
2019-12-11 11:44:39 -07:00
|
|
|
|
|
|
|
"golang.org/x/mod/modfile"
|
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-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 (
|
2020-06-19 17:07:57 -06:00
|
|
|
SyntaxError = "syntax"
|
2020-02-06 12:50:26 -07:00
|
|
|
)
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type parseModHandle struct {
|
2019-12-11 11:44:39 -07:00
|
|
|
handle *memoize.Handle
|
2020-06-16 20:10:27 -06:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
mod, sum source.FileHandle
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type parseModData struct {
|
2019-12-11 11:44:39 -07:00
|
|
|
memoize.NoCopy
|
|
|
|
|
2020-06-16 20:10:27 -06:00
|
|
|
parsed *modfile.File
|
2020-06-19 17:07:57 -06:00
|
|
|
m *protocol.ColumnMapper
|
2020-01-14 14:53:48 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// parseErrors refers to syntax errors found in the go.mod file.
|
|
|
|
parseErrors []source.Error
|
2020-02-20 09:17:54 -07:00
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// err is any error encountered while parsing the file.
|
2020-01-14 14:53:48 -07:00
|
|
|
err error
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
func (mh *parseModHandle) Mod() source.FileHandle {
|
|
|
|
return mh.mod
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
func (mh *parseModHandle) Sum() source.FileHandle {
|
|
|
|
return mh.sum
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
func (mh *parseModHandle) Parse(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, []source.Error, error) {
|
2020-06-23 20:18:23 -06:00
|
|
|
v, err := mh.handle.Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
data := v.(*parseModData)
|
|
|
|
return data.parsed, data.m, data.parseErrors, data.err
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
func (s *snapshot) ParseModHandle(ctx context.Context, modFH source.FileHandle) (source.ParseModHandle, error) {
|
|
|
|
if handle := s.getModHandle(modFH.URI()); handle != nil {
|
|
|
|
return handle, nil
|
|
|
|
}
|
|
|
|
h := s.view.session.cache.store.Bind(modFH.Identity().String(), func(ctx context.Context) interface{} {
|
|
|
|
_, done := event.Start(ctx, "cache.ParseModHandle", tag.URI.Of(modFH.URI()))
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
contents, err := modFH.Read()
|
|
|
|
if err != nil {
|
|
|
|
return &parseModData{err: err}
|
|
|
|
}
|
|
|
|
m := &protocol.ColumnMapper{
|
|
|
|
URI: modFH.URI(),
|
|
|
|
Converter: span.NewContentConverter(modFH.URI().Filename(), contents),
|
|
|
|
Content: contents,
|
|
|
|
}
|
|
|
|
parsed, err := modfile.Parse(modFH.URI().Filename(), contents, nil)
|
|
|
|
if err != nil {
|
|
|
|
parseErr, _ := extractModParseErrors(modFH.URI(), m, err, contents)
|
|
|
|
var parseErrors []source.Error
|
|
|
|
if parseErr != nil {
|
|
|
|
parseErrors = append(parseErrors, *parseErr)
|
|
|
|
}
|
|
|
|
return &parseModData{
|
|
|
|
parseErrors: parseErrors,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &parseModData{
|
|
|
|
parsed: parsed,
|
|
|
|
m: m,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// 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()))
|
|
|
|
sumFH := s.FindFile(sumURI)
|
|
|
|
if sumFH == nil {
|
|
|
|
fh, err := s.view.session.cache.getFile(ctx, sumURI)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fh.err != nil && !os.IsNotExist(fh.err) {
|
|
|
|
return nil, fh.err
|
|
|
|
}
|
|
|
|
// If the file doesn't exist, we can just keep the go.sum nil.
|
|
|
|
if err != nil || fh.err != nil {
|
|
|
|
sumFH = nil
|
|
|
|
} else {
|
|
|
|
sumFH = fh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.parseModHandles[modFH.URI()] = &parseModHandle{
|
|
|
|
handle: h,
|
|
|
|
mod: modFH,
|
|
|
|
sum: sumFH,
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
return s.parseModHandles[modFH.URI()], nil
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
func sumFilename(modURI span.URI) string {
|
|
|
|
return modURI.Filename()[:len(modURI.Filename())-len("mod")] + "sum"
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractModParseErrors processes the raw errors returned by modfile.Parse,
|
|
|
|
// extracting the filenames and line numbers that correspond to the errors.
|
|
|
|
func extractModParseErrors(uri span.URI, m *protocol.ColumnMapper, parseErr error, content []byte) (*source.Error, error) {
|
|
|
|
re := regexp.MustCompile(`.*:([\d]+): (.+)`)
|
|
|
|
matches := re.FindStringSubmatch(strings.TrimSpace(parseErr.Error()))
|
|
|
|
if len(matches) < 3 {
|
|
|
|
return nil, errors.Errorf("could not parse go.mod error message: %s", parseErr)
|
|
|
|
}
|
|
|
|
line, err := strconv.Atoi(matches[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(content), "\n")
|
|
|
|
if line > len(lines) {
|
|
|
|
return nil, errors.Errorf("could not parse go.mod error message %q, line number %v out of range", content, line)
|
|
|
|
}
|
|
|
|
// The error returned from the modfile package only returns a line number,
|
|
|
|
// so we assume that the diagnostic should be for the entire line.
|
|
|
|
endOfLine := len(lines[line-1])
|
|
|
|
sOffset, err := m.Converter.ToOffset(line, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eOffset, err := m.Converter.ToOffset(line, endOfLine)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
spn := span.New(uri, span.NewPoint(line, 0, sOffset), span.NewPoint(line, endOfLine, eOffset))
|
|
|
|
rng, err := m.Range(spn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &source.Error{
|
|
|
|
Category: SyntaxError,
|
|
|
|
Message: matches[2],
|
|
|
|
Range: rng,
|
|
|
|
URI: uri,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// modKey is uniquely identifies cached data for `go mod why` or dependencies
|
|
|
|
// to upgrade.
|
|
|
|
type modKey struct {
|
|
|
|
sessionID, cfg, mod, view string
|
|
|
|
verb modAction
|
|
|
|
}
|
|
|
|
|
|
|
|
type modAction int
|
|
|
|
|
|
|
|
const (
|
|
|
|
why modAction = iota
|
|
|
|
upgrade
|
|
|
|
)
|
|
|
|
|
|
|
|
type modWhyHandle struct {
|
|
|
|
handle *memoize.Handle
|
|
|
|
|
|
|
|
pmh source.ParseModHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
type modWhyData struct {
|
|
|
|
// why keeps track of the `go mod why` results for each require statement
|
|
|
|
// in the go.mod file.
|
|
|
|
why map[string]string
|
|
|
|
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mwh *modWhyHandle) Why(ctx context.Context) (map[string]string, error) {
|
2020-06-23 20:18:23 -06:00
|
|
|
v, err := mwh.handle.Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
data := v.(*modWhyData)
|
|
|
|
return data.why, data.err
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
func (s *snapshot) ModWhyHandle(ctx context.Context) (source.ModWhyHandle, error) {
|
2020-06-10 14:13:25 -06:00
|
|
|
if err := s.awaitLoaded(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
fh, err := s.GetFile(ctx, s.view.modURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pmh, err := s.ParseModHandle(ctx, fh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-10 23:11:52 -06:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
cfg = s.config(ctx)
|
|
|
|
tmpMod = s.view.tmpMod
|
|
|
|
)
|
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-19 17:07:57 -06:00
|
|
|
mod: pmh.Mod().Identity().String(),
|
2020-06-10 23:11:52 -06:00
|
|
|
view: s.view.folder.Filename(),
|
2020-06-19 17:07:57 -06:00
|
|
|
verb: why,
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
h := s.view.session.cache.store.Bind(key, func(ctx context.Context) interface{} {
|
2020-06-19 17:07:57 -06:00
|
|
|
ctx, done := event.Start(ctx, "cache.ModHandle", tag.URI.Of(pmh.Mod().URI()))
|
2020-02-06 12:50:26 -07:00
|
|
|
defer done()
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
parsed, _, _, err := pmh.Parse(ctx)
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
2020-06-19 17:07:57 -06:00
|
|
|
return &modWhyData{err: err}
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
// No requires to explain.
|
|
|
|
if len(parsed.Require) == 0 {
|
|
|
|
return &modWhyData{}
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
// Run `go mod why` on all the dependencies.
|
|
|
|
args := []string{"why", "-m"}
|
|
|
|
for _, req := range parsed.Require {
|
|
|
|
args = append(args, req.Mod.Path)
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
_, stdout, err := runGoCommand(ctx, cfg, pmh, tmpMod, "mod", args)
|
|
|
|
if err != nil {
|
|
|
|
return &modWhyData{err: err}
|
2020-02-14 11:34:48 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
whyList := strings.Split(stdout.String(), "\n\n")
|
|
|
|
if len(whyList) != len(parsed.Require) {
|
|
|
|
return &modWhyData{
|
|
|
|
err: fmt.Errorf("mismatched number of results: got %v, want %v", len(whyList), len(parsed.Require)),
|
|
|
|
}
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
why := make(map[string]string, len(parsed.Require))
|
|
|
|
for i, req := range parsed.Require {
|
|
|
|
why[req.Mod.Path] = whyList[i]
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
return &modWhyData{why: why}
|
2020-02-06 12:50:26 -07:00
|
|
|
})
|
2020-02-18 13:47:38 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2020-06-19 17:07:57 -06:00
|
|
|
s.modWhyHandle = &modWhyHandle{
|
2020-02-06 12:50:26 -07:00
|
|
|
handle: h,
|
2020-06-19 17:07:57 -06:00
|
|
|
pmh: pmh,
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
return s.modWhyHandle, nil
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type modUpgradeHandle struct {
|
|
|
|
handle *memoize.Handle
|
|
|
|
|
|
|
|
pmh source.ParseModHandle
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type modUpgradeData struct {
|
|
|
|
// upgrades maps modules to their latest versions.
|
|
|
|
upgrades map[string]string
|
|
|
|
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (muh *modUpgradeHandle) Upgrades(ctx context.Context) (map[string]string, error) {
|
2020-06-23 20:18:23 -06:00
|
|
|
v, err := muh.handle.Get(ctx)
|
2020-06-19 17:07:57 -06:00
|
|
|
if v == nil {
|
2020-06-23 20:18:23 -06:00
|
|
|
return nil, err
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
data := v.(*modUpgradeData)
|
|
|
|
return data.upgrades, data.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshot) ModUpgradeHandle(ctx context.Context) (source.ModUpgradeHandle, error) {
|
|
|
|
if err := s.awaitLoaded(ctx); err != nil {
|
|
|
|
return nil, err
|
2020-06-10 23:11:52 -06:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
fh, err := s.GetFile(ctx, s.view.modURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-24 14:46:08 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
pmh, err := s.ParseModHandle(ctx, fh)
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
2020-06-19 17:07:57 -06:00
|
|
|
return nil, err
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
var (
|
|
|
|
cfg = s.config(ctx)
|
|
|
|
tmpMod = s.view.tmpMod
|
|
|
|
)
|
|
|
|
key := modKey{
|
|
|
|
sessionID: s.view.session.id,
|
|
|
|
cfg: hashConfig(cfg),
|
|
|
|
mod: pmh.Mod().Identity().String(),
|
|
|
|
view: s.view.folder.Filename(),
|
|
|
|
verb: upgrade,
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
h := s.view.session.cache.store.Bind(key, func(ctx context.Context) interface{} {
|
|
|
|
ctx, done := event.Start(ctx, "cache.ModUpgradeHandle", tag.URI.Of(pmh.Mod().URI()))
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
parsed, _, _, err := pmh.Parse(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return &modUpgradeData{err: err}
|
|
|
|
}
|
|
|
|
// No requires to upgrade.
|
|
|
|
if len(parsed.Require) == 0 {
|
|
|
|
return &modUpgradeData{}
|
|
|
|
}
|
|
|
|
// Run "go list -mod readonly -u -m all" to be able to see which deps can be
|
|
|
|
// upgraded without modifying mod file.
|
|
|
|
args := []string{"-u", "-m", "all"}
|
|
|
|
if !tmpMod || containsVendor(pmh.Mod().URI()) {
|
|
|
|
// Use -mod=readonly if the module contains a vendor directory
|
|
|
|
// (see golang/go#38711).
|
|
|
|
args = append([]string{"-mod", "readonly"}, args...)
|
|
|
|
}
|
|
|
|
_, stdout, err := runGoCommand(ctx, cfg, pmh, tmpMod, "list", args)
|
|
|
|
if err != nil {
|
|
|
|
return &modUpgradeData{err: err}
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
upgradesList := strings.Split(stdout.String(), "\n")
|
|
|
|
if len(upgradesList) <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
upgrades := make(map[string]string)
|
|
|
|
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 "]"
|
|
|
|
upgrades[dep] = latest
|
|
|
|
}
|
|
|
|
return &modUpgradeData{
|
|
|
|
upgrades: upgrades,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.modUpgradeHandle = &modUpgradeHandle{
|
|
|
|
handle: h,
|
|
|
|
pmh: pmh,
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
return s.modUpgradeHandle, 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()
|
|
|
|
}
|