From dc152afab0c09a348533427e55fe19500d207267 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sun, 14 Jul 2024 01:20:32 +0200 Subject: [PATCH] fix: find_upwards could go into infinite loop --- lua/neotest-golang/lib/find.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lua/neotest-golang/lib/find.lua b/lua/neotest-golang/lib/find.lua index f759a570..3caa48e4 100644 --- a/lua/neotest-golang/lib/find.lua +++ b/lua/neotest-golang/lib/find.lua @@ -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.