https://pantsbuild.org/ logo
#general
Title
# general
b

bumpy-spoon-55438

09/24/2022, 3:34 AM
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

fast-nail-55400

09/24/2022, 3:54 AM
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

bumpy-spoon-55438

09/24/2022, 4:06 AM
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

fast-nail-55400

09/24/2022, 4:07 AM
Pants has options to set environment variables within the sandbox.
b

bumpy-spoon-55438

09/24/2022, 4:10 AM
I also tried this but not working
Copy code
[test]
extra_env_vars = [
    "PATH=$GOROOT/bin:$PATH"
]
f

fast-nail-55400

09/24/2022, 4:11 AM
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

bumpy-spoon-55438

09/24/2022, 4:16 AM
i see. i tried that and also print the path and it's actually empty
f

fast-nail-55400

09/24/2022, 4:16 AM
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

bumpy-spoon-55438

09/24/2022, 4:19 AM
like?
Copy code
[test]
extra_env_vars = [
    "GOROOT=/opt/homebrew/opt/go/libexec"
]
f

fast-nail-55400

09/24/2022, 4:25 AM
👍
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

bumpy-spoon-55438

09/24/2022, 4:34 AM
Got it. Thanks. I will have a try for the workaround
f

fast-nail-55400

09/25/2022, 5:52 PM
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
5 Views