fix(compiler): check executable before spawning process
Problem: if a configured binary was missing or not in PATH, vim.system would fail silently or with a cryptic OS error. The user had no actionable feedback without running :checkhealth. Solution: check vim.fn.executable() at the start of M.compile() and notify with an ERROR-level message pointing to :checkhealth preview if the binary is not found.
This commit is contained in:
parent
1456b3070a
commit
781a9c4818
2 changed files with 37 additions and 0 deletions
|
|
@ -58,6 +58,14 @@ end
|
|||
function M.compile(bufnr, name, provider, ctx, opts)
|
||||
opts = opts or {}
|
||||
|
||||
if vim.fn.executable(provider.cmd[1]) ~= 1 then
|
||||
vim.notify(
|
||||
'[preview.nvim]: "' .. provider.cmd[1] .. '" is not executable (run :checkhealth preview)',
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
if vim.bo[bufnr].modified then
|
||||
vim.cmd('silent! update')
|
||||
end
|
||||
|
|
|
|||
|
|
@ -99,6 +99,35 @@ describe('compiler', function()
|
|||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('notifies and returns when binary is not executable', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_nobin.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local notified = false
|
||||
local orig = vim.notify
|
||||
vim.notify = function(msg)
|
||||
if msg:find('not executable') then
|
||||
notified = true
|
||||
end
|
||||
end
|
||||
|
||||
local provider = { cmd = { 'totally_nonexistent_binary_xyz_preview' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/preview_test_nobin.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'nobin', provider, ctx)
|
||||
vim.notify = orig
|
||||
|
||||
assert.is_true(notified)
|
||||
assert.is_nil(compiler._test.active[bufnr])
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('fires PreviewCompileFailed on non-zero exit', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_fail.txt')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue