Skip to content

Commit d589d7a

Browse files
fix(goenv): handle an undeterminable go env explicitly (#595)
1 parent cd02ab9 commit d589d7a

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎lua/neotest-golang/lib/goenv.lua‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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")
45
local path = require("neotest-golang.lib.path")
56

67
local M = {}
@@ -10,12 +11,25 @@ local M = {}
1011
local 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}
1518
local 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 ""),
@@ -79,6 +93,8 @@ end
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)

‎spec/unit/goenv_spec.lua‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ describe("Go environment utilities", function()
130130
assert.is_false(result)
131131
end)
132132

133+
it("returns false when the environment could not be determined", function()
134+
goenv.set_cache_for_testing({ gopath = "", goroot = "" })
135+
local result = goenv.should_skip(
136+
"/home/user/go/pkg/mod/github.com/foo/bar",
137+
"/home/user/myproject"
138+
)
139+
assert.is_false(result)
140+
end)
141+
133142
it("does not match paths with similar prefix (the bug fix)", function()
134143
-- GOPATH is /home/user/go, but /home/user/golang should NOT match
135144
local result = goenv.should_skip(

0 commit comments

Comments
 (0)