| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cmd |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"log" |
| 14 |
"os" |
| 15 |
"strings" |
| 16 |
"time" |
| 17 |
|
| 18 |
"golang.org/x/sync/errgroup" |
| 19 |
"golang.org/x/tools/gopls/internal/cache" |
| 20 |
"golang.org/x/tools/gopls/internal/debug" |
| 21 |
"golang.org/x/tools/gopls/internal/lsprpc" |
| 22 |
"golang.org/x/tools/gopls/internal/mcp" |
| 23 |
"golang.org/x/tools/gopls/internal/protocol" |
| 24 |
"golang.org/x/tools/gopls/internal/tool" |
| 25 |
"golang.org/x/tools/gopls/internal/util/fakenet" |
| 26 |
"golang.org/x/tools/internal/jsonrpc2" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
type Serve struct { |
| 32 |
Logfile string `flag:"logfile" help:"filename to log to. if value is \"auto\", then logging to a default output file is enabled"` |
| 33 |
Mode string `flag:"mode" help:"no effect"` |
| 34 |
Address string `flag:"listen" help:"address on which to listen for remote connections. If prefixed by 'unix;', the subsequent address is assumed to be a unix domain socket. Otherwise, TCP is used."` |
| 35 |
IdleTimeout time.Duration `flag:"listen.timeout" help:"when used with -listen, shut down the server when there are no connected clients for this duration"` |
| 36 |
Trace bool `flag:"rpc.trace" help:"print the full rpc trace in lsp inspector format"` |
| 37 |
Debug string `flag:"debug" help:"serve debug information on the supplied address"` |
| 38 |
|
| 39 |
RemoteListenTimeout time.Duration `flag:"remote.listen.timeout" help:"when used with -remote=auto, the -listen.timeout value used to start the daemon"` |
| 40 |
RemoteDebug string `flag:"remote.debug" help:"when used with -remote=auto, the -debug value used to start the daemon"` |
| 41 |
RemoteLogfile string `flag:"remote.logfile" help:"when used with -remote=auto, the -logfile value used to start the daemon"` |
| 42 |
|
| 43 |
|
| 44 |
MCPAddress string `flag:"mcp.listen" help:"experimental: address on which to listen for model context protocol connections. If port is localhost:0, pick a random port in localhost instead."` |
| 45 |
|
| 46 |
app *Application |
| 47 |
} |
| 48 |
|
| 49 |
func (s *Serve) Name() string { return "serve" } |
| 50 |
func (s *Serve) Parent() string { return s.app.Name() } |
| 51 |
func (s *Serve) Usage() string { return "[server-flags]" } |
| 52 |
func (s *Serve) ShortHelp() string { |
| 53 |
return "run a server for Go code using the Language Server Protocol" |
| 54 |
} |
| 55 |
func (s *Serve) DetailedHelp(f *flag.FlagSet) { |
| 56 |
fmt.Fprint(f.Output(), ` gopls [flags] [server-flags] |
| 57 |
|
| 58 |
The server communicates using JSONRPC2 on stdin and stdout, and is intended to be run directly as |
| 59 |
a child of an editor process. |
| 60 |
|
| 61 |
server-flags: |
| 62 |
`) |
| 63 |
printFlagDefaults(f) |
| 64 |
} |
| 65 |
|
| 66 |
func (s *Serve) remoteArgs(network, address string) []string { |
| 67 |
args := []string{"serve", |
| 68 |
"-listen", fmt.Sprintf(`%s;%s`, network, address), |
| 69 |
} |
| 70 |
if s.RemoteDebug != "" { |
| 71 |
args = append(args, "-debug", s.RemoteDebug) |
| 72 |
} |
| 73 |
if s.RemoteListenTimeout != 0 { |
| 74 |
args = append(args, "-listen.timeout", s.RemoteListenTimeout.String()) |
| 75 |
} |
| 76 |
if s.RemoteLogfile != "" { |
| 77 |
args = append(args, "-logfile", s.RemoteLogfile) |
| 78 |
} |
| 79 |
return args |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func (s *Serve) Run(ctx context.Context, args ...string) error { |
| 85 |
if len(args) > 0 { |
| 86 |
return tool.CommandLineErrorf("server does not take arguments, got %v", args) |
| 87 |
} |
| 88 |
|
| 89 |
di := debug.GetInstance(ctx) |
| 90 |
isDaemon := s.Address != "" |
| 91 |
if di != nil { |
| 92 |
closeLog, err := di.SetLogFile(s.Logfile, isDaemon) |
| 93 |
if err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
defer closeLog() |
| 97 |
di.ServerAddress = s.Address |
| 98 |
di.Serve(ctx, s.Debug) |
| 99 |
} |
| 100 |
|
| 101 |
var ( |
| 102 |
ss jsonrpc2.StreamServer |
| 103 |
sessions mcp.Sessions |
| 104 |
) |
| 105 |
if s.app.Remote != "" { |
| 106 |
var err error |
| 107 |
ss, err = lsprpc.NewForwarder(s.app.Remote, s.remoteArgs) |
| 108 |
if err != nil { |
| 109 |
return fmt.Errorf("creating forwarder: %w", err) |
| 110 |
} |
| 111 |
} else { |
| 112 |
lsprpcServer := lsprpc.NewStreamServer(cache.New(nil), isDaemon, s.app.options) |
| 113 |
ss = lsprpcServer |
| 114 |
if s.MCPAddress != "" { |
| 115 |
sessions = lsprpcServer |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
group, ctx := errgroup.WithContext(ctx) |
| 120 |
|
| 121 |
|
| 122 |
success := errors.New("success") |
| 123 |
|
| 124 |
|
| 125 |
if sessions != nil { |
| 126 |
countAttachedMCP.Inc() |
| 127 |
group.Go(func() (err error) { |
| 128 |
defer func() { |
| 129 |
if err == nil { |
| 130 |
err = success |
| 131 |
} |
| 132 |
}() |
| 133 |
|
| 134 |
return mcp.Serve(ctx, s.MCPAddress, sessions, isDaemon, nil) |
| 135 |
}) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
group.Go(func() (err error) { |
| 140 |
defer func() { |
| 141 |
if err == nil { |
| 142 |
err = success |
| 143 |
} |
| 144 |
}() |
| 145 |
|
| 146 |
if s.Address != "" { |
| 147 |
|
| 148 |
network, addr := lsprpc.ParseAddr(s.Address) |
| 149 |
if strings.HasPrefix(addr, ":") { |
| 150 |
return fmt.Errorf("-listen=%s implicitly binds all network interfaces; please use an explicit host such as 0.0.0.0 (all interfaces) or localhost (safer)", addr) |
| 151 |
} |
| 152 |
log.Printf("Gopls LSP daemon: listening on %s network, address %s...", network, addr) |
| 153 |
defer log.Printf("Gopls LSP daemon: exiting") |
| 154 |
return jsonrpc2.ListenAndServe(ctx, network, addr, ss, s.IdleTimeout) |
| 155 |
} else { |
| 156 |
|
| 157 |
stream := jsonrpc2.NewHeaderStream(fakenet.NewConn("stdio", os.Stdin, os.Stdout)) |
| 158 |
if s.Trace && di != nil { |
| 159 |
stream = protocol.LoggingStream(stream, di.LogWriter) |
| 160 |
} |
| 161 |
conn := jsonrpc2.NewConn(stream) |
| 162 |
if err := ss.ServeStream(ctx, conn); errors.Is(err, io.EOF) { |
| 163 |
return nil |
| 164 |
} else { |
| 165 |
return err |
| 166 |
} |
| 167 |
} |
| 168 |
}) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
if err := group.Wait(); err != nil && !errors.Is(err, success) { |
| 174 |
return err |
| 175 |
} |
| 176 |
return nil |
| 177 |
} |