Skip to content

Commit 3665ed0

Browse files
committed
feat: surface node version error reliably
1 parent 2b974b4 commit 3665ed0

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

lua/copilot/client.lua

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local M = {
1010
capabilities = nil,
1111
config = nil,
1212
node_version = nil,
13+
node_version_error = nil,
1314
startup_error = nil,
1415
}
1516

@@ -44,13 +45,14 @@ if not lsp_start then
4445
end
4546
end
4647

47-
---@return string
48+
---@return string node_version
49+
---@return nil|string node_version_error
4850
function M.get_node_version()
4951
if not M.node_version then
5052
local node = config.get("copilot_node_command")
5153

5254
local cmd = { node, "--version" }
53-
local cmd_output_table = vim.fn.systemlist(cmd, nil, false)
55+
local cmd_output_table = vim.fn.executable(node) == 1 and vim.fn.systemlist(cmd, nil, false) or { "" }
5456
local cmd_output = cmd_output_table[#cmd_output_table]
5557
local cmd_exit_code = vim.v.shell_error
5658

@@ -59,33 +61,25 @@ function M.get_node_version()
5961
local node_version_minor = tonumber(string.match(node_version, "^%d+%.(%d+)%.")) or 0
6062

6163
if node_version_major == 0 then
62-
local err = "[Copilot] Could not determine Node.js version"
63-
vim.notify(err, vim.log.levels.WARN)
64-
vim.api.nvim_echo({
65-
{
66-
table.concat({
67-
err,
68-
"-----------",
69-
"(exit code) " .. tostring(cmd_exit_code),
70-
" (output) " .. cmd_output,
71-
"-----------",
72-
}, "\n"),
73-
"MoreMsg",
74-
},
75-
}, true, {})
64+
M.node_version_error = table.concat({
65+
"Could not determine Node.js version",
66+
"-----------",
67+
"(exit code) " .. tostring(cmd_exit_code),
68+
" (output) " .. cmd_output,
69+
"-----------",
70+
}, "\n")
7671
elseif
7772
node_version_major < 16
7873
or (node_version_major == 16 and node_version_minor < 14)
7974
or (node_version_major == 17 and node_version_minor < 3)
8075
then
81-
local err = string.format("[Copilot] Node.js version 18.x or newer required but found %s", node_version)
82-
vim.notify(err, vim.log.levels.WARN)
76+
M.node_version_error = string.format("Node.js version 18.x or newer required but found %s", node_version)
8377
end
8478

8579
M.node_version = node_version or ""
8680
end
8781

88-
return M.node_version
82+
return M.node_version, M.node_version_error
8983
end
9084

9185
function M.buf_is_attached(bufnr)
@@ -208,15 +202,17 @@ local function prepare_client_config(overrides)
208202
end)
209203
end,
210204
on_exit = function(code, _signal, client_id)
211-
if code > 0 then
205+
if M.id == client_id then
212206
vim.schedule(function()
213-
-- in case for unsupported node
214-
M.get_node_version()
207+
M.teardown()
208+
M.id = nil
209+
M.capabilities = nil
215210
end)
216211
end
217-
if M.id == client_id then
218-
M.id = nil
219-
M.capabilities = nil
212+
if code > 0 then
213+
vim.schedule(function()
214+
require("copilot.command").status()
215+
end)
220216
end
221217
end,
222218
handlers = {

lua/copilot/command.lua

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local function node_version_warning(node_version)
1212
line = line
1313
.. " 'copilot_node_command' is set to a non-default value. Consider removing it from your configuration."
1414
end
15-
return { line, "WarningMsg" }
15+
return { line, "MoreMsg" }
1616
end
1717
end
1818

@@ -36,8 +36,11 @@ function mod.version()
3636
lines[#lines + 1] = "copilot/dist/agent.js" .. " " .. "not running"
3737
end
3838

39-
local node_version = c.get_node_version()
39+
local node_version, node_version_error = c.get_node_version()
4040
lines[#lines + 1] = "Node.js" .. " " .. (#node_version == 0 and "(unknown)" or node_version)
41+
if node_version_error then
42+
lines[#lines + 1] = { node_version_error, "WarningMsg" }
43+
end
4144
lines[#lines + 1] = node_version_warning(node_version)
4245

4346
local chunks = {}
@@ -58,23 +61,31 @@ function mod.status()
5861
return
5962
end
6063

61-
lines[#lines + 1] = type(line) == "table" and line or { "[Copilot] " .. line }
64+
lines[#lines + 1] = type(line) == "table" and { "[Copilot] " .. line[1], line[2] } or { "[Copilot] " .. line }
6265
lines[#lines + 1] = { "\n", "NONE" }
6366
end
6467

6568
local function flush_lines(last_line, is_off)
6669
add_line(last_line)
6770

71+
if c.startup_error then
72+
add_line({ c.startup_error, "WarningMsg" })
73+
end
74+
75+
local node_version, node_version_error = c.get_node_version()
76+
if node_version_error then
77+
add_line({ node_version_error, "WarningMsg" })
78+
end
79+
6880
if not is_off then
69-
add_line(node_version_warning(c.get_node_version()))
81+
add_line(node_version_warning(node_version))
7082
end
7183

7284
vim.api.nvim_echo(lines, true, {})
7385
end
7486

7587
if c.is_disabled() then
76-
add_line("Offline")
77-
flush_lines(c.startup_error, true)
88+
flush_lines("Offline", true)
7889
return
7990
end
8091

0 commit comments

Comments
 (0)