diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index d96875a8..522b2ac4 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -369,9 +369,19 @@ function M.enable() break end end - -- TODO: add an autocmd on BufWritePost that checks if its in a /lua folder - -- if thats the case, then reset the plugin path. - -- This will make sure we can properly load new top-level lua modules + + -- this will reset the top-mods in case someone adds a new + -- top-level lua module to a path already on the rtp + vim.api.nvim_create_autocmd("BufWritePost", { + group = vim.api.nvim_create_augroup("cache_topmods_reset", { clear = true }), + callback = function(event) + local bufname = event.match ---@type string + local idx = bufname:find("/lua/", 1, true) + if idx then + M.reset(bufname:sub(1, idx - 1)) + end + end, + }) end --- Disables the cache: @@ -391,6 +401,7 @@ function M.disable() end end table.insert(package.loaders, 2, vim._load_package) + vim.api.nvim_del_augroup_by_name("cache_topmods_reset") end --- Return the top-level `/lua/*` modules for this path