health.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. local M = {}
  2. local health = require('vim.health')
  3. local shell_error = function()
  4. return vim.v.shell_error ~= 0
  5. end
  6. local suggest_faq = 'https://github.com/neovim/neovim/blob/master/BUILD.md#building'
  7. local function check_runtime()
  8. health.start('Runtime')
  9. -- Files from an old installation.
  10. local bad_files = {
  11. ['autoload/health/nvim.vim'] = false,
  12. ['autoload/health/provider.vim'] = false,
  13. ['autoload/man.vim'] = false,
  14. ['lua/provider/node/health.lua'] = false,
  15. ['lua/provider/perl/health.lua'] = false,
  16. ['lua/provider/python/health.lua'] = false,
  17. ['lua/provider/ruby/health.lua'] = false,
  18. ['plugin/health.vim'] = false,
  19. ['plugin/man.vim'] = false,
  20. ['queries/help/highlights.scm'] = false,
  21. ['queries/help/injections.scm'] = false,
  22. ['scripts.vim'] = false,
  23. ['syntax/syncolor.vim'] = false,
  24. }
  25. local bad_files_msg = ''
  26. for k, _ in pairs(bad_files) do
  27. local path = ('%s/%s'):format(vim.env.VIMRUNTIME, k)
  28. if vim.uv.fs_stat(path) then
  29. bad_files[k] = true
  30. bad_files_msg = ('%s%s\n'):format(bad_files_msg, path)
  31. end
  32. end
  33. local ok = (bad_files_msg == '')
  34. local info = ok and health.ok or health.info
  35. info(string.format('$VIMRUNTIME: %s', vim.env.VIMRUNTIME))
  36. if not ok then
  37. health.error(
  38. string.format(
  39. 'Found old files in $VIMRUNTIME (this can cause weird behavior):\n%s',
  40. bad_files_msg
  41. ),
  42. { 'Delete the $VIMRUNTIME directory, then reinstall Nvim.' }
  43. )
  44. end
  45. end
  46. local function check_config()
  47. health.start('Configuration')
  48. local ok = true
  49. local init_lua = vim.fn.stdpath('config') .. '/init.lua'
  50. local init_vim = vim.fn.stdpath('config') .. '/init.vim'
  51. local vimrc = vim.env.MYVIMRC and vim.fs.normalize(vim.env.MYVIMRC) or init_lua
  52. if vim.fn.filereadable(vimrc) == 0 and vim.fn.filereadable(init_vim) == 0 then
  53. ok = false
  54. local has_vim = vim.fn.filereadable(vim.fs.normalize('~/.vimrc')) == 1
  55. health.warn(
  56. ('%s user config file: %s'):format(
  57. -1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable',
  58. vimrc
  59. ),
  60. { has_vim and ':help nvim-from-vim' or ':help config' }
  61. )
  62. end
  63. -- If $VIM is empty we don't care. Else make sure it is valid.
  64. if vim.env.VIM and vim.fn.filereadable(vim.env.VIM .. '/runtime/doc/nvim.txt') == 0 then
  65. ok = false
  66. health.error('$VIM is invalid: ' .. vim.env.VIM)
  67. end
  68. if vim.env.NVIM_TUI_ENABLE_CURSOR_SHAPE then
  69. ok = false
  70. health.warn('$NVIM_TUI_ENABLE_CURSOR_SHAPE is ignored in Nvim 0.2+', {
  71. "Use the 'guicursor' option to configure cursor shape. :help 'guicursor'",
  72. 'https://github.com/neovim/neovim/wiki/Following-HEAD#20170402',
  73. })
  74. end
  75. if vim.v.ctype == 'C' then
  76. ok = false
  77. health.error(
  78. 'Locale does not support UTF-8. Unicode characters may not display correctly.'
  79. .. ('\n$LANG=%s $LC_ALL=%s $LC_CTYPE=%s'):format(
  80. vim.env.LANG,
  81. vim.env.LC_ALL,
  82. vim.env.LC_CTYPE
  83. ),
  84. {
  85. 'If using tmux, try the -u option.',
  86. 'Ensure that your terminal/shell/tmux/etc inherits the environment, or set $LANG explicitly.',
  87. 'Configure your system locale.',
  88. }
  89. )
  90. end
  91. if vim.o.paste == 1 then
  92. ok = false
  93. health.error(
  94. "'paste' is enabled. This option is only for pasting text.\nIt should not be set in your config.",
  95. {
  96. 'Remove `set paste` from your init.vim, if applicable.',
  97. 'Check `:verbose set paste?` to see if a plugin or script set the option.',
  98. }
  99. )
  100. end
  101. local writeable = true
  102. local shadaopt = vim.fn.split(vim.o.shada, ',')
  103. local shadafile = (
  104. vim.o.shada == '' and vim.o.shada
  105. or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '')
  106. )
  107. shadafile = (
  108. vim.o.shadafile == ''
  109. and (shadafile == '' and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fs.normalize(
  110. shadafile
  111. ))
  112. or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile)
  113. )
  114. if shadafile ~= '' and vim.fn.glob(shadafile) == '' then
  115. -- Since this may be the first time Nvim has been run, try to create a shada file.
  116. if not pcall(vim.cmd.wshada) then
  117. writeable = false
  118. end
  119. end
  120. if
  121. not writeable
  122. or (
  123. shadafile ~= ''
  124. and (vim.fn.filereadable(shadafile) == 0 or vim.fn.filewritable(shadafile) ~= 1)
  125. )
  126. then
  127. ok = false
  128. health.error(
  129. 'shada file is not '
  130. .. ((not writeable or vim.fn.filereadable(shadafile) == 1) and 'writeable' or 'readable')
  131. .. ':\n'
  132. .. shadafile
  133. )
  134. end
  135. if ok then
  136. health.ok('no issues found')
  137. end
  138. end
  139. local function check_performance()
  140. health.start('Performance')
  141. -- Check buildtype
  142. local buildtype = vim.fn.matchstr(vim.fn.execute('version'), [[\v\cbuild type:?\s*[^\n\r\t ]+]])
  143. if buildtype == '' then
  144. health.error('failed to get build type from :version')
  145. elseif vim.regex([[\v(MinSizeRel|Release|RelWithDebInfo)]]):match_str(buildtype) then
  146. health.ok(buildtype)
  147. else
  148. health.info(buildtype)
  149. health.warn('Non-optimized debug build. Nvim will be slower.', {
  150. 'Install a different Nvim package, or rebuild with `CMAKE_BUILD_TYPE=RelWithDebInfo`.',
  151. suggest_faq,
  152. })
  153. end
  154. -- check for slow shell invocation
  155. local slow_cmd_time = 1.5
  156. local start_time = vim.fn.reltime()
  157. vim.fn.system('echo')
  158. local elapsed_time = vim.fn.reltimefloat(vim.fn.reltime(start_time))
  159. if elapsed_time > slow_cmd_time then
  160. health.warn(
  161. 'Slow shell invocation (took ' .. vim.fn.printf('%.2f', elapsed_time) .. ' seconds).'
  162. )
  163. end
  164. end
  165. -- Load the remote plugin manifest file and check for unregistered plugins
  166. local function check_rplugin_manifest()
  167. health.start('Remote Plugins')
  168. local existing_rplugins = {}
  169. for _, item in ipairs(vim.fn['remote#host#PluginsForHost']('python3')) do
  170. existing_rplugins[item.path] = 'python3'
  171. end
  172. local require_update = false
  173. local handle_path = function(path)
  174. local python_glob = vim.fn.glob(path .. '/rplugin/python*', true, true)
  175. if vim.tbl_isempty(python_glob) then
  176. return
  177. end
  178. local python_dir = python_glob[1]
  179. local python_version = vim.fs.basename(python_dir)
  180. local scripts = vim.fn.glob(python_dir .. '/*.py', true, true)
  181. vim.list_extend(scripts, vim.fn.glob(python_dir .. '/*/__init__.py', true, true))
  182. for _, script in ipairs(scripts) do
  183. local contents = vim.fn.join(vim.fn.readfile(script))
  184. if vim.regex([[\<\%(from\|import\)\s\+neovim\>]]):match_str(contents) then
  185. if vim.regex([[[\/]__init__\.py$]]):match_str(script) then
  186. script = vim.fn.tr(vim.fn.fnamemodify(script, ':h'), '\\', '/')
  187. end
  188. if not existing_rplugins[script] then
  189. local msg = vim.fn.printf('"%s" is not registered.', vim.fs.basename(path))
  190. if python_version == 'pythonx' then
  191. if vim.fn.has('python3') == 0 then
  192. msg = msg .. ' (python3 not available)'
  193. end
  194. elseif vim.fn.has(python_version) == 0 then
  195. msg = msg .. vim.fn.printf(' (%s not available)', python_version)
  196. else
  197. require_update = true
  198. end
  199. health.warn(msg)
  200. end
  201. break
  202. end
  203. end
  204. end
  205. for _, path in ipairs(vim.fn.map(vim.split(vim.o.runtimepath, ','), 'resolve(v:val)')) do
  206. handle_path(path)
  207. end
  208. if require_update then
  209. health.warn('Out of date', { 'Run `:UpdateRemotePlugins`' })
  210. else
  211. health.ok('Up to date')
  212. end
  213. end
  214. local function check_tmux()
  215. if not vim.env.TMUX or vim.fn.executable('tmux') == 0 then
  216. return
  217. end
  218. ---@param option string
  219. local get_tmux_option = function(option)
  220. local cmd = 'tmux show-option -qvg ' .. option -- try global scope
  221. local out = vim.fn.system(vim.fn.split(cmd))
  222. local val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
  223. if shell_error() then
  224. health.error('command failed: ' .. cmd .. '\n' .. out)
  225. return 'error'
  226. elseif val == '' then
  227. cmd = 'tmux show-option -qvgs ' .. option -- try session scope
  228. out = vim.fn.system(vim.fn.split(cmd))
  229. val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
  230. if shell_error() then
  231. health.error('command failed: ' .. cmd .. '\n' .. out)
  232. return 'error'
  233. end
  234. end
  235. return val
  236. end
  237. health.start('tmux')
  238. -- check escape-time
  239. local suggestions =
  240. { 'set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10', suggest_faq }
  241. local tmux_esc_time = get_tmux_option('escape-time')
  242. if tmux_esc_time ~= 'error' then
  243. if tmux_esc_time == '' then
  244. health.error('`escape-time` is not set', suggestions)
  245. elseif tonumber(tmux_esc_time) > 300 then
  246. health.error('`escape-time` (' .. tmux_esc_time .. ') is higher than 300ms', suggestions)
  247. else
  248. health.ok('escape-time: ' .. tmux_esc_time)
  249. end
  250. end
  251. -- check focus-events
  252. local tmux_focus_events = get_tmux_option('focus-events')
  253. if tmux_focus_events ~= 'error' then
  254. if tmux_focus_events == '' or tmux_focus_events ~= 'on' then
  255. health.warn(
  256. "`focus-events` is not enabled. |'autoread'| may not work.",
  257. { '(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on' }
  258. )
  259. else
  260. health.ok('focus-events: ' .. tmux_focus_events)
  261. end
  262. end
  263. -- check default-terminal and $TERM
  264. health.info('$TERM: ' .. vim.env.TERM)
  265. local cmd = 'tmux show-option -qvg default-terminal'
  266. local out = vim.fn.system(vim.fn.split(cmd))
  267. local tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
  268. if tmux_default_term == '' then
  269. cmd = 'tmux show-option -qvgs default-terminal'
  270. out = vim.fn.system(vim.fn.split(cmd))
  271. tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
  272. end
  273. if shell_error() then
  274. health.error('command failed: ' .. cmd .. '\n' .. out)
  275. elseif tmux_default_term ~= vim.env.TERM then
  276. health.info('default-terminal: ' .. tmux_default_term)
  277. health.error(
  278. '$TERM differs from the tmux `default-terminal` setting. Colors might look wrong.',
  279. { '$TERM may have been set by some rc (.bashrc, .zshrc, ...).' }
  280. )
  281. elseif not vim.regex([[\v(tmux-256color|screen-256color)]]):match_str(vim.env.TERM) then
  282. health.error(
  283. '$TERM should be "screen-256color" or "tmux-256color" in tmux. Colors might look wrong.',
  284. {
  285. 'Set default-terminal in ~/.tmux.conf:\nset-option -g default-terminal "screen-256color"',
  286. suggest_faq,
  287. }
  288. )
  289. end
  290. -- check for RGB capabilities
  291. local info = vim.fn.system({ 'tmux', 'show-messages', '-T' })
  292. local has_setrgbb = vim.fn.stridx(info, ' setrgbb: (string)') ~= -1
  293. local has_setrgbf = vim.fn.stridx(info, ' setrgbf: (string)') ~= -1
  294. if not has_setrgbb or not has_setrgbf then
  295. health.warn(
  296. "True color support could not be detected. |'termguicolors'| won't work properly.",
  297. {
  298. "Add the following to your tmux configuration file, replacing XXX by the value of $TERM outside of tmux:\nset-option -a terminal-features 'XXX:RGB'",
  299. "For older tmux versions use this instead:\nset-option -a terminal-overrides 'XXX:Tc'",
  300. }
  301. )
  302. end
  303. end
  304. local function check_terminal()
  305. if vim.fn.executable('infocmp') == 0 then
  306. return
  307. end
  308. health.start('terminal')
  309. local cmd = 'infocmp -L'
  310. local out = vim.fn.system(vim.fn.split(cmd))
  311. local kbs_entry = vim.fn.matchstr(out, 'key_backspace=[^,[:space:]]*')
  312. local kdch1_entry = vim.fn.matchstr(out, 'key_dc=[^,[:space:]]*')
  313. if
  314. shell_error()
  315. and (
  316. vim.fn.has('win32') == 0
  317. or vim.fn.matchstr(
  318. out,
  319. [[infocmp: couldn't open terminfo file .\+\%(conemu\|vtpcon\|win32con\)]]
  320. )
  321. == ''
  322. )
  323. then
  324. health.error('command failed: ' .. cmd .. '\n' .. out)
  325. else
  326. health.info(
  327. vim.fn.printf(
  328. 'key_backspace (kbs) terminfo entry: `%s`',
  329. (kbs_entry == '' and '? (not found)' or kbs_entry)
  330. )
  331. )
  332. health.info(
  333. vim.fn.printf(
  334. 'key_dc (kdch1) terminfo entry: `%s`',
  335. (kbs_entry == '' and '? (not found)' or kdch1_entry)
  336. )
  337. )
  338. end
  339. for _, env_var in ipairs({
  340. 'XTERM_VERSION',
  341. 'VTE_VERSION',
  342. 'TERM_PROGRAM',
  343. 'COLORTERM',
  344. 'SSH_TTY',
  345. }) do
  346. if vim.env[env_var] then
  347. health.info(string.format('$%s="%s"', env_var, vim.env[env_var]))
  348. end
  349. end
  350. end
  351. local function check_external_tools()
  352. health.start('External Tools')
  353. if vim.fn.executable('rg') == 1 then
  354. local rg = vim.fn.exepath('rg')
  355. local cmd = 'rg -V'
  356. local out = vim.fn.system(vim.fn.split(cmd))
  357. health.ok(('%s (%s)'):format(vim.trim(out), rg))
  358. else
  359. health.warn('ripgrep not available')
  360. end
  361. end
  362. function M.check()
  363. check_config()
  364. check_runtime()
  365. check_performance()
  366. check_rplugin_manifest()
  367. check_terminal()
  368. check_tmux()
  369. check_external_tools()
  370. end
  371. return M