File tree Expand file tree Collapse file tree 1 file changed +34
-2
lines changed
Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Original file line number Diff line number Diff line change 292292--- @param path string path to file or directory
293293--- @return boolean
294294function M .file_exists (path )
295- local _ , error = vim .loop .fs_stat (path )
296- return error == nil
295+ if not (M .is_windows or M .is_wsl ) then
296+ local _ , error = vim .loop .fs_stat (path )
297+ return error == nil
298+ end
299+
300+ -- Windows is case-insensetive, but case-preserving
301+ -- If a file's name is being changed into itself
302+ -- with different casing, windows will falsely
303+ -- report that file is already existing, so a hand-rolled
304+ -- implementation of checking for existance is needed.
305+ -- Same holds for WSL, since it can sometimes
306+ -- access Windows files directly.
307+ -- For more details see (#3117).
308+
309+ local parent = vim .fn .fnamemodify (path , " :h" )
310+ local filename = vim .fn .fnamemodify (path , " :t" )
311+
312+ local handle = vim .loop .fs_scandir (parent )
313+ if not handle then
314+ -- File can not exist if its parent directory does not exist
315+ return false
316+ end
317+
318+ while true do
319+ local name , _ = vim .loop .fs_scandir_next (handle )
320+ if not name then
321+ break
322+ end
323+ if name == filename then
324+ return true
325+ end
326+ end
327+
328+ return false
297329end
298330
299331--- @param path string
You can’t perform that action at this time.
0 commit comments