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.
13 "golang.org/x/tools/internal/event"
14 "golang.org/x/tools/internal/lsp/command"
15 "golang.org/x/tools/internal/lsp/debug/tag"
16 "golang.org/x/tools/internal/lsp/protocol"
17 "golang.org/x/tools/internal/lsp/source"
20 func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.VersionedFileIdentity][]*source.Diagnostic, error) {
21 ctx, done := event.Start(ctx, "mod.Diagnostics", tag.Snapshot.Of(snapshot.ID()))
24 reports := map[source.VersionedFileIdentity][]*source.Diagnostic{}
25 for _, uri := range snapshot.ModFiles() {
26 fh, err := snapshot.GetVersionedFile(ctx, uri)
30 reports[fh.VersionedFileIdentity()] = []*source.Diagnostic{}
31 diagnostics, err := DiagnosticsForMod(ctx, snapshot, fh)
35 for _, d := range diagnostics {
36 fh, err := snapshot.GetVersionedFile(ctx, d.URI)
40 reports[fh.VersionedFileIdentity()] = append(reports[fh.VersionedFileIdentity()], d)
46 func DiagnosticsForMod(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]*source.Diagnostic, error) {
47 pm, err := snapshot.ParseMod(ctx, fh)
49 if pm == nil || len(pm.ParseErrors) == 0 {
52 return pm.ParseErrors, nil
55 var diagnostics []*source.Diagnostic
57 // Add upgrade quick fixes for individual modules if we know about them.
58 upgrades := snapshot.View().ModuleUpgrades()
59 for _, req := range pm.File.Require {
60 ver, ok := upgrades[req.Mod.Path]
61 if !ok || req.Mod.Version == ver {
64 rng, err := lineToRange(pm.Mapper, fh.URI(), req.Syntax.Start, req.Syntax.End)
68 // Upgrade to the exact version we offer the user, not the most recent.
69 title := fmt.Sprintf("Upgrade to %v", ver)
70 cmd, err := command.NewUpgradeDependencyCommand(title, command.DependencyArgs{
71 URI: protocol.URIFromSpanURI(fh.URI()),
73 GoCmdArgs: []string{req.Mod.Path + "@" + ver},
78 diagnostics = append(diagnostics, &source.Diagnostic{
81 Severity: protocol.SeverityInformation,
82 Source: source.UpgradeNotification,
83 Message: fmt.Sprintf("%v can be upgraded", req.Mod.Path),
84 SuggestedFixes: []source.SuggestedFix{source.SuggestedFixFromCommand(cmd)},
88 // Packages in the workspace can contribute diagnostics to go.mod files.
89 wspkgs, err := snapshot.WorkspacePackages(ctx)
90 if err != nil && !source.IsNonFatalGoModError(err) {
91 event.Error(ctx, "diagnosing go.mod", err)
94 for _, pkg := range wspkgs {
95 pkgDiagnostics, err := snapshot.DiagnosePackage(ctx, pkg)
99 diagnostics = append(diagnostics, pkgDiagnostics[fh.URI()]...)
103 tidied, err := snapshot.ModTidy(ctx, pm)
104 if err != nil && !source.IsNonFatalGoModError(err) {
105 event.Error(ctx, "diagnosing go.mod", err)
108 for _, d := range tidied.Diagnostics {
109 if d.URI != fh.URI() {
112 diagnostics = append(diagnostics, d)
115 return diagnostics, nil