Skip to content

Commit

Permalink
perf: use nio.process.run instead of vim.system
Browse files Browse the repository at this point in the history
This should avoid having Neovim freeze during long-running 'go list'
command.

Co-authored-by: Bruno Mazzo <[email protected]>
  • Loading branch information
fredrikaverpil and BrunoMazzo committed Jan 4, 2025
1 parent 25c63a0 commit e554e96
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions lua/neotest-golang/lib/cmd.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Helper functions building the command to execute.

---@type nio
local async = require("neotest.async")
local nio = require("nio") -- require("neotest.async")

local logger = require("neotest-golang.logging")
local options = require("neotest-golang.options")
Expand All @@ -15,34 +15,52 @@ function M.golist_data(cwd)
local cmd = M.golist_command()
local go_list_command_concat = table.concat(cmd, " ")
logger.info("Running Go list: " .. go_list_command_concat .. " in " .. cwd)
local result = vim.system(cmd, { cwd = cwd, text = true }):wait()

local err = nil
if result.code == 1 then
err = "go list:"
if result.stdout ~= nil and result.stdout ~= "" then
err = err .. " " .. result.stdout
local task = nio.run(function()
local result = vim.system(cmd, { cwd = cwd, text = true }):wait()

-- sleep for 3 seconds
nio.sleep(5000)

local err = nil
if result.code == 1 then
err = "go list:"
if result.stdout ~= nil and result.stdout ~= "" then
err = err .. " " .. result.stdout
end
if result.stdout ~= nil and result.stderr ~= "" then
err = err .. " " .. result.stderr
end
logger.warn({ "Go list error: ", err })
end
if result.stdout ~= nil and result.stderr ~= "" then
err = err .. " " .. result.stderr
end
logger.warn({ "Go list error: ", err })
end

local output = result.stdout or ""
local output = result.stdout or ""

local golist_output = json.decode_from_string(output)
logger.debug({ "JSON-decoded 'go list' output: ", golist_output })

return golist_output, err
end)

local golist_output, err = task.wait()

local golist_output = json.decode_from_string(output)
logger.debug({ "JSON-decoded 'go list' output: ", golist_output })
return golist_output, err
end

function M.golist_command()
-- NOTE: original command can contain a lot of data:
-- local cmd = { "go", "list", "-json" }

-- full path to the "go" executable
local go_executable = vim.fn.exepath("go")

vim.notify(vim.inspect(go_executable))

-- TODO: return error here if not found

-- NOTE: optimized command only outputs fields needed:
local cmd = {
"go",
go_executable,
"list",
"-f",
[[{
Expand Down Expand Up @@ -92,7 +110,7 @@ function M.test_command(go_test_required_args)
if runner == "go" then
cmd = M.go_test(go_test_required_args)
elseif runner == "gotestsum" then
json_filepath = vim.fs.normalize(async.fn.tempname())
json_filepath = vim.fs.normalize(nio.fn.tempname())
cmd = M.gotestsum(go_test_required_args, json_filepath)
end

Expand Down

0 comments on commit e554e96

Please sign in to comment.