11--- Go environment utilities for detecting GOPATH/GOROOT paths.
22--- Used to prevent discovering tests in Go's stdlib or installed packages.
33
4+ local logger = require (" neotest-golang.lib.logging" )
45local path = require (" neotest-golang.lib.path" )
56
67local M = {}
@@ -10,12 +11,25 @@ local M = {}
1011local go_env_cache = nil
1112
1213--- Populate the go env cache (async version).
14+ --- A failing 'go env' is cached as empty paths, which match nothing, so the
15+ --- command is executed at most once per session.
1316--- @async
1417--- @return { gopath : string , goroot : string }
1518local function get_go_env_async ()
1619 if go_env_cache == nil then
1720 local async = require (" neotest.async" )
1821 local result = async .fn .system ({ " go" , " env" , " GOPATH" , " GOROOT" })
22+ if vim .v .shell_error ~= 0 then
23+ -- A failing 'go env' does not throw, it reports its error on stderr.
24+ logger .error (
25+ " Command 'go env GOPATH GOROOT' exited with code "
26+ .. vim .v .shell_error
27+ .. " : "
28+ .. vim .trim (result or " " )
29+ )
30+ go_env_cache = { gopath = " " , goroot = " " }
31+ return go_env_cache
32+ end
1933 local lines = vim .split (vim .trim (result or " " ), " \n " )
2034 go_env_cache = {
2135 gopath = path .normalize_path (lines [1 ] or " " ),
7993--- Check if a path should be skipped because it's in GOPATH/GOROOT but cwd is not.
8094--- This prevents the adapter from discovering tests in Go's stdlib or installed packages
8195--- when the user is working in a different project.
96+ --- An environment which could not be determined holds no paths and therefore
97+ --- skips nothing: hiding tests which can be executed is the worse failure mode.
8298--- @async
8399--- @param file_path string Path to check
84100--- @param cwd string | nil Current working directory
@@ -87,10 +103,7 @@ function M.should_skip(file_path, cwd)
87103 if not cwd or not file_path then
88104 return false
89105 end
90- local env_ok , env = pcall (get_go_env_async )
91- if not env_ok then
92- return true
93- end
106+ local env = get_go_env_async ()
94107 local norm_path = path .normalize_path (file_path )
95108 local norm_cwd = path .normalize_path (cwd )
96109 return not is_in_go_env (norm_cwd , env ) and is_in_go_env (norm_path , env )
0 commit comments