Skip to content

Commit 68184a3

Browse files
committed
fix download server async
1 parent 8340d1d commit 68184a3

File tree

5 files changed

+127
-100
lines changed

5 files changed

+127
-100
lines changed

lua/eca/path_finder.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local uv = vim.uv or vim.loop
22
local Utils = require("eca.utils")
3+
local Config = require("eca.config")
34
local Logger = require("eca.logger")
45

56
---@class eca.PathFinder
@@ -184,15 +185,7 @@ function M:_download_latest_server(server_path, version)
184185
end
185186

186187
---@return string
187-
function M:find(custom_path)
188-
-- Check for custom server path first
189-
if custom_path and custom_path:gsub("%s+", "") ~= "" then
190-
if not Utils.file_exists(custom_path) then
191-
error("Custom server path does not exist: " .. custom_path)
192-
end
193-
return custom_path
194-
end
195-
188+
function M:find()
196189
local server_path = self:_get_extension_server_path()
197190
local latest_version = self:_get_latest_version()
198191
local current_version = self:_read_version_file()

lua/eca/server.lua

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,61 @@ local function on_stderr(err, data)
9191
end
9292
end
9393

94+
---@class eca.ServerStartOpts: vim.SystemOpts
95+
---@field cmd string[] The command to pass to vim.system
96+
---@field on_exit fun(out: vim.SystemCompleted) callback to pass to vim.system
97+
---@field initialize boolean Send the initialize message to ECA on startup, used
98+
---in testing
99+
---@param opts? eca.ServerStartOpts
100+
function M:_run(server_path, opts)
101+
Logger.debug("Starting ECA server: " .. server_path)
102+
103+
local args = { server_path, "server" }
104+
105+
if Config.server_args and Config.server_args ~= "" then
106+
vim.list_extend(args, vim.split(Config.server_args, " "))
107+
end
108+
109+
opts = vim.tbl_deep_extend("keep", opts, {
110+
cmd = args,
111+
text = true,
112+
cwd = self.cwd,
113+
stdin = true,
114+
stdout = on_stdout(self),
115+
stderr = on_stderr,
116+
---@param out vim.SystemCompleted
117+
on_exit = function(out)
118+
if out.code ~= 0 then
119+
require("eca.logger").notify(string.format("Server exited with status code %d", out.code), vim.log.levels
120+
.ERROR)
121+
end
122+
end,
123+
})
124+
125+
local started, process_or_err = pcall(vim.system, opts.cmd, {
126+
cwd = opts.cwd,
127+
text = opts.text,
128+
stdin = opts.stdin,
129+
stdout = opts.stdout,
130+
stderr = opts.stderr,
131+
}, opts.on_exit)
132+
133+
if not started then
134+
self.process = nil
135+
Logger.notify(vim.inspect(process_or_err), vim.log.levels.ERROR)
136+
return
137+
end
138+
139+
self.process = process_or_err
140+
if self.on_start then
141+
self.on_start(process_or_err.pid)
142+
end
143+
144+
if opts.initialize then
145+
self:initialize()
146+
end
147+
end
148+
94149
---@class eca.ServerStartOpts: vim.SystemOpts
95150
---@field cmd string[] The command to pass to vim.system
96151
---@field on_exit fun(out: vim.SystemCompleted) callback to pass to vim.system
@@ -100,6 +155,18 @@ end
100155
function M:start(opts)
101156
opts = vim.tbl_deep_extend("force", { initialize = true }, opts or {})
102157

158+
-- Check for custom server path first
159+
local custom_path = Config.server_path
160+
if custom_path and custom_path:gsub("%s+", "") ~= "" then
161+
if not Utils.file_exists(custom_path) then
162+
Logger.notify("Custom server path does not exist: " .. custom_path, vim.log.levels.ERROR)
163+
return
164+
end
165+
166+
self:_run(custom_path, opts)
167+
return
168+
end
169+
103170
local this_file = debug.getinfo(1, "S").source:sub(2)
104171
local proj_root = vim.fn.fnamemodify(this_file, ":p:h:h:h")
105172
local script_path = proj_root .. "/scripts/server_path.lua"
@@ -110,9 +177,7 @@ function M:start(opts)
110177
nvim_exe = "nvim"
111178
end
112179

