diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index 37bcc9cb..c25537f4 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -41,12 +41,23 @@ return { vim.cmd([[Neotree close]]) end, init = function() - if vim.fn.argc(-1) == 1 then - local stat = vim.uv.fs_stat(vim.fn.argv(0)) - if stat and stat.type == "directory" then - require("neo-tree") - end - end + -- FIX: use `autocmd` for lazy-loading neo-tree instead of directly requiring it, + -- because `cwd` is not set up properly. + vim.api.nvim_create_autocmd("BufEnter", { + group = vim.api.nvim_create_augroup("Neotree_start_directory", { clear = true }), + desc = "Start Neo-tree with directory", + once = true, + callback = function() + if package.loaded["neo-tree"] then + return + else + local stats = vim.uv.fs_stat(vim.fn.argv(0)) + if stats and stats.type == "directory" then + require("neo-tree") + end + end + end, + }) end, opts = { sources = { "filesystem", "buffers", "git_status", "document_symbols" }, diff --git a/lua/lazyvim/util/root.lua b/lua/lazyvim/util/root.lua index 4143206c..c8de430d 100644 --- a/lua/lazyvim/util/root.lua +++ b/lua/lazyvim/util/root.lua @@ -142,7 +142,10 @@ function M.setup() LazyVim.root.info() end, { desc = "LazyVim roots for the current buffer" }) - vim.api.nvim_create_autocmd({ "LspAttach", "BufWritePost", "DirChanged" }, { + -- FIX: doesn't properly clear cache in neo-tree `set_root` (which should happen presumably on `DirChanged`), + -- probably because the event is triggered in the neo-tree buffer, therefore add `BufEnter` + -- Maybe this is too frequent on `BufEnter` and something else should be done instead?? + vim.api.nvim_create_autocmd({ "LspAttach", "BufWritePost", "DirChanged", "BufEnter" }, { group = vim.api.nvim_create_augroup("lazyvim_root_cache", { clear = true }), callback = function(event) M.cache[event.buf] = nil