forked from golang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/test: fix path to local go in integration tests
As described in golang/go#69630, our 1.21 and 1.22 builders were not actually testing gopls' integration with the Go command, because go test modifies PATH and GOROOT when performing a toolchain switch. "Fix" this by searching PATH for a local (=non-toolchain) go command, and then mutating PATH and unsetting GOROOT to use this go command. This is very much a hack, as noted in the relevant commentary, but allows us to have much needed test coverage on the builders. In golang/go#69321, we hope to design a better solution to this problem. Many tests are updated to make their go version requirements accurate. Fixes golang/go#69630 Change-Id: I431107b97845e1e99799c2c22f33b04f85ce6dd9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/623175 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
- Loading branch information
Showing
53 changed files
with
166 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,10 @@ import ( | |
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -189,5 +192,56 @@ func Main(m *testing.M) (code int) { | |
} | ||
runner.tempDir = dir | ||
|
||
FilterToolchainPathAndGOROOT() | ||
|
||
return m.Run() | ||
} | ||
|
||
// FilterToolchainPathAndGOROOT updates the PATH and GOROOT environment | ||
// variables for the current process to effectively revert the changes made by | ||
// the go command when performing a toolchain switch in the context of `go | ||
// test` (see golang/go#68005). | ||
// | ||
// It does this by looking through PATH for a go command that is NOT a | ||
// toolchain go command, and adjusting PATH to find that go command. Then it | ||
// unsets GOROOT in order to use the default GOROOT for that go command. | ||
// | ||
// TODO(rfindley): this is very much a hack, so that our 1.21 and 1.22 builders | ||
// actually exercise integration with older go commands. In golang/go#69321, we | ||
// hope to do better. | ||
func FilterToolchainPathAndGOROOT() { | ||
if localGo, first := findLocalGo(); localGo != "" && !first { | ||
dir := filepath.Dir(localGo) | ||
path := os.Getenv("PATH") | ||
os.Setenv("PATH", dir+string(os.PathListSeparator)+path) | ||
os.Unsetenv("GOROOT") // Remove the GOROOT value that was added by toolchain switch. | ||
} | ||
} | ||
|
||
// findLocalGo returns a path to a local (=non-toolchain) Go version, or the | ||
// empty string if none is found. | ||
// | ||
// The second result reports if path matches the result of exec.LookPath. | ||
func findLocalGo() (path string, first bool) { | ||
paths := filepath.SplitList(os.Getenv("PATH")) | ||
for _, path := range paths { | ||
// Use a simple heuristic to filter out toolchain paths. | ||
if strings.Contains(path, "[email protected]") && filepath.Base(path) == "bin" { | ||
continue // toolchain path | ||
} | ||
fullPath := filepath.Join(path, "go") | ||
fi, err := os.Stat(fullPath) | ||
if err != nil { | ||
continue | ||
} | ||
if fi.Mode()&0111 != 0 { | ||
first := false | ||
pathGo, err := exec.LookPath("go") | ||
if err == nil { | ||
first = fullPath == pathGo | ||
} | ||
return fullPath, first | ||
} | ||
} | ||
return "", false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.