Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(watcher): race condition between positions update and watch run on BufWritePost #479

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lua/neotest/consumers/watch/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ local neotest = {}
---@class neotest.consumers.watch
neotest.watch = {}

local init = function(client)
client.listeners.discover_positions = function(_, tree)
for _, watcher in pairs(watchers) do
if watcher.tree:data().path == tree:data().path
and not watcher.discover_positions_event.is_set() then
watcher.discover_positions_event.set()
end
end
end
end

local function get_valid_client(bufnr)
local clients = nio.lsp.get_clients({ bufnr = bufnr })
for _, client in ipairs(clients) do
Expand Down Expand Up @@ -200,7 +211,8 @@ function neotest.watch.is_watching(position_id)
end

neotest.watch = setmetatable(neotest.watch, {
__call = function()
__call = function(_, client)
init(client)
return neotest.watch
end,
})
Expand Down
8 changes: 8 additions & 0 deletions lua/neotest/consumers/watch/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local config = require("neotest.config")
---@class neotest.consumers.watch.Watcher
---@field lsp_client nio.lsp.Client
---@field autocmd_id? string
---@field tree neotest.Tree
---@field discover_positions_event nio.control.Future
local Watcher = {}

function Watcher:new(lsp_client)
Expand Down Expand Up @@ -159,6 +161,9 @@ function Watcher:watch(tree, args)
logger.debug("Built dependencies in", elapsed, "ms for", tree:data().id, ":", dependencies)
local dependants = self:_build_dependants(dependencies)

self.tree = tree
self.discover_positions_event = nio.control.future()

self.autocmd_id = nio.api.nvim_create_autocmd("BufWritePost", {
callback = function(autocmd_args)
if type(args.run_predicate) == "function" and not args.run_predicate(autocmd_args.buf) then
Expand All @@ -172,6 +177,9 @@ function Watcher:watch(tree, args)
return
end

self.discover_positions_event.wait()
self.discover_positions_event = nio.control.future()

if tree:data().type ~= "dir" then
run.run(vim.tbl_extend("keep", { tree:data().id }, args))
else
Expand Down