mirror of https://github.com/golang/go.git
internal/lsp: use a structured format for the server's version
The server reports its version to the client--add some structure to this report so that the client can parse and use it. Fixes golang/go#42171 Change-Id: I00bff3615391cbeede89e4be6b0d028fed67989e Reviewed-on: https://go-review.googlesource.com/c/tools/+/266198 Trust: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
parent
8694a4a169
commit
3db8fd2658
|
|
@ -1,16 +0,0 @@
|
|||
// 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.
|
||||
|
||||
// +build !go1.12
|
||||
|
||||
package debug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func printBuildInfo(w io.Writer, verbose bool, mode PrintMode) {
|
||||
fmt.Fprintf(w, "version %s, built in $GOPATH mode\n", Version)
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
// 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.
|
||||
|
||||
// +build go1.12
|
||||
|
||||
package debug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func printBuildInfo(w io.Writer, verbose bool, mode PrintMode) {
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
fmt.Fprintf(w, "%v %v\n", info.Path, Version)
|
||||
printModuleInfo(w, &info.Main, mode)
|
||||
if verbose {
|
||||
for _, dep := range info.Deps {
|
||||
printModuleInfo(w, dep, mode)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "version %s, built in $GOPATH mode\n", Version)
|
||||
}
|
||||
}
|
||||
|
||||
func printModuleInfo(w io.Writer, m *debug.Module, mode PrintMode) {
|
||||
fmt.Fprintf(w, " %s@%s", m.Path, m.Version)
|
||||
if m.Sum != "" {
|
||||
fmt.Fprintf(w, " %s", m.Sum)
|
||||
}
|
||||
if m.Replace != nil {
|
||||
fmt.Fprintf(w, " => %v", m.Replace.Path)
|
||||
}
|
||||
fmt.Fprintf(w, "\n")
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -23,6 +24,71 @@ const (
|
|||
// Version is a manually-updated mechanism for tracking versions.
|
||||
var Version = "master"
|
||||
|
||||
// ServerVersion is the format used by gopls to report its version to the
|
||||
// client. This format is structured so that the client can parse it easily.
|
||||
type ServerVersion struct {
|
||||
Module
|
||||
Deps []*Module `json:"deps,omitempty"`
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
ModuleVersion
|
||||
Replace *ModuleVersion `json:"replace,omitempty"`
|
||||
}
|
||||
|
||||
type ModuleVersion struct {
|
||||
Path string `json:"path,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Sum string `json:"sum,omitempty"`
|
||||
}
|
||||
|
||||
// VersionInfo returns the build info for the gopls process. If it was not
|
||||
// built in module mode, we return a GOPATH-specific message with the
|
||||
// hardcoded version.
|
||||
func VersionInfo() *ServerVersion {
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
return getVersion(info)
|
||||
}
|
||||
path := "gopls, built in GOPATH mode"
|
||||
return &ServerVersion{
|
||||
Module: Module{
|
||||
ModuleVersion: ModuleVersion{
|
||||
Path: path,
|
||||
Version: Version,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func getVersion(info *debug.BuildInfo) *ServerVersion {
|
||||
serverVersion := ServerVersion{
|
||||
Module: Module{
|
||||
ModuleVersion: ModuleVersion{
|
||||
Path: info.Main.Path,
|
||||
Version: info.Main.Version,
|
||||
Sum: info.Main.Sum,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, d := range info.Deps {
|
||||
m := &Module{
|
||||
ModuleVersion: ModuleVersion{
|
||||
Path: d.Path,
|
||||
Version: d.Version,
|
||||
Sum: d.Sum,
|
||||
},
|
||||
}
|
||||
if d.Replace != nil {
|
||||
m.Replace = &ModuleVersion{
|
||||
Path: d.Replace.Path,
|
||||
Version: d.Replace.Version,
|
||||
}
|
||||
}
|
||||
serverVersion.Deps = append(serverVersion.Deps, m)
|
||||
}
|
||||
return &serverVersion
|
||||
}
|
||||
|
||||
// PrintServerInfo writes HTML debug info to w for the Instance.
|
||||
func (i *Instance) PrintServerInfo(ctx context.Context, w io.Writer) {
|
||||
section(w, HTML, "Server Instance", func() {
|
||||
|
|
@ -39,12 +105,13 @@ func (i *Instance) PrintServerInfo(ctx context.Context, w io.Writer) {
|
|||
// specified by mode. verbose controls whether additional information is
|
||||
// written, including section headers.
|
||||
func PrintVersionInfo(ctx context.Context, w io.Writer, verbose bool, mode PrintMode) {
|
||||
info := VersionInfo()
|
||||
if !verbose {
|
||||
printBuildInfo(w, false, mode)
|
||||
printBuildInfo(w, info, false, mode)
|
||||
return
|
||||
}
|
||||
section(w, mode, "Build info", func() {
|
||||
printBuildInfo(w, true, mode)
|
||||
printBuildInfo(w, info, true, mode)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -64,3 +131,25 @@ func section(w io.Writer, mode PrintMode, title string, body func()) {
|
|||
fmt.Fprint(w, "</pre>\n")
|
||||
}
|
||||
}
|
||||
|
||||
func printBuildInfo(w io.Writer, info *ServerVersion, verbose bool, mode PrintMode) {
|
||||
fmt.Fprintf(w, "%v %v\n", info.Path, Version)
|
||||
printModuleInfo(w, &info.Module, mode)
|
||||
if !verbose {
|
||||
return
|
||||
}
|
||||
for _, dep := range info.Deps {
|
||||
printModuleInfo(w, dep, mode)
|
||||
}
|
||||
}
|
||||
|
||||
func printModuleInfo(w io.Writer, m *Module, mode PrintMode) {
|
||||
fmt.Fprintf(w, " %s@%s", m.Path, m.Version)
|
||||
if m.Sum != "" {
|
||||
fmt.Fprintf(w, " %s", m.Sum)
|
||||
}
|
||||
if m.Replace != nil {
|
||||
fmt.Fprintf(w, " => %v", m.Replace.Path)
|
||||
}
|
||||
fmt.Fprintf(w, "\n")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ package lsp
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
|
@ -83,8 +84,10 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
|
|||
}
|
||||
}
|
||||
|
||||
goplsVer := &bytes.Buffer{}
|
||||
debug.PrintVersionInfo(ctx, goplsVer, true, debug.PlainText)
|
||||
goplsVersion, err := json.Marshal(debug.VersionInfo())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &protocol.InitializeResult{
|
||||
Capabilities: protocol.ServerCapabilities{
|
||||
|
|
@ -130,10 +133,9 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
|
|||
Version string `json:"version,omitempty"`
|
||||
}{
|
||||
Name: "gopls",
|
||||
Version: goplsVer.String(),
|
||||
Version: string(goplsVersion),
|
||||
},
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *Server) initialized(ctx context.Context, params *protocol.InitializedParams) error {
|
||||
|
|
|
|||
Loading…
Reference in New Issue