fix(watch): invalid joined path #37973

Problem:
When vim._watch.watch() is used to watch a single file, libuv returns
the basename as the filename argument in the callback. The code joins
this with the watched path, producing a nonsensical path like
"/path/to/file.lua/file.lua", which causes ENOTDIR errors on
subsequent fs_stat calls.

Solution:
Check whether the watched path is a directory before joining the
filename. When watching a file, ignore the filename from libuv and
use the watched path directly.
This commit is contained in:
Oleksandr Chekhovskyi authored and GitHub committed 2026-02-23 17:33:13 -05:00
1 parent b57ed5e940
commit d9d8c660fd
1 file changed
+3 -1
+3 -1
View File
@@ -69,10 +69,12 @@ function M.watch(path, opts, callback)
local uvflags = opts and opts.uvflags or {}
local handle = assert(uv.new_fs_event())
local watching_dir = (uv.fs_stat(path) or {}).type == 'directory'
local _, start_err, start_errname = handle:start(path, uvflags, function(err, filename, events)
assert(not err, err)
local fullpath = path
if filename then
if filename and watching_dir then
fullpath = vim.fs.normalize(vim.fs.joinpath(fullpath, filename))
end