File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -591,17 +591,17 @@ function M.pcall_trace(fn, ...)
591591end
592592
593593function M .is_path_in_cwd (path )
594- local cwd = vim .fn .getcwd ()
595- -- For relative paths, build the logical absolute path without resolving symlinks
596- -- so that files inside symlinked directories within cwd are accepted.
597- if path : sub ( 1 , 1 ) ~= ' / ' then
598- local logical = vim . fn . simplify ( cwd .. ' /' .. path )
599- if logical : sub ( 1 , # cwd ) == cwd then
600- return true
601- end
594+ local cwd = vim .fn .simplify ( vim . fn . getcwd () )
595+ local cwd_prefix = cwd == ' / ' and cwd or ( cwd .. ' / ' )
596+
597+ local logical_path
598+ if path : sub ( 1 , 1 ) == ' /' then
599+ logical_path = vim . fn . simplify ( path )
600+ else
601+ logical_path = vim . fn . simplify ( cwd .. ' / ' .. path )
602602 end
603- local abs_path = vim . fn . fnamemodify ( path , ' :p ' )
604- return abs_path :sub (1 , # cwd ) == cwd
603+
604+ return logical_path == cwd or logical_path :sub (1 , # cwd_prefix ) == cwd_prefix
605605end
606606
607607--- Check if a given path is in the system temporary directory.
Original file line number Diff line number Diff line change @@ -402,10 +402,18 @@ describe('util.is_path_in_cwd', function()
402402 assert .is_true (util .is_path_in_cwd (test_cwd .. ' /src/foo.lua' ))
403403 end )
404404
405+ it (' accepts an absolute path through a symlinked directory in cwd' , function ()
406+ assert .is_true (util .is_path_in_cwd (test_cwd .. ' /linked_folder/test.txt' ))
407+ end )
408+
405409 it (' rejects an absolute path outside cwd' , function ()
406410 assert .is_false (util .is_path_in_cwd (' /tmp/outside/foo.lua' ))
407411 end )
408412
413+ it (' rejects a path that only shares the cwd prefix' , function ()
414+ assert .is_false (util .is_path_in_cwd (' /tmp/test_project2/src/foo.lua' ))
415+ end )
416+
409417 it (' rejects a relative path that escapes cwd via ..' , function ()
410418 assert .is_false (util .is_path_in_cwd (' ../outside/foo.lua' ))
411419 end )
You can’t perform that action at this time.
0 commit comments