Skip to content

Commit

Permalink
internal/lsp/lsprpc: only propagate explicit GOWORK when using remote
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
leitzler authored and findleyr committed Apr 1, 2022
1 parent cda13e2 commit 153e30b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/lsp/lsprpc/goenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"

"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/gocommand"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/lsp/lsprpc/lsprpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 153e30b

Please sign in to comment.