1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package mod provides core features related to go.mod file
6 // handling for use by Go editors and tools.
12 "golang.org/x/tools/internal/event"
13 "golang.org/x/tools/internal/lsp/debug/tag"
14 "golang.org/x/tools/internal/lsp/protocol"
15 "golang.org/x/tools/internal/lsp/source"
18 func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.VersionedFileIdentity][]*source.Diagnostic, error) {
19 ctx, done := event.Start(ctx, "mod.Diagnostics", tag.Snapshot.Of(snapshot.ID()))
22 reports := map[source.VersionedFileIdentity][]*source.Diagnostic{}
23 for _, uri := range snapshot.ModFiles() {
24 fh, err := snapshot.GetVersionedFile(ctx, uri)
28 reports[fh.VersionedFileIdentity()] = []*source.Diagnostic{}
29 errors, err := ErrorsForMod(ctx, snapshot, fh)
33 for _, e := range errors {
34 d := &source.Diagnostic{
39 if e.Category == "syntax" {
40 d.Severity = protocol.SeverityError
42 d.Severity = protocol.SeverityWarning
44 fh, err := snapshot.GetVersionedFile(ctx, e.URI)
48 reports[fh.VersionedFileIdentity()] = append(reports[fh.VersionedFileIdentity()], d)
54 func ErrorsForMod(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]source.Error, error) {
55 tidied, err := snapshot.ModTidy(ctx, fh)
57 if source.IsNonFatalGoModError(err) {
63 return tidied.Errors, nil