1
0
mirror of https://github.com/golang/go synced 2024-10-01 08:38:47 -06:00
go/internal/lsp/cmd/check.go
Heschi Kreinick 3ded1b734d internal/lsp: add and use nonstandard gopls/diagnoseFiles
Switch the command line client, and its tests, away from the hardcoded
30-second timeout and to a newly-added custom request.

Inconveniently for us, the jsonrpc2 package only serializes requests,
not replies. (Notifications are requests for this purpose.) So, for a
flow like this:

diagnoseFiles -->
  <-- publishDiagnostics
  <-- publishDiagnostics
diagnoseFiles <-- (reply)

...there's actually no guarantee that the incoming requests will be
processed before the reply comes in -- it gets to jump the
serialization. The only way to guarantee previous notifications have
been processed is to send another request. I didn't feel like adding
nonstandard notification support, so I just send a fake diagnostic.

Error handling for untyped JSON is hideous so for now we just panic.
Nobody else should be calling these, or if they do it's at their own
risk.

Fixes golang/go#36518.

Change-Id: I63f8df5bb627c9783314a0d38d2dac8721b3c320
Reviewed-on: https://go-review.googlesource.com/c/tools/+/214583
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-01-15 19:23:06 +00:00

73 lines
1.7 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"
"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{}
var uris []span.URI
// 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)
uris = append(uris, uri)
file := conn.AddFile(ctx, uri)
if file.err != nil {
return file.err
}
checking[uri] = file
}
if err := conn.diagnoseFiles(ctx, uris); err != nil {
return err
}
for _, file := range checking {
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
}