Skip to content

Commit 16c9f00

Browse files
committed
feat(util): support symlinked directories in is_path_in_cwd
1 parent 434e348 commit 16c9f00

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

lua/opencode/util.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,17 @@ function M.pcall_trace(fn, ...)
591591
end
592592

593593
function 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
605605
end
606606

607607
--- Check if a given path is in the system temporary directory.

tests/unit/util_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)