Skip to content

Commit d88b0f2

Browse files
committed
feat(system): improve system command context error handling
Display error messages when system commands fail, showing the stderr output when available, or a generic failure message with the exit code when stderr is empty. Additionally, differentiate between successful and failed commands by using distinct filenames for better context in the Copilot Chat UI. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 60b0f11 commit d88b0f2

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lua/CopilotChat/config/contexts.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,25 @@ return {
324324
end
325325

326326
local out = utils.system({ shell, shell_flag, input })
327-
if not out or out.stdout == '' then
327+
if not out then
328328
return {}
329329
end
330330

331+
local out_type = 'command_output'
332+
local out_text = out.stdout
333+
if out.code ~= 0 then
334+
out_type = 'command_error'
335+
if out.stderr and out.stderr ~= '' then
336+
out_text = out.stderr
337+
elseif not out_text or out_text == '' then
338+
out_text = 'Command failed with exit code ' .. out.code
339+
end
340+
end
341+
331342
return {
332343
{
333-
content = out.stdout,
334-
filename = 'command_output_' .. input:gsub('[^%w]', '_'):sub(1, 20),
344+
content = out_text,
345+
filename = out_type .. '_' .. input:gsub('[^%w]', '_'):sub(1, 20),
335346
filetype = 'text',
336347
},
337348
}

0 commit comments

Comments
 (0)