From e9b9426d7d1fa5aeb0451bf21b4a5515b4ef5901 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Sat, 13 Jun 2026 20:20:45 +0300 Subject: [PATCH] fix(filetype): `vim.filetype.match` fails if CWD goes missing #40190 Problem: Running `vim.filetype.match()` when current working directory was removed from disk throws a `vim.fs.abspath` assertion error. However, the matching might still be possible without trying to match against full path (like if it is a known extension). Solution: Safely compute absolute path and ignore it if it errors. --- runtime/lua/vim/filetype.lua | 7 ++++--- test/functional/lua/filetype_spec.lua | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 782bd495ad..133d2b0d30 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -3238,16 +3238,17 @@ function M.match(args) if name then name = normalize_path(name) - local path = vim.fs.abspath(name) - do -- First check for the simple case where the full path exists as a key + local ok_abspath, path = pcall(vim.fs.abspath, name) + if ok_abspath then -- First check for the simple case where the full path exists as a key local ft, on_detect = dispatch(filename[path], path, bufnr) if ft then return ft, on_detect end + else + path = name end local tail = vim.fs.basename(name) - do -- Next check against just the file name local ft, on_detect = dispatch(filename[tail], path, bufnr) if ft then diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua index ab124028a6..c174a8c065 100644 --- a/test/functional/lua/filetype_spec.lua +++ b/test/functional/lua/filetype_spec.lua @@ -216,6 +216,9 @@ describe('vim.filetype', function() end) ) rmdir('Xfiletype') + + -- Should still work even if current directory is not on disk + eq('zsh', exec_lua('return vim.filetype.match({ filename = ".zshrc" })')) end) it('fallback to conf if any of the first five lines start with a #', function()