Skip to content

Commit 6737334

Browse files
fix: Remove pytest_cache dependency for project root detection
split_at_root now recognises common project root markers instead of relying solely on .pytest_cache, which only exists after running pytest. Also fixes an infinite loop when no root marker was found (0 is truthy in Lua, so `while #t do` never terminates on an empty table). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 63444b8 commit 6737334

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

lua/pytrize/paths.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@ local M = {}
22

33
local warn = require('pytrize.warn').warn
44

5+
local root_markers = {
6+
'.pytest_cache',
7+
'pyproject.toml',
8+
'setup.py',
9+
'setup.cfg',
10+
'pytest.ini',
11+
'tox.ini',
12+
'.git',
13+
}
14+
515
local function is_root_dir(dir)
6-
return vim.fn.finddir('.pytest_cache', dir) ~= ''
16+
for _, marker in ipairs(root_markers) do
17+
if vim.fn.finddir(marker, dir) ~= '' or vim.fn.findfile(marker, dir) ~= '' then
18+
return true
19+
end
20+
end
21+
return false
722
end
823

924
local function join_path(fragments)
@@ -18,7 +33,7 @@ end
1833
M.split_at_root = function(file)
1934
local dir_fragments = vim.fn.split(file, '/', 1)
2035
local rel_file_fragments = {}
21-
while #dir_fragments do
36+
while #dir_fragments > 0 do
2237
table.insert(rel_file_fragments, 1, table.remove(dir_fragments, #dir_fragments))
2338
local dir = join_path(dir_fragments)
2439
if is_root_dir(dir) then

0 commit comments

Comments
 (0)