Skip to content

Commit f122741

Browse files
timhughAntoineGS
andauthored
feat: relax and improve validation for copilot_node_command and nodejs (#527)
Co-authored-by: Antoine Gaudreau Simard <[email protected]>
1 parent 9c62af8 commit f122741

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

lua/copilot/client/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ end
140140
function M.setup()
141141
logger.trace("setting up client")
142142
local node_command = config.copilot_node_command
143-
lsp.setup(config.server, node_command)
143+
if not lsp.setup(config.server, node_command) then
144+
is_disabled = true
145+
return
146+
end
147+
144148
M.config = require("copilot.client.config").prepare_client_config(config.server_opts_overrides, M)
145149

146150
if not M.config then

lua/copilot/lsp/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,25 @@ end
5454

5555
---@param server_config ServerConfig
5656
---@param copilot_node_command string
57+
---@return boolean
5758
function M.setup(server_config, copilot_node_command)
59+
local result = true
60+
5861
if not server_config then
5962
logger.error("server_config is required")
6063
end
6164

6265
if server_config.type == "nodejs" then
63-
M.nodejs.setup(copilot_node_command, server_config.custom_server_filepath)
66+
result = M.nodejs.setup(copilot_node_command, server_config.custom_server_filepath)
6467
elseif server_config.type == "binary" then
6568
M.binary.setup(server_config.custom_server_filepath)
6669
else
6770
logger.error("invalid server_config.type")
71+
result = false
6872
end
6973

7074
M.config = server_config
75+
return result
7176
end
7277

7378
return M

lua/copilot/lsp/nodejs.lua

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ local M = {
1313
---@return nil|string node_version_error
1414
function M.get_node_version()
1515
if not M.node_version then
16-
local cmd = { M.node_command, "--version" }
17-
local cmd_output_table = vim.fn.executable(M.node_command) == 1 and vim.fn.systemlist(cmd, nil, 0) or { "" }
18-
local cmd_output = cmd_output_table[#cmd_output_table]
19-
local cmd_exit_code = vim.v.shell_error
16+
local version_cmd = vim.split(M.node_command, " ")
17+
table.insert(version_cmd, "--version")
18+
2019
local node_version_major = 0
2120
local node_version = ""
22-
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-
else
27-
cmd_output = "[no output]"
21+
local cmd_exit_code = -1
22+
local cmd_output = "[no output]"
23+
local ok, process = pcall(vim.system, version_cmd)
24+
25+
if ok and process then
26+
local result = process:wait()
27+
cmd_output = result.stdout or cmd_output
28+
cmd_exit_code = result.code
29+
30+
if cmd_output and cmd_output ~= "[no output]" then
31+
node_version = string.match(cmd_output, "^v(%S+)") or node_version
32+
node_version_major = tonumber(string.match(node_version, "^(%d+)%.")) or node_version_major
33+
end
2834
end
2935

3036
if node_version_major == 0 then
@@ -63,17 +69,6 @@ function M.validate_node_version()
6369
return true
6470
end
6571

66-
function M.node_exists()
67-
local node_exists = vim.fn.executable(M.node_command) == 1
68-
69-
if not node_exists then
70-
logger.error("node.js is not installed or not in PATH")
71-
return false
72-
end
73-
74-
return true
75-
end
76-
7772
---@param server_path? string
7873
---@return boolean
7974
function M.init_agent_path(server_path)
@@ -101,11 +96,10 @@ end
10196

10297
---@return table
10398
function M.get_execute_command()
104-
return {
105-
M.node_command,
106-
M.server_path or M.get_server_path(),
107-
"--stdio",
108-
}
99+
local cmd = vim.split(M.node_command, " ")
100+
table.insert(cmd, M.server_path or M.get_server_path())
101+
table.insert(cmd, "--stdio")
102+
return cmd
109103
end
110104

111105
---@param node_command? string
@@ -114,7 +108,7 @@ end
114108
function M.setup(node_command, custom_server_path)
115109
M.node_command = node_command or "node"
116110

117-
if not M.node_exists() or not M.validate_node_version() or not M.init_agent_path(custom_server_path) then
111+
if not M.validate_node_version() or not M.init_agent_path(custom_server_path) then
118112
return false
119113
end
120114

0 commit comments

Comments
 (0)