fix(watchfiles): skip Created events when poll starts (#23139)

This commit is contained in:
Jon Huhn
2023-04-17 11:50:05 -05:00
committed by GitHub
parent 9e5f9c25d9
commit 6cc76011ca
2 changed files with 17 additions and 17 deletions

View File

@ -88,6 +88,9 @@ local default_poll_interval_ms = 2000
--- be invoked recursively) --- be invoked recursively)
--- - children (table|nil) --- - children (table|nil)
--- A mapping of directory entry name to its recursive watches --- A mapping of directory entry name to its recursive watches
-- - started (boolean|nil)
-- Whether or not the watcher has first been initialized. Used
-- to prevent a flood of Created events on startup.
local function poll_internal(path, opts, callback, watches) local function poll_internal(path, opts, callback, watches)
path = vim.fs.normalize(path) path = vim.fs.normalize(path)
local interval = opts and opts.interval or default_poll_interval_ms local interval = opts and opts.interval or default_poll_interval_ms
@ -112,7 +115,9 @@ local function poll_internal(path, opts, callback, watches)
end) end)
) )
assert(not start_err, start_err) assert(not start_err, start_err)
callback(path, M.FileChangeType.Created) if watches.started then
callback(path, M.FileChangeType.Created)
end
end end
watches.cancel = function() watches.cancel = function()
@ -132,6 +137,7 @@ local function poll_internal(path, opts, callback, watches)
if not watches.children[name] then if not watches.children[name] then
watches.children[name] = { watches.children[name] = {
is_dir = ftype == 'directory', is_dir = ftype == 'directory',
started = watches.started,
} }
poll_internal(filepath_join(path, name), opts, callback, watches.children[name]) poll_internal(filepath_join(path, name), opts, callback, watches.children[name])
end end
@ -150,6 +156,8 @@ local function poll_internal(path, opts, callback, watches)
watches.children = newchildren watches.children = newchildren
end end
watches.started = true
return watches.cancel return watches.cancel
end end

View File

@ -122,10 +122,6 @@ describe('vim._watch', function()
table.insert(events, { path = path, change_type = change_type }) table.insert(events, { path = path, change_type = change_type })
end) end)
-- polling generates Created events for the existing entries when it starts.
expected_events = expected_events + 1
wait_for_events()
vim.wait(100) vim.wait(100)
local watched_path = root_dir .. '/file' local watched_path = root_dir .. '/file'
@ -158,39 +154,35 @@ describe('vim._watch', function()
root_dir root_dir
) )
eq(5, #result) eq(4, #result)
eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Created]]),
path = root_dir,
}, result[1])
eq({ eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Created]]), change_type = exec_lua([[return vim._watch.FileChangeType.Created]]),
path = root_dir .. '/file', path = root_dir .. '/file',
}, result[2]) }, result[1])
eq({ eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Changed]]), change_type = exec_lua([[return vim._watch.FileChangeType.Changed]]),
path = root_dir, path = root_dir,
}, result[3]) }, result[2])
-- The file delete and corresponding directory change events do not happen in any -- The file delete and corresponding directory change events do not happen in any
-- particular order, so allow either -- particular order, so allow either
if result[4].path == root_dir then if result[3].path == root_dir then
eq({ eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Changed]]), change_type = exec_lua([[return vim._watch.FileChangeType.Changed]]),
path = root_dir, path = root_dir,
}, result[4]) }, result[3])
eq({ eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Deleted]]), change_type = exec_lua([[return vim._watch.FileChangeType.Deleted]]),
path = root_dir .. '/file', path = root_dir .. '/file',
}, result[5]) }, result[4])
else else
eq({ eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Deleted]]), change_type = exec_lua([[return vim._watch.FileChangeType.Deleted]]),
path = root_dir .. '/file', path = root_dir .. '/file',
}, result[4]) }, result[3])
eq({ eq({
change_type = exec_lua([[return vim._watch.FileChangeType.Changed]]), change_type = exec_lua([[return vim._watch.FileChangeType.Changed]]),
path = root_dir, path = root_dir,
}, result[5]) }, result[4])
end end
end) end)
end) end)