Skip to content

Commit fce2ac6

Browse files
committed
fix(addon-loader): use Lua version from share/lua/5.X to require loader modules
1 parent cac6368 commit fce2ac6

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

src/luarocks/lls-addon-loader.lua

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,38 @@ local fs = require("bee.filesystem")
55
local furi = require("file-uri")
66
local workspace = require("workspace")
77

8-
local start, _, dir_sep = string.find(package.path, "([/\\])luarocks%1%?%.lua$")
8+
-- this relies on the plugin module's behavior with modifying
9+
-- package.path... very fragile
10+
local luarocksLuaVersion, start = string.match(package.path, "(%d%.%d)()([/\\])luarocks%3%?%.lua$")
911
assert(start, "could not replace path")
10-
package.path = string.sub(package.path, 1, start - 1) .. dir_sep .. "?.lua"
12+
package.path = string.sub(package.path, 1, start) .. "?.lua"
1113

1214
local cfg = require("luarocks.core.cfg") --[[@as luarocks.core.cfg]]
13-
require("luarocks.loader")
1415

1516
local workspaceRoot = fs.path(furi.decode(workspace.rootUri))
1617
local lua_modules = workspaceRoot / "lua_modules"
1718
if fs.exists(lua_modules:string()) then
1819
print("luarocks-build-lls-addon: configured workspace as project directory")
1920
-- re-initialize cfg with the project directory (loads in the project config from ./.luarocks)
20-
cfg.init({ project_dir = workspaceRoot:string() })
21+
assert(cfg.init({ project_dir = workspaceRoot:string(), lua_version = luarocksLuaVersion }))
22+
cfg.init_package_paths()
2123
-- add ./lua_modules as one of the queryable rocks trees
2224
table.insert(cfg.rocks_trees, 1, { name = "project", root = lua_modules:string() } --[[@as luarocks.core.Tree]])
2325
end
2426

27+
-- Stub out cfg.init() when `luarocks.loader` is running, since it calls the
28+
-- function unconditionally with no arguments.
29+
local oldInit = cfg.init
30+
function cfg.init()
31+
return true
32+
end
33+
require("luarocks.loader")
34+
cfg.init = oldInit
35+
36+
-- initialize it again with 5.5 so other plugins can use it
37+
cfg.init()
38+
cfg.init_package_paths()
39+
2540
-- because of how `plugin.dispatch` works, leaving these functions undefined
2641
-- means plugins defined later can't use these callbacks!
2742
-- all thanks to this line: https://github.com/LuaLS/lua-language-server/blob/0187ddf19f940d8b9b95d916d73f4660ec417471/script/plugin.lua#L35

0 commit comments

Comments
 (0)