Skip to content

Commit 60fe75c

Browse files
committed
fix(task): run on_exit async. See #1569
1 parent 4615524 commit 60fe75c

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lua/lazy/async.lua

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function Async:sleep(ms)
3737
vim.defer_fn(function()
3838
self.sleeping = false
3939
end, ms)
40+
coroutine.yield()
4041
end
4142

4243
function Async:suspend()

lua/lazy/manage/process.lua

+15-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,21 @@ function M.exec(cmd, opts)
197197
lines = _lines
198198
end,
199199
})
200-
vim.fn.jobwait({ job })
200+
201+
if job <= 0 then
202+
error("Failed to start job: " .. vim.inspect(cmd))
203+
end
204+
205+
local Async = require("lazy.async")
206+
local async = Async.current
207+
if async then
208+
while vim.fn.jobwait({ job }, 0)[1] == -1 do
209+
async:sleep(10)
210+
end
211+
else
212+
vim.fn.jobwait({ job })
213+
end
214+
201215
return lines
202216
end
203217

lua/lazy/manage/task/init.lua

+7-6
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,13 @@ function Task:spawn(cmd, opts)
217217
self._running:suspend()
218218

219219
local running = true
220-
local ret = true
220+
local ret = { ok = true, output = "" }
221221
---@param output string
222222
function opts.on_exit(ok, output)
223223
if not headless then
224224
self:log(vim.trim(output), ok and vim.log.levels.DEBUG or vim.log.levels.ERROR)
225225
end
226-
if on_exit then
227-
pcall(on_exit, ok, output)
228-
end
229-
ret = ok
226+
ret = { ok = ok, output = output }
230227
running = false
231228
self._running:resume()
232229
end
@@ -241,7 +238,11 @@ function Task:spawn(cmd, opts)
241238
Process.spawn(cmd, opts)
242239
coroutine.yield()
243240
assert(not running, "process still running?")
244-
return ret
241+
if on_exit then
242+
pcall(on_exit, ret.ok, ret.output)
243+
end
244+
coroutine.yield()
245+
return ret.ok
245246
end
246247

247248
function Task:prefix()

0 commit comments

Comments
 (0)