Skip to content

Commit 274b237

Browse files
findleyrgopherbot
authored andcommitted
gopls: add a -severity flag for gopls check
In golang/go#50764, users were reporting having to filter out noisy diagnostics from the output of `gopls check` in CI. This is because there was no differentiation between diagnostics that represent real bugs, and those that are suggestions. By contrast, hint level diagnostics are very unobtrusive in the editor. Add a new -severity flag to control the minimum severity output by gopls check, and set its default to "warning". For golang/go#50764 Change-Id: I48d8bb74371fa6035fef4d2608412b986f509f7b Reviewed-on: https://go-review.googlesource.com/c/tools/+/651616 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 5299dcb commit 274b237

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

gopls/doc/release/v0.19.0.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Configuration Changes
2+
3+
- The `gopls check` subcommant now accepts a `-severity` flag to set a minimum
4+
severity for the diagnostics it reports. By default, the minimum severity
5+
is "warning", so `gopls check` may report fewer diagnostics than before. Set
6+
`-severity=hint` to reproduce the previous behavior.
7+
18
# New features
29

310
## "Eliminate dot import" code action

gopls/internal/cmd/check.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616

1717
// check implements the check verb for gopls.
1818
type check struct {
19-
app *Application
19+
app *Application
20+
Severity string `flag:"severity" help:"minimum diagnostic severity (hint, info, warning, or error)"`
2021
}
2122

2223
func (c *check) Name() string { return "check" }
@@ -35,6 +36,20 @@ Example: show the diagnostic results of this file:
3536
// Run performs the check on the files specified by args and prints the
3637
// results to stdout.
3738
func (c *check) Run(ctx context.Context, args ...string) error {
39+
severityCutoff := protocol.SeverityWarning
40+
switch c.Severity {
41+
case "hint":
42+
severityCutoff = protocol.SeverityHint
43+
case "info":
44+
severityCutoff = protocol.SeverityInformation
45+
case "warning":
46+
// default
47+
case "error":
48+
severityCutoff = protocol.SeverityError
49+
default:
50+
return fmt.Errorf("unrecognized -severity value %q", c.Severity)
51+
}
52+
3853
if len(args) == 0 {
3954
return nil
4055
}
@@ -95,6 +110,9 @@ func (c *check) Run(ctx context.Context, args ...string) error {
95110
file.diagnosticsMu.Unlock()
96111

97112
for _, diag := range diags {
113+
if diag.Severity > severityCutoff { // lower severity value => greater severity, counterintuitively
114+
continue
115+
}
98116
if err := print(file.uri, diag.Range, diag.Message); err != nil {
99117
return err
100118
}

gopls/internal/cmd/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (app *Application) internalCommands() []tool.Application {
284284
func (app *Application) featureCommands() []tool.Application {
285285
return []tool.Application{
286286
&callHierarchy{app: app},
287-
&check{app: app},
287+
&check{app: app, Severity: "warning"},
288288
&codeaction{app: app},
289289
&codelens{app: app},
290290
&definition{app: app},

gopls/internal/cmd/integration_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ var C int
108108
-- c/c2.go --
109109
package c
110110
var C int
111+
-- d/d.go --
112+
package d
113+
114+
import "io/ioutil"
115+
116+
var _ = ioutil.ReadFile
111117
`)
112118

113119
// no files
@@ -141,6 +147,22 @@ var C int
141147
res.checkStdout(`c2.go:2:5-6: C redeclared in this block`)
142148
res.checkStdout(`c.go:2:5-6: - other declaration of C`)
143149
}
150+
151+
// No deprecated (hint) diagnostic without -severity.
152+
{
153+
res := gopls(t, tree, "check", "./d/d.go")
154+
res.checkExit(true)
155+
if len(res.stdout) > 0 {
156+
t.Errorf("check ./d/d.go returned unexpected output:\n%s", res.stdout)
157+
}
158+
}
159+
160+
// Deprecated (hint) diagnostics with -severity=hint
161+
{
162+
res := gopls(t, tree, "check", "-severity=hint", "./d/d.go")
163+
res.checkExit(true)
164+
res.checkStdout(`ioutil.ReadFile is deprecated`)
165+
}
144166
}
145167

146168
// TestCallHierarchy tests the 'call_hierarchy' subcommand (call_hierarchy.go).

gopls/internal/cmd/usage/check.hlp

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ Usage:
66
Example: show the diagnostic results of this file:
77

88
$ gopls check internal/cmd/check.go
9+
-severity=string
10+
minimum diagnostic severity (hint, info, warning, or error) (default "warning")

0 commit comments

Comments
 (0)