internal/lsp/lsprpc: only propagate explicit GOWORK when using remote

When using gopls remote, existing go env vars are injected in the
initialize message. Setting GOWORK explicitly will impact cmd/go
invocations, for example "go list" during package load. The reason it
impacts is that gopls manually sets PWD to fix paths returned by cmd/go
if the working directory is inside a symlinked directory.

By only propagate GOWORK when it is explicitly set we will ensure that
the behavior is the same when running with or without remote.

See golang/go#51823.

Fixes golang/go#51825

Change-Id: I6280aa7d8208e5aee269f19356668c7713e9f0a4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/394054
Trust: Pontus Leitzler <leitzler@gmail.com>
Run-TryBot: Pontus Leitzler <leitzler@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Pontus Leitzler 2022-03-20 10:46:05 +01:00 committed by Robert Findley
parent cda13e227d
commit 153e30b3c7
2 changed files with 13 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/gocommand"
@ -54,7 +55,13 @@ func addGoEnvToInitializeRequestV2(ctx context.Context, req *jsonrpc2_v2.Request
if err != nil {
return err
}
// We don't want to propagate GOWORK unless explicitly set since that could mess with
// path inference during cmd/go invocations, see golang/go#51825.
_, goworkSet := os.LookupEnv("GOWORK")
for govar, value := range goenv {
if govar == "GOWORK" && !goworkSet {
continue
}
env[govar] = value
}
opts["env"] = env

View File

@ -347,7 +347,13 @@ func addGoEnvToInitializeRequest(ctx context.Context, r jsonrpc2.Request) (jsonr
if err != nil {
return nil, err
}
// We don't want to propagate GOWORK unless explicitly set since that could mess with
// path inference during cmd/go invocations, see golang/go#51825.
_, goworkSet := os.LookupEnv("GOWORK")
for govar, value := range goenv {
if govar == "GOWORK" && !goworkSet {
continue
}
env[govar] = value
}
opts["env"] = env