Skip to content

Commit f22d869

Browse files
committed
refactor: improve error handling when node --version does not output anything
1 parent 55e4302 commit f22d869

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lua/copilot/lsp/nodejs.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ function M.get_node_version()
1717
local cmd_output_table = vim.fn.executable(M.node_command) == 1 and vim.fn.systemlist(cmd, nil, 0) or { "" }
1818
local cmd_output = cmd_output_table[#cmd_output_table]
1919
local cmd_exit_code = vim.v.shell_error
20+
local node_version_major = 0
21+
local node_version = ""
2022

21-
local node_version = string.match(cmd_output, "^v(%S+)") or ""
22-
local node_version_major = tonumber(string.match(node_version, "^(%d+)%.")) or 0
23+
if cmd_output then
24+
node_version = string.match(cmd_output, "^v(%S+)") or node_version
25+
node_version_major = tonumber(string.match(node_version, "^(%d+)%.")) or node_version_major
26+
end
2327

2428
if node_version_major == 0 then
2529
M.node_version_error = table.concat({
@@ -33,7 +37,7 @@ function M.get_node_version()
3337
M.node_version_error = string.format("Node.js version 20 or newer required but found %s", node_version)
3438
end
3539

36-
M.node_version = node_version or ""
40+
M.node_version = node_version
3741
end
3842

3943
return M.node_version, M.node_version_error

0 commit comments

Comments
 (0)