From b087899bed533b0bc53b4ac21dd0c9b9a76a931e Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Fri, 12 Jan 2024 00:21:11 +0800 Subject: [PATCH 1/2] fix: avoid data race between formatter and Neovim By default, nvim-go auto-formats on save. Occasionally when saving, goimports returns a "no such file" error. This seems to be caused by a race between the format job, and Neovim writing out the file. By running the formatting job synchronously this race is avoided. --- lua/go/format.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/go/format.lua b/lua/go/format.lua index e6da67f..6d38bfe 100644 --- a/lua/go/format.lua +++ b/lua/go/format.lua @@ -48,7 +48,7 @@ local function do_fmt(formatter, args) local original_line = vim.fn.getline('.') local original_col_nr = vim.fn.col('.') local cmd = system.wrap_file_command(formatter, args, file_path) - vim.fn.jobstart(cmd, { + local job_id = vim.fn.jobstart(cmd, { on_exit = function(_, code, _) if code == 0 then output.show_success('GoFormat', 'Success') @@ -72,6 +72,8 @@ local function do_fmt(formatter, args) output.show_error('GoFormat', results) end, }) + -- wait for the job so we don't race nvim writing out the buffer + vim.fn.jobwait({job_id}) end function M.lsp() From 91c6470fff979a0b91390c332910fa628d0551e3 Mon Sep 17 00:00:00 2001 From: Manuel Coenen Date: Thu, 21 Mar 2024 12:07:40 +0100 Subject: [PATCH 2/2] feat: allow specifying cwd for test_all() --- lua/go/test.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/go/test.lua b/lua/go/test.lua index 9a113e1..6ee9cc1 100644 --- a/lua/go/test.lua +++ b/lua/go/test.lua @@ -52,7 +52,7 @@ local function valid_buf() return false end -local function do_test(prefix, cmd) +local function do_test(prefix, cmd, cwd) if not valid_buf() then return end @@ -92,7 +92,7 @@ local function do_test(prefix, cmd) end end - local cwd = vim.fn.expand('%:p:h') + cwd = cwd or vim.fn.expand('%:p:h') local env = config.options.test_env local opts = { on_exit = function(_, code, _) @@ -125,14 +125,14 @@ function M.test() do_test(prefix, build_args(cmd)) end -function M.test_all() +function M.test_all(cwd) if not util.binary_exists('go') then return end local prefix = 'GoTestAll' local cmd = { 'go', 'test', './...' } - do_test(prefix, build_args(cmd)) + do_test(prefix, build_args(cmd), cwd) end function M.test_func(opt)