feat(watch): add on_error callback

Problem: Watcher failures are reported inconsistently, and callers
cannot detect when a watcher is no longer usable.

Solution: Route startup and event failures and unexpected inotifywait
exits through an optional on_error callback. Log to nvim-watch.log by
default. Ignore disappearing child directories and exits caused by
cancellation. Let LSP log failures and notify once per message, using
INFO for missing roots and ERROR otherwise, without interrupting
registration.

AI-assisted
This commit is contained in:
Lewis Russell
2026-09-07 16:21:52 +01:00
committed by Lewis Russell
parent 318ea4de21
commit efdd73c096
5 changed files with 196 additions and 66 deletions

View File

@@ -2,6 +2,7 @@ local bit = require('bit')
local glob = vim.glob
local watch = vim._watch
local log = require('vim.lsp.log')
local notify = require('vim._core.util').notify
local protocol = require('vim.lsp.protocol')
local lpeg = vim.lpeg
@@ -185,6 +186,20 @@ function M.register(reg, client_id)
-- match a *particular* pattern+kind pair.
include_pattern = include_pattern,
exclude_pattern = M._poll_exclude_pattern,
on_error = function(err)
local name = string.format('LSP[%s]', client.name)
local message = string.format('file watcher failed for %s', base_dir)
local level = vim.log.levels.ERROR
-- Servers may register a nonexistent baseUri. Keep this informational
-- and continue registering the other watchers.
if err:match('^ENOENT:') then
level = vim.log.levels.INFO
log.info(name, message, err)
else
log.error(name, message, err)
end
notify(name, message .. ': ' .. err, level, true)
end,
}, callback(base_dir))
)
end