Skip to content

Commit f1523d2

Browse files
committed
[gopls-release-branch.0.5] internal/lsp: print the go version only once the view is created
Printing the Go version without the session's go command runner means that we may not find the right Go version. Also, panicking when we cannot find a go command is not useful to the user--show the error as a view initialization error instead. Fixes golang/go#41701 Change-Id: I0e0753da9795b1c78331db1faecd27c2bfcee9b4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/258312 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]> (cherry picked from commit 66e72d0) Reviewed-on: https://go-review.googlesource.com/c/tools/+/258517
1 parent b4c11cb commit f1523d2

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

internal/lsp/cache/view.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,26 @@ func (v *View) WriteEnv(ctx context.Context, w io.Writer) error {
359359
}
360360

361361
}
362-
fmt.Fprintf(w, "go env for %v\n(root %s)\n(valid build configuration = %v)\n(build flags: %v)\n",
363-
v.folder.Filename(), v.rootURI.Filename(), v.hasValidBuildConfiguration, buildFlags)
362+
goVersion, err := v.session.gocmdRunner.Run(ctx, gocommand.Invocation{
363+
Verb: "version",
364+
BuildFlags: buildFlags,
365+
Env: env,
366+
WorkingDir: v.rootURI.Filename(),
367+
})
368+
if err != nil {
369+
return err
370+
}
371+
fmt.Fprintf(w, `go env for %v
372+
(root %s)
373+
(go version %s)
374+
(valid build configuration = %v)
375+
(build flags: %v)
376+
`,
377+
v.folder.Filename(),
378+
v.rootURI.Filename(),
379+
goVersion.String(),
380+
v.hasValidBuildConfiguration,
381+
buildFlags)
364382
for k, v := range fullEnv {
365383
fmt.Fprintf(w, "%s=%s\n", k, v)
366384
}

internal/lsp/debug/info.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"fmt"
1111
"io"
1212
"strings"
13-
14-
"golang.org/x/tools/internal/gocommand"
1513
)
1614

1715
type PrintMode int
@@ -48,17 +46,6 @@ func PrintVersionInfo(ctx context.Context, w io.Writer, verbose bool, mode Print
4846
section(w, mode, "Build info", func() {
4947
printBuildInfo(w, true, mode)
5048
})
51-
fmt.Fprint(w, "\n")
52-
section(w, mode, "Go info", func() {
53-
gocmdRunner := &gocommand.Runner{}
54-
version, err := gocmdRunner.Run(ctx, gocommand.Invocation{
55-
Verb: "version",
56-
})
57-
if err != nil {
58-
panic(err)
59-
}
60-
fmt.Fprintln(w, version.String())
61-
})
6249
}
6350

6451
func section(w io.Writer, mode PrintMode, title string, body func()) {

internal/lsp/general.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"golang.org/x/tools/internal/event"
1919
"golang.org/x/tools/internal/jsonrpc2"
2020
"golang.org/x/tools/internal/lsp/debug"
21-
"golang.org/x/tools/internal/lsp/debug/tag"
2221
"golang.org/x/tools/internal/lsp/protocol"
2322
"golang.org/x/tools/internal/lsp/source"
2423
"golang.org/x/tools/internal/span"
@@ -147,12 +146,6 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
147146
options := s.session.Options()
148147
defer func() { s.session.SetOptions(options) }()
149148

150-
// TODO: this event logging may be unnecessary.
151-
// The version info is included in the initialize response.
152-
buf := &bytes.Buffer{}
153-
debug.PrintVersionInfo(ctx, buf, true, debug.PlainText)
154-
event.Log(ctx, buf.String())
155-
156149
if err := s.addFolders(ctx, s.pendingFolders); err != nil {
157150
return err
158151
}
@@ -199,7 +192,7 @@ func (s *Server) addFolders(ctx context.Context, folders []protocol.WorkspaceFol
199192
continue
200193
}
201194
work := s.progress.start(ctx, "Setting up workspace", "Loading packages...", nil, nil)
202-
view, snapshot, release, err := s.addView(ctx, folder.Name, uri)
195+
snapshot, release, err := s.addView(ctx, folder.Name, uri)
203196
if err != nil {
204197
viewErrors[uri] = err
205198
work.end(fmt.Sprintf("Error loading packages: %s", err))
@@ -219,8 +212,8 @@ func (s *Server) addFolders(ctx context.Context, folders []protocol.WorkspaceFol
219212

220213
// Print each view's environment.
221214
buf := &bytes.Buffer{}
222-
if err := view.WriteEnv(ctx, buf); err != nil {
223-
event.Error(ctx, "failed to write environment", err, tag.Directory.Of(view.Folder().Filename()))
215+
if err := snapshot.View().WriteEnv(ctx, buf); err != nil {
216+
viewErrors[uri] = err
224217
continue
225218
}
226219
event.Log(ctx, buf.String())

internal/lsp/workspace.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ func (s *Server) didChangeWorkspaceFolders(ctx context.Context, params *protocol
2626
return s.addFolders(ctx, event.Added)
2727
}
2828

29-
func (s *Server) addView(ctx context.Context, name string, uri span.URI) (source.View, source.Snapshot, func(), error) {
29+
func (s *Server) addView(ctx context.Context, name string, uri span.URI) (source.Snapshot, func(), error) {
3030
s.stateMu.Lock()
3131
state := s.state
3232
s.stateMu.Unlock()
3333
if state < serverInitialized {
34-
return nil, nil, func() {}, errors.Errorf("addView called before server initialized")
34+
return nil, func() {}, errors.Errorf("addView called before server initialized")
3535
}
3636
options := s.session.Options().Clone()
3737
if err := s.fetchConfig(ctx, name, uri, options); err != nil {
38-
return nil, nil, func() {}, err
38+
return nil, func() {}, err
3939
}
40-
return s.session.NewView(ctx, name, uri, options)
40+
_, snapshot, release, err := s.session.NewView(ctx, name, uri, options)
41+
return snapshot, release, err
4142
}
4243

4344
func (s *Server) didChangeConfiguration(ctx context.Context, changed interface{}) error {

0 commit comments

Comments
 (0)