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()