Skip to content

Commit

Permalink
fix(rocks): better errors / warnings when something goes wrong with l…
Browse files Browse the repository at this point in the history
…uarocks
  • Loading branch information
folke committed Jun 25, 2024
1 parent 9005e8e commit 7d3f691
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 133 deletions.
4 changes: 1 addition & 3 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,8 @@ function M.add_to_rtp(plugin)
local rtp = vim.api.nvim_get_runtime_file("", true)
local idx_dir, idx_after

local is_win = jit.os:find("Windows")

for i, path in ipairs(rtp) do
if is_win then
if Util.is_win then
path = Util.norm(path)
end
if path == Config.me then
Expand Down
8 changes: 0 additions & 8 deletions lua/lazy/core/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ function M:load_pkgs()
if not Config.options.pkg.enabled then
return
end
local have_rockspec = false
for _, pkg in ipairs(Pkg.get()) do
have_rockspec = have_rockspec or pkg.source == "rockspec"
local meta, fragment = self:add(pkg.spec)
if meta and fragment then
meta._.pkg = pkg
Expand All @@ -48,12 +46,6 @@ function M:load_pkgs()
self.pkgs[pkg.dir] = fragment.id
end
end
if have_rockspec then
local hererocks = Pkg.hererocks()
if hererocks then
self:add(hererocks)
end
end
end

--- Remove a plugin and all its fragments.
Expand Down
14 changes: 14 additions & 0 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ function M.load()
lazy._.loaded = {}
end

-- add hererocks when enabled and needed
if Config.options.rocks.hererocks then
for _, plugin in pairs(Config.spec.plugins) do
if plugin.build == "rockspec" then
Config.spec.meta:add({
"luarocks/hererocks",
build = "rockspec",
lazy = true,
})
break
end
end
end

local existing = Config.plugins
Config.plugins = Config.spec.plugins
-- copy state. This wont do anything during startup
Expand Down
1 change: 1 addition & 0 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local M = {}

---@type LazyProfile[]
M._profiles = { { name = "lazy" } }
M.is_win = jit.os:find("Windows")

---@param data (string|{[string]:string})?
---@param time number?
Expand Down
77 changes: 64 additions & 13 deletions lua/lazy/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,62 @@ local start = vim.health.start or vim.health.report_start
local ok = vim.health.ok or vim.health.report_ok
local warn = vim.health.warn or vim.health.report_warn
local error = vim.health.error or vim.health.report_error
local info = vim.health.info or vim.health.report_info

function M.check()
start("lazy.nvim")
---@class LazyHealth
---@field error? fun(msg:string)
---@field warn? fun(msg:string)
---@field ok? fun(msg:string)

if vim.fn.executable("git") == 1 then
ok("'git' installed")
else
error("'git' not installed?")
end
---@class LazyHealthHave : LazyHealth
---@field version? string
---@field version_pattern? string
---@field optional? boolean

if Config.options.rocks.enabled then
if vim.fn.executable("luarocks") == 1 then
ok("'luarocks' installed")
else
error("'luarocks' not installed. Either install it or set opts.rocks.enabled = false")
---@param cmd string|string[]
---@param opts? LazyHealthHave
function M.have(cmd, opts)
opts = vim.tbl_extend("force", {
error = error,
warn = warn,
ok = ok,
version = "--version",
}, opts or {})

cmd = type(cmd) == "table" and cmd or { cmd }
---@cast cmd string[]
---@type string?
local found
for _, c in ipairs(cmd) do
if vim.fn.executable(c) == 1 then
local version = vim.fn.system(c .. " " .. opts.version) or ""
version = vim.trim(vim.split(version, "\n")[1])
version = version:gsub("^%s*" .. vim.pesc(c) .. "%s*", "")
if opts.version_pattern and not version:find(opts.version_pattern, 1, true) then
opts.warn(("`%s` version `%s` needed, but found `%s`"):format(c, opts.version_pattern, version))
else
found = ("{%s} `%s`"):format(c, version)
break
end
end
end
if found then
opts.ok(found)
return true
else
(opts.optional and opts.warn or opts.error)(
("{%s} %snot installed"):format(
table.concat(cmd, "} or {"),
opts.version_pattern and "version `" .. opts.version_pattern .. "` " or ""
)
)
end
end

function M.check()
start("lazy.nvim")

M.have("git")

local sites = vim.opt.packpath:get()
local default_site = vim.fn.stdpath("data") .. "/site"
Expand All @@ -45,7 +84,7 @@ function M.check()
ok("no existing packages found by other package managers")
end

for _, name in ipairs({ "packer", "plugged", "paq" }) do
for _, name in ipairs({ "packer", "plugged", "paq", "pckr", "mini.deps" }) do
for _, path in ipairs(vim.opt.rtp:get()) do
if path:find(name, 1, true) then
error("Found paths on the rtp from another plugin manager `" .. name .. "`")
Expand Down Expand Up @@ -82,6 +121,18 @@ function M.check()
end
end
end

