robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
11.0 KiB339 linesraw
1local M = {}
2
3local health = vim.health
4
5local function get_lockfile_path()
6  return vim.pack._plugin_lock_path or vim.go.packlockfile
7end
8
9local function get_plug_dir()
10  return vim.fs.joinpath(vim.fn.stdpath('data'), 'site', 'pack', 'core', 'opt')
11end
12
13--- @param cmd string[]
14--- @param cwd string
15local function git_cmd(cmd, cwd)
16  cmd = vim.list_extend({ 'git', '-c', 'gc.auto=0' }, cmd)
17  local env = vim.fn.environ() --- @type table<string,string>
18  env.GIT_DIR, env.GIT_WORK_TREE, env.GIT_INDEX_FILE = nil, nil, nil
19  local sys_opts = { cwd = cwd, text = true, env = env, clear_env = true }
20  local out = vim.system(cmd, sys_opts):wait() --- @type vim.SystemCompleted
21  if out.code ~= 0 then
22    return false, ((out.stderr or ''):gsub('\n+$', ''))
23  end
24  return true, ((out.stdout or ''):gsub('\n+$', ''))
25end
26
27local function check_basics()
28  health.start('vim.pack: basics')
29
30  -- Requirements
31  if vim.fn.executable('git') == 0 then
32    health.warn('`git` executable is required. Install it using your package manager')
33    return false, false
34  end
35
36  -- Detect if not used
37  local lockfile_path = get_lockfile_path()
38  local has_lockfile = vim.fn.filereadable(lockfile_path) == 1
39  local plug_dir = get_plug_dir()
40  local has_plug_dir = vim.fn.isdirectory(plug_dir) == 1
41  if not has_lockfile and not has_plug_dir then
42    health.ok('`vim.pack` is not used')
43    return false, false
44  end
45
46  -- General info
47  local git = vim.fn.exepath('git')
48  local _, version = git_cmd({ 'version' }, vim.uv.cwd())
49  health.info(('Git: %s (%s)'):format(version:gsub('^git%s*', ''), git))
50  health.info('Lockfile: ' .. lockfile_path)
51  health.info('Plugin directory: ' .. plug_dir)
52
53  if has_lockfile and has_plug_dir then
54    health.ok('')
55  else
56    local lockfile_absent = has_lockfile and 'present' or 'absent'
57    local plug_dir_absent = has_plug_dir and 'present' or 'absent'
58    local msg = ('Lockfile is %s, plugin directory is %s.'):format(lockfile_absent, plug_dir_absent)
59      .. ' Restart Nvim and run `vim.pack.add({})` to '
60      .. (has_lockfile and 'install plugins from the lockfile' or 'regenerate the lockfile')
61    health.warn(msg)
62  end
63
64  return has_lockfile, has_plug_dir
65end
66
67--- @param x any
68local function is_version(x)
69  return type(x) == 'string' or (type(x) == 'table' and pcall(x.has, x, '1'))
70end
71
72--- @param plug_name string
73--- @param plug_path string
74local function failed_git_cmd(plug_name, plug_path)
75  local msg = ('Failed Git command inside plugin %s.'):format(vim.inspect(plug_name))
76    .. ' This is unexpected (like after interrupted `git clone`) and should not happen.'
77    .. (' Manually delete directory %s and reinstall plugin'):format(plug_path)
78  health.error(msg)
79  return false
80end
81
82--- @param plug_name any
83--- @param lock_data any
84--- @return boolean Whether a check is successful
85local function check_plugin_lock_data(plug_name, lock_data)
86  local name_str = vim.inspect(plug_name)
87  --- @param reason string
88  local function error_with_del_advice(reason)
89    local msg = ('%s %s.'):format(name_str, reason)
90      .. (' Delete %s entry (do not create trailing comma) and '):format(name_str)
91      .. 'restart Nvim to regenerate lockfile data'
92    health.error(msg)
93    return false
94  end
95
96  -- Types
97  if type(plug_name) ~= 'string' then
98    return error_with_del_advice('is not a valid plugin name')
99  end
100  if type(lock_data) ~= 'table' then
101    return error_with_del_advice('entry is not a valid type')
102  end
103  if type(lock_data.rev) ~= 'string' then
104    local reason = '`rev` entry is ' .. (lock_data.rev and 'not a valid type' or 'missing')
105    return error_with_del_advice(reason)
106  end
107  if type(lock_data.src) ~= 'string' then
108    local reason = '`src` entry is ' .. (lock_data.src and 'not a valid type' or 'missing')
109    return error_with_del_advice(reason)
110  end
111  if lock_data.version and not is_version(lock_data.version) then
112    return error_with_del_advice('`version` entry is not a valid type')
113  end
114
115  -- Alignment with what is actually present on disk
116  local plug_path = vim.fs.joinpath(get_plug_dir(), plug_name)
117  if vim.fn.isdirectory(plug_path) ~= 1 then
118    health.warn(
119      ('Plugin %s is not installed but present in the lockfile.'):format(name_str)
120        .. ' Restart Nvim and run `vim.pack.add({})` to autoinstall.'
121        .. (' To fully delete, run `vim.pack.del({ %s }, { force = true })`'):format(name_str)
122    )
123    return false
124  end
125
126  -- NOTE: `vim.pack` currently only supports Git repos as plugins
127  if not git_cmd({ 'rev-parse', '--git-dir' }, plug_path) then
128    return true
129  end
130
131  local has_head, head = git_cmd({ 'rev-list', '-1', 'HEAD' }, plug_path)
132  if not has_head then
133    return failed_git_cmd(plug_name, plug_path)
134  elseif lock_data.rev ~= head then
135    health.error(
136      ('Plugin %s is not at expected revision\n'):format(name_str)
137        .. ('Expected: %s\nActual:   %s\n'):format(lock_data.rev, head)
138        .. 'To synchronize, restart Nvim and run '
139        .. ('`vim.pack.update({ %s }, { offline = true })`\n'):format(name_str)
140        .. 'If there are no updates, delete `rev` lockfile entry (do not create trailing comma) '
141        .. 'and restart Nvim to regenerate lockfile data\n'
142        .. 'This can happen after updating plugins with read-only `$XDG_CONFIG_HOME`'
143    )
144    return false
145  end
146
147  local has_origin, origin = git_cmd({ 'remote', 'get-url', 'origin' }, plug_path)
148  if not has_origin then
149    return failed_git_cmd(plug_name, plug_path)
150  elseif lock_data.src ~= origin then
151    -- Check if lockfile source relies on "insteadOf" Git config
152    local ok, src_resolved = git_cmd({ 'ls-remote', '--get-url', lock_data.src }, plug_path)
153    if not (ok and src_resolved == origin) then
154      health.error(
155        ('Plugin %s has not expected source\n'):format(name_str)
156          .. ('Expected: %s\nActual:   %s\n'):format(lock_data.src, origin)
157          .. 'Delete `src` lockfile entry (do not create trailing comma) and '
158          .. 'restart Nvim to regenerate lockfile data'
159      )
160      return false
161    end
162  end
163
164  return true
165end
166
167local function check_lockfile()
168  health.start('vim.pack: lockfile')
169
170  local path = get_lockfile_path()
171  local can_read, text = pcall(vim.fn.readblob, path)
172  if not can_read then
173    health.error('Could not read lockfile. Delete it and restart Nvim.')
174    return
175  end
176
177  --- @cast text string
178  local can_parse, data = pcall(vim.json.decode, text)
179  if not can_parse then
180    health.error(('Could not parse lockfile: %s\nDelete it and restart Nvim'):format(data))
181    return
182  end
183
184  if type(data.plugins) ~= 'table' then
185    health.error('Field `plugins` is not proper type. Delete lockfile and restart Nvim')
186    return
187  end
188
189  local is_good = true
190  if path ~= vim.go.packlockfile then
191    health.warn(
192      "Lockfile path is not the same as 'packlockfile' option value. "
193        .. "Set 'packlockfile' before the first usage of `vim.pack` function."
194    )
195    is_good = false
196  end
197  if path ~= vim.fs.abspath(path) then
198    health.warn('Lockfile path is not absolute. Make sure that this is intentional.')
199    is_good = false
200  end
201
202  --- @cast data { plugins: table<any, any> }
203  for plug_name, lock_data in pairs(data.plugins) do
204    is_good = check_plugin_lock_data(plug_name, lock_data) and is_good
205  end
206
207  if is_good then
208    health.ok('')
209  end
210end
211
212--- @param manifest vim.pack.Manifest
213--- @param plug_name string
214--- @param plug_path string
215local function check_manifest(manifest, plug_name, plug_path)
216  local name_str = vim.inspect(plug_name)
217  if vim.tbl_count(manifest) == 0 then
218    health.warn(('Plugin %s has empty or malformed manifest file'):format(name_str))
219    return false
220  end
221
222  local nvim_engine = (manifest.engines or {}).nvim or '*'
223  local ok_version, nvim_version_range = pcall(vim.version.range, nvim_engine)
224  if not ok_version then
225    health.warn(('Plugin %s has malformed `engines.nvim` in manifest file'):format(name_str))
226    return false
227  end
228  --- @cast nvim_version_range vim.VersionRange
229  if not nvim_version_range:has(vim.version()) then
230    health.warn(
231      ('Plugin %s Nvim version requirement %s'):format(name_str, tostring(nvim_version_range))
232        .. (' does not match current version %s'):format(tostring(vim.version()))
233    )
234    return false
235  end
236
237  local ok_scripts = true
238  ---@diagnostic disable-next-line: no-unknown
239  for name, script_path in pairs(manifest.scripts or {}) do
240    if vim.fn.filereadable(vim.fs.joinpath(plug_path, script_path)) == 0 then
241      health.warn(('Plugin %s has no %s script at %s path'):format(name_str, name, script_path))
242      ok_scripts = false
243    end
244  end
245  if not ok_scripts then
246    return false
247  end
248
249  return true
250end
251
252--- @param plug_name string
253--- @return boolean Whether a check is successful
254local function check_installed_plugin(plug_name)
255  local name_str = vim.inspect(plug_name)
256  local plug_path = vim.fs.joinpath(get_plug_dir(), plug_name)
257
258  if vim.fn.isdirectory(plug_path) ~= 1 then
259    health.error(('%s is not a directory. Delete it'):format(plug_name))
260    return false
261  end
262
263  if not git_cmd({ 'rev-parse', '--git-dir' }, plug_path) then
264    health.error(
265      ('%s is not a Git repository.'):format(name_str)
266        .. ' It was not installed by `vim.pack` and should not be present in the plugin directory.'
267        .. ' If installed manually, use dedicated `:h packages`'
268    )
269    return false
270  end
271
272  -- Detached HEAD is a sign that plugin is managed by `vim.pack`
273  local has_head_ref, head_ref = git_cmd({ 'rev-parse', '--abbrev-ref', 'HEAD' }, plug_path)
274  if not has_head_ref then
275    return failed_git_cmd(plug_name, plug_path)
276  elseif head_ref ~= 'HEAD' then
277    health.warn(
278      ('Plugin %s is not at state which is a result of `vim.pack` operation.\n'):format(name_str)
279        .. 'If it was intentional, make sure you know what you are doing.\n'
280        .. 'Otherwise, restart Nvim and run '
281        .. ('`vim.pack.update({ %s }, { offline = true })`.\n'):format(name_str)
282        .. 'If nothing is updated, plugin is at correct revision and will be managed as expected'
283    )
284    return false
285  end
286
287  -- Usage data
288  local has_pack_info, info = pcall(vim.pack.get, { plug_name })
289  if not has_pack_info then
290    health.error('Could not get `vim.pack` usage information for plugin ' .. name_str)
291    return false
292  end
293  local plug = assert(info[1])
294
295  if not plug.active then
296    health.info(
297      ('Plugin %s is not active.'):format(name_str)
298        .. ' Is it lazy loaded or did you forget to run `vim.pack.del()`?'
299    )
300  end
301
302  -- Manifest
303  if plug.manifest then
304    return check_manifest(plug.manifest, plug_name, plug_path)
305  end
306
307  return true
308end
309
310local function check_plug_dir()
311  health.start('vim.pack: plugin directory')
312
313  local is_good = true
314  local plug_dir = get_plug_dir()
315  for plug_name, _, err in vim.fs.dir(plug_dir, { err = true }) do
316    if err then
317      health.error(err)
318      is_good = false
319    else
320      is_good = check_installed_plugin(plug_name) and is_good
321    end
322  end
323
324  if is_good then
325    health.ok('')
326  end
327end
328
329function M.check()
330  local has_lockfile, has_plug_dir = check_basics()
331  if has_lockfile then
332    check_lockfile()
333  end
334  if has_plug_dir then
335    check_plug_dir()
336  end
337end
338
339return M