internal/lsp: only construct a cache when we need to

we only construct a cache as we build a server, rather than for each instance
of Application now.

Change-Id: Ic18966906f8f61b18b71fc6d6f7ccc8e9cebbd29
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207904
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go
index 8bc2128..2f179f8 100644
--- a/internal/lsp/cmd/cmd.go
+++ b/internal/lsp/cmd/cmd.go
@@ -45,8 +45,8 @@
 	// TODO: Remove this when we stop allowing the serve verb by default.
 	Serve Serve
 
-	// The base cache to use for sessions from this application.
-	cache source.Cache
+	// the options configuring function to invoke when building a server
+	options func(*source.Options)
 
 	// The name of the binary, used in help and telemetry.
 	name string
@@ -77,7 +77,7 @@
 		wd, _ = os.Getwd()
 	}
 	app := &Application{
-		cache:   cache.New(options),
+		options: options,
 		name:    name,
 		wd:      wd,
 		env:     env,
@@ -165,7 +165,7 @@
 	switch app.Remote {
 	case "":
 		connection := newConnection(app)
-		ctx, connection.Server = lsp.NewClientServer(ctx, app.cache, connection.Client)
+		ctx, connection.Server = lsp.NewClientServer(ctx, cache.New(app.options), connection.Client)
 		return connection, connection.initialize(ctx)
 	case "internal":
 		internalMu.Lock()
@@ -181,7 +181,7 @@
 		ctx, jc, connection.Server = protocol.NewClient(ctx, jsonrpc2.NewHeaderStream(cr, cw), connection.Client)
 		go jc.Run(ctx)
 		go func() {
-			ctx, srv := lsp.NewServer(ctx, app.cache, jsonrpc2.NewHeaderStream(sr, sw))
+			ctx, srv := lsp.NewServer(ctx, cache.New(app.options), jsonrpc2.NewHeaderStream(sr, sw))
 			srv.Run(ctx)
 		}()
 		if err := connection.initialize(ctx); err != nil {
diff --git a/internal/lsp/cmd/serve.go b/internal/lsp/cmd/serve.go
index 79b4546..2fdec95 100644
--- a/internal/lsp/cmd/serve.go
+++ b/internal/lsp/cmd/serve.go
@@ -18,6 +18,7 @@
 
 	"golang.org/x/tools/internal/jsonrpc2"
 	"golang.org/x/tools/internal/lsp"
+	"golang.org/x/tools/internal/lsp/cache"
 	"golang.org/x/tools/internal/lsp/debug"
 	"golang.org/x/tools/internal/lsp/protocol"
 	"golang.org/x/tools/internal/lsp/telemetry"
@@ -87,16 +88,16 @@
 	}
 	run := func(ctx context.Context, srv *lsp.Server) { go prepare(ctx, srv).Run(ctx) }
 	if s.Address != "" {
-		return lsp.RunServerOnAddress(ctx, s.app.cache, s.Address, run)
+		return lsp.RunServerOnAddress(ctx, cache.New(s.app.options), s.Address, run)
 	}
 	if s.Port != 0 {
-		return lsp.RunServerOnPort(ctx, s.app.cache, s.Port, run)
+		return lsp.RunServerOnPort(ctx, cache.New(s.app.options), s.Port, run)
 	}
 	stream := jsonrpc2.NewHeaderStream(os.Stdin, os.Stdout)
 	if s.Trace {
 		stream = protocol.LoggingStream(stream, out)
 	}
-	ctx, srv := lsp.NewServer(ctx, s.app.cache, stream)
+	ctx, srv := lsp.NewServer(ctx, cache.New(s.app.options), stream)
 	return prepare(ctx, srv).Run(ctx)
 }