From 153e30b3c7fa2996154a94565eaedf3fcdf9e514 Mon Sep 17 00:00:00 2001 From: Pontus Leitzler Date: Sun, 20 Mar 2022 10:46:05 +0100 Subject: [PATCH] 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 Run-TryBot: Pontus Leitzler gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Robert Findley --- internal/lsp/lsprpc/goenv.go | 7 +++++++ internal/lsp/lsprpc/lsprpc.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/internal/lsp/lsprpc/goenv.go b/internal/lsp/lsprpc/goenv.go index 4b16d8d9ea..f313724c87 100644 --- a/internal/lsp/lsprpc/goenv.go +++ b/internal/lsp/lsprpc/goenv.go @@ -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 diff --git a/internal/lsp/lsprpc/lsprpc.go b/internal/lsp/lsprpc/lsprpc.go index ca32f0e174..478d362e02 100644 --- a/internal/lsp/lsprpc/lsprpc.go +++ b/internal/lsp/lsprpc/lsprpc.go @@ -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