MiniMisc.setup_auto_root
w/ snacks projects cd failure workaround
#2024
-
Contributing guidelines
Module(s)mini.misc QuestionI'm not particularly sure if this is a bug in either project, but I thought I'd share a workaround. I recently started using This can be reproduced in the following scenario:
Expected: Switch to repository Actual: Still shows me files from repository Here is a repro script: for name, url in pairs({
mini = "https://github.com/nvim-mini/mini.nvim",
wk = "https://github.com/folke/which-key.nvim",
snacks = "https://github.com/folke/snacks.nvim",
}) do
local tmpdir = vim.loop.os_tmpdir()
local install_path = vim.fn.fnamemodify(tmpdir .. "/misc_root_repro/" .. name, ":p")
if vim.fn.isdirectory(install_path) == 0 then
vim.fn.system({ "git", "clone", "--depth=1", url, install_path })
end
vim.opt.runtimepath:append(install_path)
end
vim.g.leader = " "
vim.o.timeoutlen = 1000
local wk = require("which-key")
wk.setup()
require("mini.misc").setup_auto_root()
require("snacks").setup({
picker = { enabled = true, sources = {}, layout = {
preset = "telescope",
} },
})
vim.api.nvim_create_user_command("ProjectsFailsToCd", function(opts)
local path = vim.fn.expand(opts.args)
require("snacks.picker").projects({ dev = { path }, matcher = { frecency = false } })
end, { nargs = 1, complete = "dir" })
vim.api.nvim_create_user_command("ProjectsWorkaround", function(opts)
local path = vim.fn.expand(opts.args)
require("snacks.picker").projects({
dev = { path },
matcher = { frecency = false },
-- can override the confirm to do something else
-- instead of load_session here (the default), just cd and open a file picker
-- https://github.com/folke/snacks.nvim/blob/68da653d206069007f71d4373049193248bf913b/lua/snacks/picker/actions.lua#L543
confirm = function(picker, chosen)
-- change default from load_session, just cd and open a picker
picker:close()
vim.fn.chdir(chosen.file)
Snacks.picker.files()
end,
})
end, { nargs = 1, complete = "dir" }) If you download that to
Then run I included This may also just be a weird interaction with edit: to state the obvious, if you disable |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think the culprit lies in 'snacks.picker'. Thanks to your helpful link to the actual action that is used here, this call is the problem here. It defers the Since a lot can happen during those 100ms (like user/plugin changing working directory to something else), I think it is more reasonable to explicitly start diff --git a/lua/snacks/picker/actions.lua b/lua/snacks/picker/actions.lua
index 6e6050e..fa20238 100644
--- a/lua/snacks/picker/actions.lua
+++ b/lua/snacks/picker/actions.lua
@@ -555,7 +555,7 @@ function M.load_session(picker, item)
})
vim.defer_fn(function()
if not session_loaded then
- Snacks.picker.files()
+ Snacks.picker.files({ dirs = { dir } })
end
end, 100)
vim.fn.chdir(dir) For future, I'd highly recommend to limit reproduction setup to as minimal code as possible. Leave only the code that you can not remove while still reproducing the issue. In particular, there is no reason to install/setup 'folke/which-key.nvim', set particular options, or create a dedicated user command (the |
Beta Was this translation helpful? Give feedback.
I think the culprit lies in 'snacks.picker'. Thanks to your helpful link to the actual action that is used here, this call is the problem here. It defers the
files
picker by 100 ms and eventually calls it without arguments. This implicitly assumes that it will start in target directory due to thischdir
, which does not hold in this particular case.Since a lot can happen during those 100ms (like user/plugin changing working directory to something else), I think it is more reasonable to explicitly start
files
picker with target directory. Basically applying the following patch to 'snacks.n…