From 86784f7bb6a6eee8634be6ce23bfbe4f635eb190 Mon Sep 17 00:00:00 2001 From: Tom Dyas Date: Sun, 25 Sep 2022 00:49:23 -0400 Subject: [PATCH] go: prepend $GOROOT/bin to PATH for tests (#16993) Prepend $GOROOT/bin to PATH when running tests just as `go test` does. Fixes https://github.com/pantsbuild/pants/issues/16990. [ci skip-build-wheels] # Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] --- src/python/pants/backend/go/goals/test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/python/pants/backend/go/goals/test.py b/src/python/pants/backend/go/goals/test.py index 0fd98cabd87..83a6d817e42 100644 --- a/src/python/pants/backend/go/goals/test.py +++ b/src/python/pants/backend/go/goals/test.py @@ -33,6 +33,7 @@ FirstPartyPkgAnalysisRequest, FirstPartyPkgDigestRequest, ) +from pants.backend.go.util_rules.goroot import GoRoot from pants.backend.go.util_rules.import_analysis import ImportConfig, ImportConfigRequest from pants.backend.go.util_rules.link import LinkedGoBinary, LinkGoBinaryRequest from pants.backend.go.util_rules.tests_analysis import GeneratedTestMain, GenerateTestMainRequest @@ -164,6 +165,7 @@ async def run_go_tests( test_subsystem: TestSubsystem, go_test_subsystem: GoTestSubsystem, test_extra_env: TestExtraEnv, + goroot: GoRoot, ) -> TestResult: maybe_pkg_analysis, maybe_pkg_digest, dependencies = await MultiGet( Get(FallibleFirstPartyPkgAnalysis, FirstPartyPkgAnalysisRequest(field_set.address)), @@ -370,6 +372,14 @@ def compilation_failure(exit_code: int, stdout: str | None, stderr: str | None) **field_set_extra_env, } + # Add $GOROOT/bin to the PATH just as `go test` does. + # See https://github.com/golang/go/blob/master/src/cmd/go/internal/test/test.go#L1384 + goroot_bin_path = os.path.join(goroot.path, "bin") + if "PATH" in extra_env: + extra_env["PATH"] = f"{goroot_bin_path}:{extra_env['PATH']}" + else: + extra_env["PATH"] = goroot_bin_path + cache_scope = ( ProcessCacheScope.PER_SESSION if test_subsystem.force else ProcessCacheScope.SUCCESSFUL )