Skip to content

Commit

Permalink
Merge pull request #86 from TheLeoP/fix_fs_find
Browse files Browse the repository at this point in the history
fix(fs): avoid infinite loop searching parent dir
  • Loading branch information
gorillamoe authored Jul 30, 2024
2 parents aa1cbc0 + 945fab4 commit 8995170
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions lua/kulala/utils/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@ local M = {}
--- @return string|nil
--- @usage local p = fs.find_file_in_parent_dirs('Makefile')
M.find_file_in_parent_dirs = function(filename)
local dir = vim.fn.expand('%:p:h')
-- make sure we don't go into an infinite loop
-- if the file is in the root directory of windows or unix
-- we should stop at the root directory
-- for linux, the root directory is '/'
-- for windows, the root directory is '[SOME_LETTER]:\'
while dir ~= '/' and dir:match('[A-Z]:\\') == nil do
local parent = dir .. '/' .. filename
if vim.fn.filereadable(parent) == 1 then
return parent
end
dir = vim.fn.fnamemodify(dir, ':h')
end
return nil
return vim.fs.find({filename}, {upward=true, limit=1})[1]
end

-- Writes string to file
Expand Down

0 comments on commit 8995170

Please sign in to comment.