start("luarocks")
if Config.options.rocks.enabled then
if Config.options.rocks.hererocks then
info("checking `hererocks` installation")
else
info("checking `luarocks` installation")
end
require("lazy.pkg.rockspec").check({ error = error, warn = warn, ok = ok })
else
ok("luarocks disabled")
end
end

---@param plugin LazyPlugin
Expand Down
25 changes: 25 additions & 0 deletions lua/lazy/manage/task/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local Process = require("lazy.manage.process")
---@field output string
---@field status string
---@field error? string
---@field warn? string
---@field private _task fun(task:LazyTask)
---@field private _running LazyPluginState[]
---@field private _started? number
Expand Down Expand Up @@ -74,6 +75,30 @@ function Task:start()
self:_check()
end

---@param msg string|string[]
---@param severity? vim.diagnostic.Severity
function Task:notify(msg, severity)
local var = severity == vim.diagnostic.severity.ERROR and "error"
or severity == vim.diagnostic.severity.WARN and "warn"
or "output"
msg = type(msg) == "table" and table.concat(msg, "\n") or msg
---@cast msg string
---@diagnostic disable-next-line: no-unknown
self[var] = self[var] and (self[var] .. "\n" .. msg) or msg
self.status = msg
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
end

---@param msg string|string[]
function Task:notify_error(msg)
self:notify(msg, vim.diagnostic.severity.ERROR)
end

---@param msg string|string[]
function Task:notify_warn(msg)
self:notify(msg, vim.diagnostic.severity.WARN)
end

---@param fn async fun()
function Task:async(fn)
local co = coroutine.create(fn)
Expand Down
49 changes: 2 additions & 47 deletions lua/lazy/manage/task/plugin.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Config = require("lazy.core.config")
local Loader = require("lazy.core.loader")
local Rocks = require("lazy.pkg.rockspec")
local Util = require("lazy.util")

---@type table<string, LazyTaskDef>
Expand All @@ -16,52 +17,6 @@ end

local B = {}

---@param task LazyTask
function B.rockspec(task)
---@type table<string, string>
local env = {}

local luarocks = "luarocks"
if Config.options.rocks.hererocks then
local is_win = jit.os:find("Windows")
local sep = is_win and ";" or ":"
local hererocks = Config.options.rocks.root .. "/hererocks/bin"
if is_win then
hererocks = hererocks:gsub("/", "\\")
end
local path = vim.split(vim.env.PATH, sep)
table.insert(path, 1, hererocks)
env = {
PATH = table.concat(path, sep),
}
if is_win then
luarocks = luarocks .. ".bat"
end
local plugin = Config.plugins.hererocks
-- hererocks is still building, so skip for now
if plugin and plugin._.build then
return
end
end

local root = Config.options.rocks.root .. "/" .. task.plugin.name
task:spawn(luarocks, {
args = {
"--tree",
root,
"--server",
Config.options.rocks.server,
"--dev",
"--lua-version",
"5.1",
"make",
"--force-fast",
},
cwd = task.plugin.dir,
env = env,
})
end

---@param task LazyTask
---@param build string
function B.cmd(task, build)
Expand Down Expand Up @@ -114,7 +69,7 @@ M.build = {
build(self.plugin)
end)
elseif build == "rockspec" then
B.rockspec(self)
Rocks.build(self)
elseif build:sub(1, 1) == ":" then
B.cmd(self, build)
elseif build:match("%.lua$") then
Expand Down
26 changes: 0 additions & 26 deletions lua/lazy/pkg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,6 @@ local function _load()
Util.track()
end

---@return LazyPluginSpec?, string?
function M.hererocks()
if not (Config.options.rocks.enabled and Config.options.rocks.hererocks) then
return
end

local root = Config.options.rocks.root .. "/hererocks"

local cmd = {
"python",
"hererocks.py",
"--verbose",
"-l",
"5.1",
"-r",
"latest",
root,
}

return {
"luarocks/hererocks",
lazy = true,
build = table.concat(cmd, " "),
}, root
end

---@param dir string
---@return LazyPkg?
---@overload fun():LazyPkg[]
Expand Down
Loading

3 comments on commit 7d3f691

@mehalter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@folke Just a heads up, I am running into issues with this commit on fresh installations of Neovim configurations with the error message:

lazy/lazy.nvim/lua/lazy/core/meta.lua:97: attempt to concatenate local 'old_dir' (a nil value)

@folke
Copy link
Owner Author

@folke folke commented on 7d3f691 Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not home right now, but will check in about half an hour.
Would be great if you could provide a repro, or a dotfile repo in a new issue

@mehalter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I finally got it reduced down to a minimal configuration so I was able to open a proper issue.

Please sign in to comment.