Skip to content

Commit

Permalink
fix(task): run on_exit async. See #1569
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 27, 2024
1 parent 4615524 commit 60fe75c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions lua/lazy/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Async:sleep(ms)
vim.defer_fn(function()
self.sleeping = false
end, ms)
coroutine.yield()
end

function Async:suspend()
Expand Down
16 changes: 15 additions & 1 deletion lua/lazy/manage/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,21 @@ function M.exec(cmd, opts)
lines = _lines
end,
})
vim.fn.jobwait({ job })

if job <= 0 then
error("Failed to start job: " .. vim.inspect(cmd))
end

local Async = require("lazy.async")
local async = Async.current
if async then
while vim.fn.jobwait({ job }, 0)[1] == -1 do
async:sleep(10)
end
else
vim.fn.jobwait({ job })
end

return lines
end

Expand Down
13 changes: 7 additions & 6 deletions lua/lazy/manage/task/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,13 @@ function Task:spawn(cmd, opts)
self._running:suspend()

local running = true
local ret = true
local ret = { ok = true, output = "" }
---@param output string
function opts.on_exit(ok, output)
if not headless then
self:log(vim.trim(output), ok and vim.log.levels.DEBUG or vim.log.levels.ERROR)
end
if on_exit then
pcall(on_exit, ok, output)
end
ret = ok
ret = { ok = ok, output = output }
running = false
self._running:resume()
end
Expand All @@ -241,7 +238,11 @@ function Task:spawn(cmd, opts)
Process.spawn(cmd, opts)
coroutine.yield()
assert(not running, "process still running?")
return ret
if on_exit then
pcall(on_exit, ret.ok, ret.output)
end
coroutine.yield()
return ret.ok
end

function Task:prefix()
Expand Down

0 comments on commit 60fe75c

Please sign in to comment.