113-
local lua_cmd = string.format("lua ServerPath.run(%s)", Utils.lua_quote(Config.server_path or ""))
114-
115-
local cmd = { nvim_exe, "--headless", "--noplugin", (opts.clean and " --clean" or ""), "-u", script_path, "-c", lua_cmd }
180+
local cmd = { nvim_exe, "--headless", "-S", script_path }
116181

117182
vim.system(cmd, { text = true }, function(out)
118183
if out.code ~= 0 then
@@ -123,52 +188,7 @@ function M:start(opts)
123188
local stdout_lines = Utils.split_lines(out.stdout)
124189
local server_path = stdout_lines[#stdout_lines]
125190

126-
Logger.debug("Starting ECA server: " .. server_path)
127-
128-
local args = { server_path, "server" }
129-
130-
if Config.server_args and Config.server_args ~= "" then
131-
vim.list_extend(args, vim.split(Config.server_args, " "))
132-
end
133-
134-
opts = vim.tbl_deep_extend("keep", opts, {
135-
cmd = args,
136-
text = true,
137-
cwd = self.cwd,
138-
stdin = true,
139-
stdout = on_stdout(self),
140-
stderr = on_stderr,
141-
---@param output vim.SystemCompleted
142-
on_exit = function(output)
143-
if output.code ~= 0 then
144-
require("eca.logger").notify(string.format("Server exited with status code %d", output.code), vim.log.levels
145-
.ERROR)
146-
end
147-
end,
148-
})
149-
150-
local started, process_or_err = pcall(vim.system, opts.cmd, {
151-
cwd = opts.cwd,
152-
text = opts.text,
153-
stdin = opts.stdin,
154-
stdout = opts.stdout,
155-
stderr = opts.stderr,
156-
}, opts.on_exit)
157-
158-
if not started then
159-
self.process = nil
160-
Logger.notify(vim.inspect(process_or_err), vim.log.levels.ERROR)
161-
return
162-
end
163-
164-
self.process = process_or_err
165-
if self.on_start then
166-
self.on_start(process_or_err.pid)
167-
end
168-
169-
if opts.initialize then
170-
self:initialize()
171-
end
191+
self:_run(server_path, opts)
172192
end)
173193
end
174194

scripts/server_path.lua

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
local ServerPath = {}
2-
31
-- Setup if headless
42
if #vim.api.nvim_list_uis() == 0 then
5-
_G.ServerPath = ServerPath
6-
73
-- hijack to make server tests work on CI using --clean mode
8-
local eca_available = pcall(require, "eca")
9-
if not eca_available then
4+
local ok = pcall(require, "eca")
5+
if not ok then
106
vim.cmd([[let &rtp.=','.getcwd()]])
117
vim.cmd('set rtp+=deps/nui.nvim')
128
vim.cmd('set rtp+=deps/eca-nvim')
9+
10+
local try_again, err = pcall(require, "eca")
11+
if not try_again then
12+
error("Could not load eca.nvim: " .. tostring(err))
13+
os.exit(1)
14+
end
1315
end
1416

1517
vim.o.swapfile = false
1618
vim.o.backup = false
1719
vim.o.writebackup = false
18-
19-
require("eca").setup({})
2020
end
2121

22-
ServerPath.run = function(custom_path)
23-
local path_finder = require("eca.path_finder"):new()
24-
local path
22+
local path_finder, path_finder_or_err = pcall(require, "eca.path_finder")
2523

26-
local ok, err = pcall(function()
27-
path = path_finder:find(custom_path)
28-
end)
24+
if not path_finder then
25+
io.stderr:write("Could not load eca.path_finder: " .. tostring(path_finder_or_err))
26+
os.exit(1)
27+
end
2928

30-
if not ok then
31-
io.stderr:write(tostring(err))
32-
os.exit(1)
33-
end
29+
local PathFinder = path_finder_or_err:new()
30+
local path
31+
32+
local ok, err = pcall(function()
33+
path = PathFinder:find()
34+
end)
3435

35-
io.stdout:write(tostring(path))
36-
os.exit(0)
36+
if not ok then
37+
io.stderr:write(tostring(err))
38+
os.exit(1)
3739
end
3840

39-
return ServerPath
41+
io.stdout:write(tostring(path))
42+
os.exit(0)

tests/test_server_integration.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ end
4040
T["server"] = MiniTest.new_set()
4141

4242
T["server"]["start"] = function()
43-
child.lua("_G.server:start({ clean = true })")
43+
child.lua("_G.server:start()")
4444
child.lua([[
4545
_G.server_started = vim.wait(10000, function()
4646
return _G.server and _G.server:is_running()
@@ -52,7 +52,7 @@ T["server"]["start"] = function()
5252
end
5353

5454
T["server"]["start without initialize"] = function()
55-
child.lua("_G.server:start({ clean = true, initialize = false })")
55+
child.lua("_G.server:start({ initialize = false })")
5656
child.lua([[
5757
_G.server_started = vim.wait(10000, function()
5858
return _G.server and _G.server:is_running()
@@ -65,18 +65,20 @@ end
6565

6666
T["server"]["start with inexistent path"] = function()
6767
child.lua([[
68-
Config = require("eca.config")
69-
Config.setup({ server_path = "non-existing-path" } )
70-
_G.server:start({ clean = true })
68+
_G.config = require("eca.config")
69+
_G.config.setup({ server_path = "non-existing-path" })
70+
_G.server:start()
7171
]])
7272
child.lua([[
7373
_G.server_started = vim.wait(1000, function()
7474
return _G.server and _G.server:is_running()
7575
end, 100)
7676
]])
77-
eq(string.find(child.lua_get("_G.notifications[1].message"), "non-existing-path", 1 , true) ~= nil, true)
77+
7878
eq(child.lua_get("_G.server_started"), false)
79+
sleep(1000)
7980
eq(child.lua_get("_G.server.initialized"), false)
81+
eq(string.find(child.lua_get("_G.notifications[1].message"), "non-existing-path", 1 , true) ~= nil, true)
8082
end
8183

8284
return T

tests/test_server_path.lua

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,35 @@ local function setup_test_environment()
88
return {
99
"nvim",
1010
"--headless",
11-
"--noplugin",
12-
"--clean",
1311
"--cmd",
14-
[[lua package.preload["eca.path_finder"] = function()
15-
local M = {}
16-
function M.new()
17-
return setmetatable({}, { __index = M })
12+
string.format([[lua
13+
package.preload["eca.config"] = function()
14+
local M = {}
15+
M.server_path = %q
16+
return M
1817
end
19-
function M:find(custom_path)
20-
if custom_path == "error" then
21-
error("custom-server-path-error")
18+
]], custom_path or ""),
19+
"--cmd",
20+
[[lua
21+
package.preload["eca.path_finder"] = function()
22+
local M = {}
23+
function M.new()
24+
return setmetatable({}, { __index = M })
25+
end
26+
function M:find()
27+
Config = require("eca.config")
28+
local custom_path = Config.server_path
29+
30+
if custom_path == "error" then
31+
error("custom-server-path-error")
32+
end
33+
return (custom_path ~= "" and custom_path) or "no-custom-server-path"
2234
end
23-
return (custom_path ~= "" and custom_path) or "no-custom-server-path"
35+
return M
2436
end
25-
return M
26-
end]],
27-
"-u",
37+
]],
38+
"-S",
2839
"scripts/server_path.lua",
29-
"-c",
30-
string.format("lua ServerPath.run(%s)", Utils.lua_quote(custom_path or "")),
3140
}
3241
end
3342
end

0 commit comments

Comments
 (0)