2018-12-17 11:52:58 -07:00
|
|
|
// Copyright 2018 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"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2019-02-12 11:16:08 -07:00
|
|
|
"net"
|
2018-12-17 11:52:58 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
2019-05-28 23:38:51 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
2019-08-27 10:52:32 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-07-12 14:33:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2018-12-17 11:52:58 -07:00
|
|
|
"golang.org/x/tools/internal/tool"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2018-12-17 11:52:58 -07:00
|
|
|
)
|
|
|
|
|
2019-02-07 15:05:20 -07:00
|
|
|
// Serve is a struct that exposes the configurable parts of the LSP server as
|
2018-12-17 11:52:58 -07:00
|
|
|
// flags, in the right form for tool.Main to consume.
|
2019-02-07 15:05:20 -07:00
|
|
|
type Serve struct {
|
2018-12-17 11:52:58 -07:00
|
|
|
Logfile string `flag:"logfile" help:"filename to log to. if value is \"auto\", then logging to a default output file is enabled"`
|
|
|
|
Mode string `flag:"mode" help:"no effect"`
|
2019-01-16 15:01:06 -07:00
|
|
|
Port int `flag:"port" help:"port on which to run gopls for debugging purposes"`
|
2019-02-12 11:16:08 -07:00
|
|
|
Address string `flag:"listen" help:"address on which to listen for remote connections"`
|
2019-03-29 17:04:29 -06:00
|
|
|
Trace bool `flag:"rpc.trace" help:"Print the full rpc trace in lsp inspector format"`
|
2019-05-28 23:38:51 -06:00
|
|
|
Debug string `flag:"debug" help:"Serve debug information on the supplied address"`
|
2019-02-07 15:05:20 -07:00
|
|
|
|
|
|
|
app *Application
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 15:05:20 -07:00
|
|
|
func (s *Serve) Name() string { return "serve" }
|
|
|
|
func (s *Serve) Usage() string { return "" }
|
|
|
|
func (s *Serve) ShortHelp() string {
|
2018-12-17 11:52:58 -07:00
|
|
|
return "run a server for Go code using the Language Server Protocol"
|
|
|
|
}
|
2019-02-07 15:05:20 -07:00
|
|
|
func (s *Serve) DetailedHelp(f *flag.FlagSet) {
|
2018-12-17 11:52:58 -07:00
|
|
|
fmt.Fprint(f.Output(), `
|
|
|
|
The server communicates using JSONRPC2 on stdin and stdout, and is intended to be run directly as
|
|
|
|
a child of an editor process.
|
2018-12-14 13:46:12 -07:00
|
|
|
|
|
|
|
gopls server flags are:
|
2018-12-17 11:52:58 -07:00
|
|
|
`)
|
2018-12-14 13:46:12 -07:00
|
|
|
f.PrintDefaults()
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run configures a server based on the flags, and then runs it.
|
|
|
|
// It blocks until the server shuts down.
|
2019-02-07 15:05:20 -07:00
|
|
|
func (s *Serve) Run(ctx context.Context, args ...string) error {
|
2018-12-17 11:52:58 -07:00
|
|
|
if len(args) > 0 {
|
|
|
|
return tool.CommandLineErrorf("server does not take arguments, got %v", args)
|
|
|
|
}
|
|
|
|
out := os.Stderr
|
|
|
|
if s.Logfile != "" {
|
|
|
|
filename := s.Logfile
|
|
|
|
if filename == "auto" {
|
2019-01-16 15:01:06 -07:00
|
|
|
filename = filepath.Join(os.TempDir(), fmt.Sprintf("gopls-%d.log", os.Getpid()))
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
return errors.Errorf("Unable to create log file: %v", err)
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(io.MultiWriter(os.Stderr, f))
|
|
|
|
out = f
|
|
|
|
}
|
2019-05-28 23:38:51 -06:00
|
|
|
|
|
|
|
debug.Serve(ctx, s.Debug)
|
|
|
|
|
2019-02-07 15:05:20 -07:00
|
|
|
if s.app.Remote != "" {
|
|
|
|
return s.forward()
|
|
|
|
}
|
2019-03-29 17:04:29 -06:00
|
|
|
|
|
|
|
// For debugging purposes only.
|
2019-07-10 19:01:12 -06:00
|
|
|
run := func(ctx context.Context, srv *lsp.Server) {
|
2019-05-30 04:13:54 -06:00
|
|
|
go srv.Run(ctx)
|
2019-03-29 17:04:29 -06:00
|
|
|
}
|
|
|
|
if s.Address != "" {
|
2019-05-17 08:51:19 -06:00
|
|
|
return lsp.RunServerOnAddress(ctx, s.app.cache, s.Address, run)
|
2019-03-29 17:04:29 -06:00
|
|
|
}
|
|
|
|
if s.Port != 0 {
|
2019-05-17 08:51:19 -06:00
|
|
|
return lsp.RunServerOnPort(ctx, s.app.cache, s.Port, run)
|
2019-03-29 17:04:29 -06:00
|
|
|
}
|
|
|
|
stream := jsonrpc2.NewHeaderStream(os.Stdin, os.Stdout)
|
2019-08-27 10:52:32 -06:00
|
|
|
if s.Trace {
|
|
|
|
stream = protocol.LoggingStream(stream, out)
|
|
|
|
}
|
2019-07-10 19:01:12 -06:00
|
|
|
ctx, srv := lsp.NewServer(ctx, s.app.cache, stream)
|
2019-05-30 04:13:54 -06:00
|
|
|
return srv.Run(ctx)
|
2019-03-29 17:04:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serve) forward() error {
|
|
|
|
conn, err := net.Dial("tcp", s.app.Remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errc := make(chan error)
|
|
|
|
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
_, err := io.Copy(conn, os.Stdin)
|
|
|
|
errc <- err
|
|
|
|
}(conn)
|
|
|
|
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
_, err := io.Copy(os.Stdout, conn)
|
|
|
|
errc <- err
|
|
|
|
}(conn)
|
|
|
|
|
|
|
|
return <-errc
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:57:35 -06:00
|
|
|
type handler struct {
|
2019-08-27 10:52:32 -06:00
|
|
|
out io.Writer
|
2019-07-11 10:57:35 -06:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:43:12 -06:00
|
|
|
type rpcStats struct {
|
2019-07-09 18:16:21 -06:00
|
|
|
method string
|
|
|
|
direction jsonrpc2.Direction
|
|
|
|
id *jsonrpc2.ID
|
|
|
|
payload *json.RawMessage
|
|
|
|
start time.Time
|
|
|
|
delivering func()
|
|
|
|
close func()
|
2019-07-11 22:43:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type statsKeyType int
|
|
|
|
|
|
|
|
const statsKey = statsKeyType(0)
|
|
|
|
|
2019-07-11 10:57:35 -06:00
|
|
|
func (h *handler) Deliver(ctx context.Context, r *jsonrpc2.Request, delivered bool) bool {
|
2019-07-09 18:16:21 -06:00
|
|
|
stats := h.getStats(ctx)
|
|
|
|
if stats != nil {
|
|
|
|
stats.delivering()
|
|
|
|
}
|
2019-07-11 10:57:35 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Cancel(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID, cancelled bool) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-11 22:43:12 -06:00
|
|
|
func (h *handler) Request(ctx context.Context, direction jsonrpc2.Direction, r *jsonrpc2.WireRequest) context.Context {
|
2019-07-16 14:24:47 -06:00
|
|
|
if r.Method == "" {
|
|
|
|
panic("no method in rpc stats")
|
2019-07-11 22:43:12 -06:00
|
|
|
}
|
2019-07-16 17:56:49 -06:00
|
|
|
stats := &rpcStats{
|
2019-07-11 22:43:12 -06:00
|
|
|
method: r.Method,
|
|
|
|
start: time.Now(),
|
2019-07-16 14:24:47 -06:00
|
|
|
direction: direction,
|
2019-07-11 22:43:12 -06:00
|
|
|
payload: r.Params,
|
|
|
|
}
|
2019-07-16 17:56:49 -06:00
|
|
|
ctx = context.WithValue(ctx, statsKey, stats)
|
2019-07-16 14:24:47 -06:00
|
|
|
mode := telemetry.Outbound
|
|
|
|
if direction == jsonrpc2.Receive {
|
|
|
|
mode = telemetry.Inbound
|
|
|
|
}
|
2019-07-16 17:56:49 -06:00
|
|
|
ctx, stats.close = trace.StartSpan(ctx, r.Method,
|
2019-08-14 10:51:42 -06:00
|
|
|
telemetry.Method.Of(r.Method),
|
|
|
|
telemetry.RPCDirection.Of(mode),
|
|
|
|
telemetry.RPCID.Of(r.ID),
|
2019-07-16 14:24:47 -06:00
|
|
|
)
|
|
|
|
telemetry.Started.Record(ctx, 1)
|
2019-07-09 18:16:21 -06:00
|
|
|
_, stats.delivering = trace.StartSpan(ctx, "queued")
|
2019-07-11 22:43:12 -06:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Response(ctx context.Context, direction jsonrpc2.Direction, r *jsonrpc2.WireResponse) context.Context {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Done(ctx context.Context, err error) {
|
|
|
|
stats := h.getStats(ctx)
|
2019-07-16 14:24:47 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx = telemetry.StatusCode.With(ctx, "ERROR")
|
|
|
|
} else {
|
|
|
|
ctx = telemetry.StatusCode.With(ctx, "OK")
|
|
|
|
}
|
|
|
|
elapsedTime := time.Since(stats.start)
|
|
|
|
latencyMillis := float64(elapsedTime) / float64(time.Millisecond)
|
|
|
|
telemetry.Latency.Record(ctx, latencyMillis)
|
|
|
|
stats.close()
|
2019-07-11 22:43:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Read(ctx context.Context, bytes int64) context.Context {
|
2019-07-16 14:24:47 -06:00
|
|
|
telemetry.SentBytes.Record(ctx, bytes)
|
2019-07-11 22:43:12 -06:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Wrote(ctx context.Context, bytes int64) context.Context {
|
2019-07-16 14:24:47 -06:00
|
|
|
telemetry.ReceivedBytes.Record(ctx, bytes)
|
2019-07-11 22:43:12 -06:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
const eol = "\r\n\r\n\r\n"
|
|
|
|
|
|
|
|
func (h *handler) Error(ctx context.Context, err error) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) getStats(ctx context.Context) *rpcStats {
|
|
|
|
stats, ok := ctx.Value(statsKey).(*rpcStats)
|
|
|
|
if !ok || stats == nil {
|
2019-07-16 17:56:49 -06:00
|
|
|
method, ok := ctx.Value(telemetry.Method).(string)
|
|
|
|
if !ok {
|
|
|
|
method = "???"
|
|
|
|
}
|
2019-07-11 22:43:12 -06:00
|
|
|
stats = &rpcStats{
|
2019-07-16 17:56:49 -06:00
|
|
|
method: method,
|
2019-07-16 14:24:47 -06:00
|
|
|
close: func() {},
|
2019-07-11 22:43:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|