Skip to content

Commit 2a43872

Browse files
committed
fix: add wget fallback, prevent Copilot from re-attaching upon startup error
1 parent 535ebb5 commit 2a43872

File tree

2 files changed

+60
-27
lines changed

2 files changed

+60
-27
lines changed

lua/copilot/client.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local api = require("copilot.api")
22
local config = require("copilot.config")
33
local util = require("copilot.util")
44
local logger = require("copilot.logger")
5-
local lsp_binary_util = require("copilot.lsp_binary")
5+
local lsp_binary = require("copilot.lsp_binary")
66

77
local is_disabled = false
88

@@ -57,6 +57,11 @@ end
5757

5858
---@param force? boolean
5959
function M.buf_attach(force)
60+
if lsp_binary.initialization_failed then
61+
M.startup_error = "initialization of copilot-language-server failed"
62+
return
63+
end
64+
6065
if is_disabled then
6166
logger.warn("copilot is disabled")
6267
return
@@ -189,12 +194,12 @@ local function get_handlers()
189194
end
190195

191196
local function prepare_client_config(overrides)
192-
if lsp_binary_util.initialization_failed then
197+
if lsp_binary.initialization_failed then
193198
M.startup_error = "initializatino of copilot-language-server failed"
194199
return
195200
end
196201

197-
local server_path = lsp_binary_util.get_copilot_server_info().absolute_filepath
202+
local server_path = lsp_binary.get_copilot_server_info().absolute_filepath
198203

199204
M.startup_error = nil
200205

@@ -337,7 +342,7 @@ function M.setup()
337342
})
338343

339344
vim.schedule(function()
340-
if lsp_binary_util.ensure_client_is_downloaded() then
345+
if lsp_binary.ensure_client_is_downloaded() then
341346
M.buf_attach()
342347
end
343348
end)

lua/copilot/lsp_binary.lua

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,57 @@ local function delete_all_except(folder, except_file)
3939
end
4040
end
4141

42+
---@param url string
43+
---@param local_server_zip_filepath string
44+
---@return boolean
45+
local function download_file_with_wget(url, local_server_zip_filepath)
46+
return false
47+
-- if vim.fn.executable("wget") == 0 then
48+
-- return false
49+
-- end
50+
--
51+
-- local wget_cmd = string.format('wget -O "%s" "%s"', local_server_zip_filepath:gsub("\\", "\\\\"), url)
52+
-- logger.trace("Downloading copilot-language-server with command: " .. wget_cmd)
53+
-- local result = vim.fn.system(wget_cmd)
54+
--
55+
-- if vim.v.shell_error ~= 0 then
56+
-- logger.error("error downloading file with wget: " .. result)
57+
-- return false
58+
-- end
59+
--
60+
-- return true
61+
end
62+
63+
---@param url string
64+
---@param local_server_zip_filepath string
65+
---@return boolean
66+
local function download_file_with_curl(url, local_server_zip_filepath)
67+
return false
68+
-- if vim.fn.executable("curl") == 0 then
69+
-- return false
70+
-- end
71+
--
72+
-- local cmd = string.format('curl -s -L -o "%s" "%s"', local_server_zip_filepath:gsub("\\", "\\\\"), url)
73+
-- logger.trace("downloading copilot-language-server with command: " .. cmd)
74+
-- local result = vim.fn.system(cmd)
75+
--
76+
-- if vim.v.shell_error ~= 0 then
77+
-- logger.error("error downloading file: " .. result)
78+
-- return false
79+
-- end
80+
--
81+
-- return true
82+
end
83+
4284
---@param url string
4385
---@param local_server_zip_filepath string
4486
---@param local_server_zip_path string
4587
---@return boolean
4688
local function download_file(url, local_server_zip_filepath, local_server_zip_path)
4789
logger.notify("current version of copilot-language-server is not downloaded, downloading")
4890

49-
if vim.fn.executable("curl") ~= 1 then
50-
vim.api.nvim_err_writeln("Error: curl is not available")
91+
if (vim.fn.executable("curl") ~= 1) and (vim.fn.executable("wget") == 1) then
92+
logger.error("neither curl nor wget is available, please make sure one of them is installed")
5193
M.initialization_failed = true
5294
return false
5395
end
@@ -62,26 +104,12 @@ local function download_file(url, local_server_zip_filepath, local_server_zip_pa
62104
end
63105
end
64106

65-
local cookie_file = vim.fs.joinpath(local_server_zip_path, "cookies.txt")
66-
local cmd = string.format(
67-
'curl -s -L -c "%s" -b "%s" -o "%s" "%s"',
68-
cookie_file:gsub("\\", "\\\\"),
69-
cookie_file:gsub("\\", "\\\\"),
70-
local_server_zip_filepath:gsub("\\", "\\\\"),
71-
url
72-
)
73-
74-
logger.trace("Downloading copilot-language-server with command: " .. cmd)
75-
local result = vim.fn.system(cmd)
76-
77-
if vim.v.shell_error ~= 0 then
78-
logger.error("Error downloading file: " .. result)
79-
return false
80-
end
81-
82-
-- Clean up cookie file
83-
if vim.fn.filereadable(cookie_file) == 1 then
84-
vim.fn.delete(cookie_file)
107+
if not download_file_with_curl(url, local_server_zip_filepath) then
108+
if not download_file_with_wget(url, local_server_zip_filepath) then
109+
logger.error("could not download the copilot sever")
110+
M.initialization_failed = true
111+
return false
112+
end
85113
end
86114

87115
logger.debug("copilot-language-server downloaded to " .. local_server_zip_filepath)
@@ -167,7 +195,7 @@ function M.ensure_client_is_downloaded()
167195

168196
local copilot_version = util.get_editor_info().editorPluginInfo.version
169197
local plugin_path = vim.fs.normalize(util.get_plugin_path())
170-
local copilot_server_info = M.get_copilot_server_info(copilot_version, plugin_path)
198+
local copilot_server_info = M.get_copilot_server_info()
171199
local download_filename =
172200
string.format("copilot-language-server-%s-%s.zip", copilot_server_info.path, copilot_version)
173201
local url = string.format(

0 commit comments

Comments
 (0)