2019-12-17 14:13:33 -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 mod provides core features related to go.mod file
|
|
|
|
// handling for use by Go editors and tools.
|
|
|
|
package mod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-25 18:47:34 -06:00
|
|
|
"fmt"
|
2020-07-13 23:50:57 -06:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-07-22 18:29:48 -06:00
|
|
|
"unicode"
|
2019-12-17 14:13:33 -07:00
|
|
|
|
2020-07-13 23:50:57 -06:00
|
|
|
"golang.org/x/mod/modfile"
|
|
|
|
"golang.org/x/mod/module"
|
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"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2020-07-13 23:50:57 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-12-17 14:13:33 -07:00
|
|
|
)
|
|
|
|
|
2020-07-26 16:01:39 -06:00
|
|
|
func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.VersionedFileIdentity][]*source.Diagnostic, error) {
|
2020-06-10 23:11:52 -06:00
|
|
|
uri := snapshot.View().ModFile()
|
2020-06-19 14:31:22 -06:00
|
|
|
if uri == "" {
|
2020-07-14 22:19:10 -06:00
|
|
|
return nil, nil
|
2020-06-19 14:31:22 -06:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
ctx, done := event.Start(ctx, "mod.Diagnostics", tag.URI.Of(uri))
|
2019-12-17 14:13:33 -07:00
|
|
|
defer done()
|
|
|
|
|
2020-06-10 23:11:52 -06:00
|
|
|
fh, err := snapshot.GetFile(ctx, uri)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-07-14 22:19:10 -06:00
|
|
|
return nil, err
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-08-11 19:37:34 -06:00
|
|
|
tidied, err := snapshot.ModTidy(ctx, fh)
|
2020-06-19 17:07:57 -06:00
|
|
|
if err == source.ErrTmpModfileUnsupported {
|
2020-07-14 22:19:10 -06:00
|
|
|
return nil, nil
|
2020-06-19 17:07:57 -06:00
|
|
|
}
|
2020-07-26 16:01:39 -06:00
|
|
|
reports := map[source.VersionedFileIdentity][]*source.Diagnostic{
|
|
|
|
fh.VersionedFileIdentity(): {},
|
2020-07-13 23:50:57 -06:00
|
|
|
}
|
2020-02-06 11:59:27 -07:00
|
|
|
if err != nil {
|
2020-07-14 22:19:10 -06:00
|
|
|
return nil, err
|
2020-02-06 11:59:27 -07:00
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, e := range tidied.Errors {
|
2020-03-31 21:53:42 -06:00
|
|
|
diag := &source.Diagnostic{
|
2020-04-29 15:49:22 -06:00
|
|
|
Message: e.Message,
|
|
|
|
Range: e.Range,
|
|
|
|
Source: e.Category,
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
if e.Category == "syntax" {
|
|
|
|
diag.Severity = protocol.SeverityError
|
|
|
|
} else {
|
|
|
|
diag.Severity = protocol.SeverityWarning
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-07-14 22:19:10 -06:00
|
|
|
fh, err := snapshot.GetFile(ctx, e.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-26 16:01:39 -06:00
|
|
|
reports[fh.VersionedFileIdentity()] = append(reports[fh.VersionedFileIdentity()], diag)
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-07-14 22:19:10 -06:00
|
|
|
return reports, nil
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
|
2020-07-22 18:29:48 -06:00
|
|
|
var moduleAtVersionRe = regexp.MustCompile(`^(?P<module>.*)@(?P<version>.*)$`)
|
2020-07-13 23:50:57 -06:00
|
|
|
|
|
|
|
// ExtractGoCommandError tries to parse errors that come from the go command
|
|
|
|
// and shape them into go.mod diagnostics.
|
|
|
|
func ExtractGoCommandError(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, loadErr error) (*source.Diagnostic, error) {
|
|
|
|
// We try to match module versions in error messages. Some examples:
|
|
|
|
//
|
2020-07-22 18:29:48 -06:00
|
|
|
// example.com@v1.2.2: reading example.com/@v/v1.2.2.mod: no such file or directory
|
|
|
|
// go: github.com/cockroachdb/apd/v2@v2.0.72: reading github.com/cockroachdb/apd/go.mod at revision v2.0.72: unknown revision v2.0.72
|
|
|
|
// go: example.com@v1.2.3 requires\n\trandom.org@v1.2.3: parsing go.mod:\n\tmodule declares its path as: bob.org\n\tbut was required as: random.org
|
2020-07-13 23:50:57 -06:00
|
|
|
//
|
2020-07-22 18:29:48 -06:00
|
|
|
// We split on colons and whitespace, and attempt to match on something
|
|
|
|
// that matches module@version. If we're able to find a match, we try to
|
|
|
|
// find anything that matches it in the go.mod file.
|
2020-07-13 23:50:57 -06:00
|
|
|
var v module.Version
|
2020-07-22 18:29:48 -06:00
|
|
|
fields := strings.FieldsFunc(loadErr.Error(), func(r rune) bool {
|
|
|
|
return unicode.IsSpace(r) || r == ':'
|
|
|
|
})
|
|
|
|
for _, s := range fields {
|
2020-07-13 23:50:57 -06:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
match := moduleAtVersionRe.FindStringSubmatch(s)
|
|
|
|
if match == nil || len(match) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.Path = match[1]
|
|
|
|
v.Version = match[2]
|
|
|
|
if err := module.Check(v.Path, v.Version); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
pm, err := snapshot.ParseMod(ctx, fh)
|
2020-07-13 23:50:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
toDiagnostic := func(line *modfile.Line) (*source.Diagnostic, error) {
|
2020-07-24 11:39:58 -06:00
|
|
|
rng, err := rangeFromPositions(fh.URI(), pm.Mapper, line.Start, line.End)
|
2020-07-13 23:50:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &source.Diagnostic{
|
|
|
|
Message: loadErr.Error(),
|
|
|
|
Range: rng,
|
|
|
|
Severity: protocol.SeverityError,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
// Check if there are any require, exclude, or replace statements that
|
|
|
|
// match this module version.
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, req := range pm.File.Require {
|
2020-07-13 23:50:57 -06:00
|
|
|
if req.Mod != v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toDiagnostic(req.Syntax)
|
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, ex := range pm.File.Exclude {
|
2020-07-13 23:50:57 -06:00
|
|
|
if ex.Mod != v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toDiagnostic(ex.Syntax)
|
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, rep := range pm.File.Replace {
|
2020-07-13 23:50:57 -06:00
|
|
|
if rep.New != v && rep.Old != v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toDiagnostic(rep.Syntax)
|
|
|
|
}
|
2020-07-22 18:29:48 -06:00
|
|
|
// No match for the module path was found in the go.mod file.
|
2020-08-25 18:47:34 -06:00
|
|
|
// Show the error on the module declaration, if one exists.
|
|
|
|
if pm.File.Module == nil {
|
|
|
|
return nil, fmt.Errorf("no module declaration in %s", fh.URI())
|
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
return toDiagnostic(pm.File.Module.Syntax)
|
2020-07-13 23:50:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func rangeFromPositions(uri span.URI, m *protocol.ColumnMapper, s, e modfile.Position) (protocol.Range, error) {
|
|
|
|
toPoint := func(offset int) (span.Point, error) {
|
|
|
|
l, c, err := m.Converter.ToPosition(offset)
|
|
|
|
if err != nil {
|
|
|
|
return span.Point{}, err
|
|
|
|
}
|
|
|
|
return span.NewPoint(l, c, offset), nil
|
|
|
|
}
|
|
|
|
start, err := toPoint(s.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
end, err := toPoint(e.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
return m.Range(span.New(uri, start, end))
|
|
|
|
}
|