Hi, I am using pants to build and run some go test...
# general
b
Hi, I am using pants to build and run some go tests. the test would invoke certain go commands, but I encountered errors "err: go command required, not found: exec: "go": executable file not found in $PATH". does pants have go command on PATH?
f
We don't currently set a specific
PATH
when running tests. See https://github.com/pantsbuild/pants/blob/1bced7cf2f918345f032c31f9ff3393784e6ece0/src/python/pants/backend/go/goals/test.py#L368-L373 for where the env is setup for Go test runs.
Would it be useful to put
$GOROOT/bin
on the
PATH
?
Actually looking at the
go
toolchain source, it appears to always append the
$GOROOT/bin
to the
PATH
when invoking the test binary.
I can probably get this early next week.
It's probably just a 1-2 line change.
b
thanks.
do you see any workaround at this moment?
i tried prepend goroot/bin in PATH but seems not working
like
os.Setenv("PATH", strings.Join([]string{"$GOROOT/bin", os.Getenv("PATH")}, ":"))
f
Pants has options to set environment variables within the sandbox.
b
I also tried this but not working
Copy code
[test]
extra_env_vars = [
    "PATH=$GOROOT/bin:$PATH"
]
f
Pants does not expand other environment variables within an environment variable definition like that.
you can run your Go snippet in a
func init() {}
that should have it run before any tests
more like:
Copy code
func init() {
  goroot := os.Getenv("GOROOT")
  if goroot != "" {
    path := os.Getenv("PATH")
    if path != "" {
      os.Setenv("PATH", fmt.Sprintf("%s/bin:%s", goroot, path)
    } else {
      os.Setenv("PATH", fmt.Sprintf("%s/bin", goroot)
    }
  }
}
although probably better to use
strings.Join
and
filepath.Join
instead of
fmt.Sprintf
b
i see. i tried that and also print the path and it's actually empty
f
hmm
ah running the test does not use
GoSdkProcess
which sets the
GOROOT
automatically. I'll have to add that to the issue to set as well.
for now, you could set the
GOROOT
explicitly via
--test-extra-env
and then that would be available to the test run
and then the Go snippet could append the current
$GOROOT/bin
to the
PATH
b
like?
Copy code
[test]
extra_env_vars = [
    "GOROOT=/opt/homebrew/opt/go/libexec"
]
f
👍
any way, I'll fix this when I'm back at work on Monday and try to get it in for the 2.14 release.
ah running the test does not use
GoSdkProcess
which sets the
GOROOT
automatically. I'll have to add that to the issue to set as well.
note to self: did some research and
go test
does not set
GOROOT
just modifies the
PATH
b
Got it. Thanks. I will have a try for the workaround
f
This is now fixed on
main
and
2.14.x
branches.
so next release of 2.14.x rc series will have the fix.
❤️ 2
🙌 2