1
0
mirror of https://github.com/golang/go synced 2024-11-19 06:24:41 -07:00
go/internal/lsp/cmd/check.go
Rebecca Stambler 73cd2cc3b5 internal/lsp: don't run analyses on the entire view
Running staticcheck on the entire workspace causes a slowdown, and most
likely users don't want to see staticcheck reports for every
subdirectory of their workspace. Only run staticcheck on open files.

Also, fixed a staticcheck warning that showed up along the way. Filed
golang/go#35718 to remind ourselves to fix all of the staticcheck warnings
that showed up when we ran gopls with staticcheck on x/tools.

Finally, made sure that we don't send empty diagnostics when diagnosing
the snapshot on start-up, as that is not necessary.

Change-Id: Ic51d1abfc80b1b53397057f06a4cfd7e2dc930f9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208098
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-11-25 22:48:44 +00:00

78 lines
1.9 KiB
Go

// 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 cmd
import (
"context"
"flag"
"fmt"
"time"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)
// check implements the check verb for gopls.
type check struct {
app *Application
}
func (c *check) Name() string { return "check" }
func (c *check) Usage() string { return "<filename>" }
func (c *check) ShortHelp() string { return "show diagnostic results for the specified file" }
func (c *check) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), `
Example: show the diagnostic results of this file:
$ gopls check internal/lsp/cmd/check.go
gopls check flags are:
`)
f.PrintDefaults()
}
// Run performs the check on the files specified by args and prints the
// results to stdout.
func (c *check) Run(ctx context.Context, args ...string) error {
if len(args) == 0 {
// no files, so no results
return nil
}
checking := map[span.URI]*cmdFile{}
// now we ready to kick things off
conn, err := c.app.connect(ctx)
if err != nil {
return err
}
defer conn.terminate(ctx)
for _, arg := range args {
uri := span.FileURI(arg)
file := conn.AddFile(ctx, uri)
if file.err != nil {
return file.err
}
checking[uri] = file
}
// now wait for results
// TODO: maybe conn.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{Command: "gopls-wait-idle"})
for _, file := range checking {
select {
case <-file.hasDiagnostics:
case <-time.After(30 * time.Second):
return errors.Errorf("timed out waiting for results from %v", file.uri)
}
file.diagnosticsMu.Lock()
defer file.diagnosticsMu.Unlock()
for _, d := range file.diagnostics {
spn, err := file.mapper.RangeSpan(d.Range)
if err != nil {
return errors.Errorf("Could not convert position %v for %q", d.Range, d.Message)
}
fmt.Printf("%v: %v\n", spn, d.Message)
}
}
return nil
}