fix(fs): expand drive-relative paths on Windows #37084

Problem:
On windows, if a drive-relative path doesn't contain a slash,
`path_to_absolute` can't split out the relative component, causing
expansion to fails. e.g., `c:` `c:.` `c:..` `c:foo.md`

Solution:
For these cases, we can pass letter and colon to `path_full_dir_name`.
Notably, `..` is included as well.
if that relative path exists, it can be expanded correctly.
This commit is contained in:
tao
2026-03-19 07:54:19 +08:00
committed by GitHub
parent f4f1149292
commit 19715e6e8a
2 changed files with 22 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ local getcwd = n.fn.getcwd
local command = n.command
local write_file = t.write_file
local is_os = t.is_os
local chdir = n.fn.chdir
local function eq_slashconvert(expected, got)
eq(t.fix_slashes(expected), t.fix_slashes(got))
@@ -16,12 +17,15 @@ end
describe('fnamemodify()', function()
setup(function()
write_file('Xtest-fnamemodify.txt', [[foobar]])
t.mkdir('foo')
write_file('foo/bar', [[bar]])
end)
before_each(clear)
teardown(function()
os.remove('Xtest-fnamemodify.txt')
n.rmdir('foo')
end)
it('handles the root path', function()
@@ -37,6 +41,19 @@ describe('fnamemodify()', function()
eq(root, fnamemodify([[\]], ':p'))
eq(root, fnamemodify([[/]], ':p:h'))
eq(root, fnamemodify([[/]], ':p'))
local letter_colon = root:sub(1, 2)
local old_dir = getcwd() .. '/'
local foo_dir = old_dir .. 'foo/'
eq(old_dir, fnamemodify(letter_colon, ':p'))
eq(old_dir, fnamemodify(letter_colon .. '.', ':p'))
eq(old_dir, fnamemodify(letter_colon .. './', ':p'))
eq(foo_dir, fnamemodify(letter_colon .. './foo', ':p'))
eq(foo_dir, fnamemodify(letter_colon .. 'foo', ':p'))
chdir('foo')
eq(old_dir, fnamemodify(letter_colon .. '..', ':p'))
eq(old_dir, fnamemodify(letter_colon .. '../', ':p'))
eq(foo_dir .. 'bar', fnamemodify(letter_colon .. 'bar', ':p'))
end
end)