From f11f8546e7fed3bbd77ce30364580433c1bd4196 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Sun, 16 Nov 2025 21:41:26 -0800 Subject: [PATCH] fix(vim.fs): root() should always return absolute path #36466 --- runtime/lua/vim/fs.lua | 3 ++- test/functional/lua/fs_spec.lua | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 6773bb00ff..22ed685566 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -450,7 +450,8 @@ function M.root(source, marker) }) if #paths ~= 0 then - return vim.fs.dirname(paths[1]) + local dir = vim.fs.dirname(paths[1]) + return dir and vim.fn.fnamemodify(dir, ':p:h') or nil end end diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index e735f60a69..eaea5c7540 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -432,6 +432,14 @@ describe('vim.fs', function() command('file lua://') eq(test_source_path, exec_lua([[return vim.fs.root(0, 'CMakePresets.json')]])) end) + + it('returns an absolute path for an invalid filename', function() + assert(n.fn.isabsolutepath(test_source_path) == 1) + eq( + t.fix_slashes(test_source_path), + t.fix_slashes(exec_lua([[return vim.fs.root('file://asd', 'CMakePresets.json')]])) + ) + end) end) describe('joinpath()', function()