Skip to content

Commit ff0c88a

Browse files
committed
fix(undotree): use tmpname for tmp undo file (#2464)
1 parent 0284d64 commit ff0c88a

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

lua/fzf-lua/fzf.lua

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,6 @@ local libuv = require "fzf-lua.libuv"
1111

1212
local M = {}
1313

14-
-- workaround to a potential 'tempname' bug? (#222)
15-
-- neovim doesn't guarantee the existence of the
16-
-- parent temp dir potentially failing `mkfifo`
17-
-- https://github.com/neovim/neovim/issues/1432
18-
-- https://github.com/neovim/neovim/pull/11284
19-
local function tempname()
20-
local tmpname = vim.fn.tempname()
21-
local parent = vim.fn.fnamemodify(tmpname, ":h")
22-
-- parent must exist for `mkfifo` to succeed
23-
-- if the neovim temp dir was deleted or the
24-
-- tempname already exists, we use 'os.tmpname'
25-
if not uv.fs_stat(parent) or uv.fs_stat(tmpname) then
26-
tmpname = os.tmpname()
27-
-- 'os.tmpname' touches the file which
28-
-- will also fail `mkfifo`, delete it
29-
vim.fn.delete(tmpname)
30-
end
31-
return tmpname
32-
end
33-
3414
-- contents can be either a table with tostring()able items, or a function that
3515
-- can be called repeatedly for values. The latter can use coroutines for async
3616
-- behavior.
@@ -48,7 +28,7 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
4828

4929
if not opts then opts = {} end
5030
local cmd = { opts.fzf_bin or "fzf" }
51-
local outputtmpname = tempname()
31+
local outputtmpname = utils.tempname()
5232

5333
-- we use a temporary env $FZF_DEFAULT_COMMAND instead of piping
5434
-- the command to fzf, this way fzf kills the command when it exits.

lua/fzf-lua/providers/undotree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ local function load_undo_buf(buf)
203203
end
204204
info.buf = buf
205205
info.changedtick = changedtick
206-
info.tmp_file = path.join({ vim.fn.stdpath("cache") --[[@as string]], "fzf-lua-undo" })
206+
info.tmp_file = path.join({ path.parent(utils.tempname()), "fzf-lua-undo" })
207207
info.tmp_undo = info.tmp_file .. ".undo"
208208
info.tmp_buf = vim.fn.bufadd(info.tmp_file)
209209
vim.bo[info.tmp_buf].swapfile = false

lua/fzf-lua/utils.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,27 @@ function M.termopen(cmd, opts)
15611561
end
15621562
end
15631563

1564+
--- workaround to a potential 'tempname' bug? (#222)
1565+
--- neovim doesn't guarantee the existence of the
1566+
--- parent temp dir potentially failing `mkfifo`
1567+
--- https://github.com/neovim/neovim/issues/1432
1568+
--- https://github.com/neovim/neovim/pull/11284
1569+
--- @return string
1570+
function M.tempname()
1571+
local tmpname = vim.fn.tempname()
1572+
local parent = vim.fn.fnamemodify(tmpname, ":h")
1573+
-- parent must exist for `mkfifo` to succeed
1574+
-- if the neovim temp dir was deleted or the
1575+
-- tempname already exists, we use 'os.tmpname'
1576+
if not uv.fs_stat(parent) or uv.fs_stat(tmpname) then
1577+
tmpname = os.tmpname()
1578+
-- 'os.tmpname' touches the file which
1579+
-- will also fail `mkfifo`, delete it
1580+
vim.fn.delete(tmpname)
1581+
end
1582+
return tmpname
1583+
end
1584+
15641585
---@param cmd string
15651586
---@param flag string
15661587
---@param enabled boolean?

0 commit comments

Comments
 (0)