Skip to content

Commit bdb252d

Browse files
author
Zhe Yu
committed
fix(jobrunner): Correctly propagate errors from cmd jobrunner
1 parent 4232d9c commit bdb252d

1 file changed

Lines changed: 28 additions & 46 deletions

File tree

lua/vectorcode/jobrunner/cmd.lua

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---@type VectorCode.JobRunner
22
local runner = {}
33

4-
local Job = require("plenary.job")
5-
---@type {integer: Job}
4+
---@type table<integer, vim.SystemObj>
65
local jobs = {}
76
local logger = require("vectorcode.config").logger
87

@@ -15,44 +14,30 @@ function runner.run_async(args, callback, bufnr)
1514
logger.debug(
1615
("cmd jobrunner for buffer %s args: %s"):format(bufnr, vim.inspect(args))
1716
)
18-
---@diagnostic disable-next-line: missing-fields
19-
local job = Job:new({
20-
command = require("vectorcode.config").get_user_config().cli_cmds.vectorcode,
21-
args = args,
22-
on_exit = function(self, code, signal)
23-
jobs[self.pid] = nil
24-
local result = self:result()
25-
logger.debug(result)
26-
local ok, decoded = pcall(vim.json.decode, table.concat(result, ""))
27-
if callback ~= nil then
28-
if ok then
29-
callback(decoded or {}, self:stderr_result(), code, signal)
30-
if vim.islist(result) then
31-
logger.debug(
32-
"cmd jobrunner result:\n",
33-
vim.tbl_map(function(item)
34-
if type(item) == "table" then
35-
item.document = nil
36-
item.chunk = nil
37-
end
38-
return item
39-
end, vim.deepcopy(result))
40-
)
41-
end
42-
else
43-
callback({ result }, self:stderr_result(), code, signal)
44-
logger.warn("cmd runner: failed to decode result:\n", result)
45-
end
46-
end
47-
end,
48-
})
49-
local ok = pcall(job.start, job)
50-
if ok then
51-
jobs[job.pid] = job
52-
return tonumber(job.pid)
53-
else
54-
logger.error("Failed to start job.")
55-
end
17+
18+
table.insert(
19+
args,
20+
1,
21+
require("vectorcode.config").get_user_config().cli_cmds.vectorcode
22+
)
23+
24+
---@type vim.SystemObj?
25+
local job
26+
job = vim.system(args, {}, function(out)
27+
if job and job.pid then
28+
jobs[job.pid] = job
29+
end
30+
local stdout = out.stdout or "{}"
31+
if stdout == "" then
32+
stdout = "{}"
33+
end
34+
local _, decoded = pcall(vim.json.decode, stdout, { object = true, array = true })
35+
if type(callback) == "function" then
36+
callback(decoded or {}, out.stderr, out.code, out.signal)
37+
end
38+
end)
39+
jobs[job.pid] = job
40+
return tonumber(job.pid)
5641
end
5742

5843
function runner.run(args, timeout_ms, bufnr)
@@ -66,11 +51,8 @@ function runner.run(args, timeout_ms, bufnr)
6651
code = e_code
6752
signal = s
6853
end, bufnr)
69-
if pid ~= nil then
70-
vim.wait(timeout_ms, function()
71-
return res ~= nil or err ~= nil
72-
end)
73-
jobs[pid] = nil
54+
if pid ~= nil and jobs[pid] ~= nil then
55+
jobs[pid]:wait(timeout_ms)
7456
end
7557
return res or {}, err, code, signal
7658
end
@@ -82,7 +64,7 @@ end
8264
function runner.stop_job(job_handle)
8365
local job = jobs[job_handle]
8466
if job ~= nil then
85-
job:shutdown(1, 15)
67+
job:kill(15)
8668
end
8769
end
8870

0 commit comments

Comments
 (0)