From dcbe650bd4811cefe5a885fafb6309c7d592bda6 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 6 Nov 2023 20:57:00 -0800 Subject: fix: catch jobstart errors (#183) --- lua/conform/errors.lua | 11 +++++++---- lua/conform/runner.lua | 10 +++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'lua') diff --git a/lua/conform/errors.lua b/lua/conform/errors.lua index 3a56b80..43e9a7b 100644 --- a/lua/conform/errors.lua +++ b/lua/conform/errors.lua @@ -11,14 +11,16 @@ M.ERROR_CODE = { INVALID_ARGS = 1, -- Command was not executable NOT_EXECUTABLE = 2, + -- Error occurred during when calling jobstart + JOBSTART = 3, -- Command timed out during execution - TIMEOUT = 3, + TIMEOUT = 4, -- Command was pre-empted by another call to format - INTERRUPTED = 4, + INTERRUPTED = 5, -- Command produced an error during execution - RUNTIME = 5, + RUNTIME = 6, -- Asynchronous formatter results were discarded due to a concurrent modification - CONCURRENT_MODIFICATION = 6, + CONCURRENT_MODIFICATION = 7, } ---@param code conform.ERROR_CODE @@ -40,6 +42,7 @@ M.is_execution_error = function(code) return code == M.ERROR_CODE.RUNTIME or code == M.ERROR_CODE.NOT_EXECUTABLE or code == M.ERROR_CODE.INVALID_ARGS + or code == M.ERROR_CODE.JOBSTART end ---@param err1? conform.Error diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua index 4146163..4b166a9 100644 --- a/lua/conform/runner.lua +++ b/lua/conform/runner.lua @@ -299,7 +299,7 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, opts, c local stderr local exit_codes = config.exit_codes or { 0 } local jid - jid = vim.fn.jobstart(cmd, { + local ok, jid_or_err = pcall(vim.fn.jobstart, cmd, { cwd = cwd, env = env, stdout_buffered = true, @@ -364,6 +364,14 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, opts, c end end, }) + if not ok then + callback({ + code = errors.ERROR_CODE.JOBSTART, + message = string.format("Formatter '%s' error in jobstart: %s", formatter.name, jid_or_err), + }) + return + end + jid = jid_or_err if jid == 0 then callback({ code = errors.ERROR_CODE.INVALID_ARGS, -- cgit v1.2.3-70-g09d2