Skip to content

Commit

Permalink
fix: find_upwards could go into infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 13, 2024
1 parent 51ffe50 commit dc152af
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lua/neotest-golang/lib/find.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ local M = {}
--- @param start_path string
--- @return string | nil
function M.file_upwards(filename, start_path)
local found_filepath = nil
while start_path ~= vim.fn.expand("$HOME") do
local files = scandir.scan_dir(start_path, {
-- Ensure start_path is a directory
local start_dir = vim.fn.isdirectory(start_path) == 1 and start_path
or vim.fn.fnamemodify(start_path, ":h")
local home_dir = vim.fn.expand("$HOME")

while start_dir ~= home_dir do
local files = scandir.scan_dir(start_dir, {
search_pattern = convert.to_lua_pattern(filename),
depth = 1,
add_dirs = false,
})
if #files > 0 then
found_filepath = files[1]
return found_filepath
return files[1]
end
end

if found_filepath == nil then
-- go up one directory and try again
start_path = vim.fn.fnamemodify(start_path, ":h")
return M.file_upwards(filename, start_path)
-- Go up one directory
start_dir = vim.fn.fnamemodify(start_dir, ":h")
end

return nil
end

-- Get all *_test.go files in a directory recursively.
Expand Down

0 comments on commit dc152af

Please sign in to comment.