From 4040e33683041c942f92a395fe5147ac704d4588 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Mon, 31 Jan 2022 17:40:50 -0800 Subject: [PATCH] multiprocess: delete neovim temp dir on os.exit (closes #329) --- lua/fzf-lua/libuv.lua | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lua/fzf-lua/libuv.lua b/lua/fzf-lua/libuv.lua index 3ce3e6f..d05a88c 100644 --- a/lua/fzf-lua/libuv.lua +++ b/lua/fzf-lua/libuv.lua @@ -306,6 +306,10 @@ M.spawn_stdio = function(opts, fn_transform, fn_preprocess) local stderr, stdout = nil, nil + -- due to 'os.exit' neovim doesn't delete the temporary + -- directory, save it so we can delete prior to exit (#329) + local tmpdir = vim.fn.fnamemodify(vim.fn.tempname(), ':h') + local function stderr_write(msg) -- prioritize writing errors to stderr if stderr then stderr:write(msg) @@ -314,7 +318,17 @@ M.spawn_stdio = function(opts, fn_transform, fn_preprocess) local function exit(exit_code, msg) if msg then stderr_write(msg) end - os.exit(exit_code) + -- we can't call delete directly, it will fail with error: + -- E5560: vimL function must not be called in a lua loop callback + -- so we defer both 'delete' and 'os.exit' so delete happens first + if tmpdir then + vim.defer_fn(function() + vim.fn.delete(tmpdir, "rf") + end, 0) + end + vim.defer_fn(function() + os.exit(exit_code) + end, 0) end local function pipe_open(pipename)