provider.vim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. let s:shell_error = 0
  2. function! s:is_bad_response(s) abort
  3. return a:s =~? '\v(^unable)|(^error)|(^outdated)'
  4. endfunction
  5. function! s:trim(s) abort
  6. return substitute(a:s, '^\_s*\|\_s*$', '', 'g')
  7. endfunction
  8. " Convert '\' to '/'. Collapse '//' and '/./'.
  9. function! s:normalize_path(s) abort
  10. return substitute(substitute(a:s, '\', '/', 'g'), '/\./\|/\+', '/', 'g')
  11. endfunction
  12. " Returns TRUE if `cmd` exits with success, else FALSE.
  13. function! s:cmd_ok(cmd) abort
  14. call system(a:cmd)
  15. return v:shell_error == 0
  16. endfunction
  17. " Simple version comparison.
  18. function! s:version_cmp(a, b) abort
  19. let a = split(a:a, '\.', 0)
  20. let b = split(a:b, '\.', 0)
  21. for i in range(len(a))
  22. if str2nr(a[i]) > str2nr(b[i])
  23. return 1
  24. elseif str2nr(a[i]) < str2nr(b[i])
  25. return -1
  26. endif
  27. endfor
  28. return 0
  29. endfunction
  30. " Handler for s:system() function.
  31. function! s:system_handler(jobid, data, event) dict abort
  32. if a:event ==# 'stderr'
  33. if self.add_stderr_to_output
  34. let self.output .= join(a:data, '')
  35. else
  36. let self.stderr .= join(a:data, '')
  37. endif
  38. elseif a:event ==# 'stdout'
  39. let self.output .= join(a:data, '')
  40. elseif a:event ==# 'exit'
  41. let s:shell_error = a:data
  42. endif
  43. endfunction
  44. " Attempts to construct a shell command from an args list.
  45. " Only for display, to help users debug a failed command.
  46. function! s:shellify(cmd) abort
  47. if type(a:cmd) != type([])
  48. return a:cmd
  49. endif
  50. return join(map(copy(a:cmd),
  51. \'v:val =~# ''\m[^\-.a-zA-Z_/]'' ? shellescape(v:val) : v:val'), ' ')
  52. endfunction
  53. " Run a system command and timeout after 30 seconds.
  54. function! s:system(cmd, ...) abort
  55. let stdin = a:0 ? a:1 : ''
  56. let ignore_error = a:0 > 2 ? a:3 : 0
  57. let opts = {
  58. \ 'add_stderr_to_output': a:0 > 1 ? a:2 : 0,
  59. \ 'output': '',
  60. \ 'stderr': '',
  61. \ 'on_stdout': function('s:system_handler'),
  62. \ 'on_stderr': function('s:system_handler'),
  63. \ 'on_exit': function('s:system_handler'),
  64. \ }
  65. let jobid = jobstart(a:cmd, opts)
  66. if jobid < 1
  67. call health#report_error(printf('Command error (job=%d): `%s` (in %s)',
  68. \ jobid, s:shellify(a:cmd), string(getcwd())))
  69. let s:shell_error = 1
  70. return opts.output
  71. endif
  72. if !empty(stdin)
  73. call jobsend(jobid, stdin)
  74. endif
  75. let res = jobwait([jobid], 30000)
  76. if res[0] == -1
  77. call health#report_error(printf('Command timed out: %s', s:shellify(a:cmd)))
  78. call jobstop(jobid)
  79. elseif s:shell_error != 0 && !ignore_error
  80. let emsg = printf("Command error (job=%d, exit code %d): `%s` (in %s)",
  81. \ jobid, s:shell_error, s:shellify(a:cmd), string(getcwd()))
  82. if !empty(opts.output)
  83. let emsg .= "\noutput: " . opts.output
  84. end
  85. if !empty(opts.stderr)
  86. let emsg .= "\nstderr: " . opts.stderr
  87. end
  88. call health#report_error(emsg)
  89. endif
  90. return opts.output
  91. endfunction
  92. function! s:systemlist(cmd, ...) abort
  93. let stdout = split(s:system(a:cmd, a:0 ? a:1 : ''), "\n")
  94. if a:0 > 1 && !empty(a:2)
  95. return filter(stdout, '!empty(v:val)')
  96. endif
  97. return stdout
  98. endfunction
  99. " Fetch the contents of a URL.
  100. function! s:download(url) abort
  101. let has_curl = executable('curl')
  102. if has_curl && system(['curl', '-V']) =~# 'Protocols:.*https'
  103. let rv = s:system(['curl', '-sL', a:url], '', 1, 1)
  104. return s:shell_error ? 'curl error with '.a:url.': '.s:shell_error : rv
  105. elseif executable('python')
  106. let script = "
  107. \try:\n
  108. \ from urllib.request import urlopen\n
  109. \except ImportError:\n
  110. \ from urllib2 import urlopen\n
  111. \\n
  112. \response = urlopen('".a:url."')\n
  113. \print(response.read().decode('utf8'))\n
  114. \"
  115. let rv = s:system(['python', '-c', script])
  116. return empty(rv) && s:shell_error
  117. \ ? 'python urllib.request error: '.s:shell_error
  118. \ : rv
  119. endif
  120. return 'missing `curl` '
  121. \ .(has_curl ? '(with HTTPS support) ' : '')
  122. \ .'and `python`, cannot make web request'
  123. endfunction
  124. " Check for clipboard tools.
  125. function! s:check_clipboard() abort
  126. call health#report_start('Clipboard (optional)')
  127. if !empty($TMUX) && executable('tmux') && executable('pbpaste') && !s:cmd_ok('pbpaste')
  128. let tmux_version = matchstr(system('tmux -V'), '\d\+\.\d\+')
  129. call health#report_error('pbcopy does not work with tmux version: '.tmux_version,
  130. \ ['Install tmux 2.6+. https://superuser.com/q/231130',
  131. \ 'or use tmux with reattach-to-user-namespace. https://superuser.com/a/413233'])
  132. endif
  133. let clipboard_tool = provider#clipboard#Executable()
  134. if exists('g:clipboard') && empty(clipboard_tool)
  135. call health#report_error(
  136. \ provider#clipboard#Error(),
  137. \ ["Use the example in :help g:clipboard as a template, or don't set g:clipboard at all."])
  138. elseif empty(clipboard_tool)
  139. call health#report_warn(
  140. \ 'No clipboard tool found. Clipboard registers (`"+` and `"*`) will not work.',
  141. \ [':help clipboard'])
  142. else
  143. call health#report_ok('Clipboard tool found: '. clipboard_tool)
  144. endif
  145. endfunction
  146. " Get the latest Nvim Python client (pynvim) version from PyPI.
  147. function! s:latest_pypi_version() abort
  148. let pypi_version = 'unable to get pypi response'
  149. let pypi_response = s:download('https://pypi.python.org/pypi/pynvim/json')
  150. if !empty(pypi_response)
  151. try
  152. let pypi_data = json_decode(pypi_response)
  153. catch /E474/
  154. return 'error: '.pypi_response
  155. endtry
  156. let pypi_version = get(get(pypi_data, 'info', {}), 'version', 'unable to parse')
  157. endif
  158. return pypi_version
  159. endfunction
  160. " Get version information using the specified interpreter. The interpreter is
  161. " used directly in case breaking changes were introduced since the last time
  162. " Nvim's Python client was updated.
  163. "
  164. " Returns: [
  165. " {python executable version},
  166. " {current nvim version},
  167. " {current pypi nvim status},
  168. " {installed version status}
  169. " ]
  170. function! s:version_info(python) abort
  171. let pypi_version = s:latest_pypi_version()
  172. let python_version = s:trim(s:system([
  173. \ a:python,
  174. \ '-c',
  175. \ 'import sys; print(".".join(str(x) for x in sys.version_info[:3]))',
  176. \ ]))
  177. if empty(python_version)
  178. let python_version = 'unable to parse '.a:python.' response'
  179. endif
  180. let nvim_path = s:trim(s:system([
  181. \ a:python, '-c',
  182. \ 'import sys; ' .
  183. \ 'sys.path = [p for p in sys.path if p != ""]; ' .
  184. \ 'import neovim; print(neovim.__file__)']))
  185. if s:shell_error || empty(nvim_path)
  186. return [python_version, 'unable to load neovim Python module', pypi_version,
  187. \ nvim_path]
  188. endif
  189. " Assuming that multiple versions of a package are installed, sort them
  190. " numerically in descending order.
  191. function! s:compare(metapath1, metapath2) abort
  192. let a = matchstr(fnamemodify(a:metapath1, ':p:h:t'), '[0-9.]\+')
  193. let b = matchstr(fnamemodify(a:metapath2, ':p:h:t'), '[0-9.]\+')
  194. return a == b ? 0 : a > b ? 1 : -1
  195. endfunction
  196. " Try to get neovim.VERSION (added in 0.1.11dev).
  197. let nvim_version = s:system([a:python, '-c',
  198. \ 'from neovim import VERSION as v; '.
  199. \ 'print("{}.{}.{}{}".format(v.major, v.minor, v.patch, v.prerelease))'],
  200. \ '', 1, 1)
  201. if empty(nvim_version)
  202. let nvim_version = 'unable to find pynvim module version'
  203. let base = fnamemodify(nvim_path, ':h')
  204. let metas = glob(base.'-*/METADATA', 1, 1)
  205. \ + glob(base.'-*/PKG-INFO', 1, 1)
  206. \ + glob(base.'.egg-info/PKG-INFO', 1, 1)
  207. let metas = sort(metas, 's:compare')
  208. if !empty(metas)
  209. for meta_line in readfile(metas[0])
  210. if meta_line =~# '^Version:'
  211. let nvim_version = matchstr(meta_line, '^Version: \zs\S\+')
  212. break
  213. endif
  214. endfor
  215. endif
  216. endif
  217. let nvim_path_base = fnamemodify(nvim_path, ':~:h')
  218. let version_status = 'unknown; '.nvim_path_base
  219. if !s:is_bad_response(nvim_version) && !s:is_bad_response(pypi_version)
  220. if s:version_cmp(nvim_version, pypi_version) == -1
  221. let version_status = 'outdated; from '.nvim_path_base
  222. else
  223. let version_status = 'up to date'
  224. endif
  225. endif
  226. return [python_version, nvim_version, pypi_version, version_status]
  227. endfunction
  228. " Check the Python interpreter's usability.
  229. function! s:check_bin(bin) abort
  230. if !filereadable(a:bin) && (!has('win32') || !filereadable(a:bin.'.exe'))
  231. call health#report_error(printf('"%s" was not found.', a:bin))
  232. return 0
  233. elseif executable(a:bin) != 1
  234. call health#report_error(printf('"%s" is not executable.', a:bin))
  235. return 0
  236. endif
  237. return 1
  238. endfunction
  239. " Check "loaded" var for given a:provider.
  240. " Returns 1 if the caller should return (skip checks).
  241. function! s:disabled_via_loaded_var(provider) abort
  242. let loaded_var = 'g:loaded_'.a:provider.'_provider'
  243. if exists(loaded_var) && !exists('*provider#'.a:provider.'#Call')
  244. let v = eval(loaded_var)
  245. if 0 is v
  246. call health#report_info('Disabled ('.loaded_var.'='.v.').')
  247. return 1
  248. else
  249. call health#report_info('Disabled ('.loaded_var.'='.v.'). This might be due to some previous error.')
  250. endif
  251. endif
  252. return 0
  253. endfunction
  254. function! s:check_python() abort
  255. call health#report_start('Python 3 provider (optional)')
  256. let pyname = 'python3'
  257. let python_exe = ''
  258. let venv = exists('$VIRTUAL_ENV') ? resolve($VIRTUAL_ENV) : ''
  259. let host_prog_var = pyname.'_host_prog'
  260. let python_multiple = []
  261. if s:disabled_via_loaded_var(pyname)
  262. return
  263. endif
  264. let [pyenv, pyenv_root] = s:check_for_pyenv()
  265. if exists('g:'.host_prog_var)
  266. call health#report_info(printf('Using: g:%s = "%s"', host_prog_var, get(g:, host_prog_var)))
  267. endif
  268. let [pyname, pythonx_warnings] = provider#pythonx#Detect(3)
  269. if empty(pyname)
  270. call health#report_warn('No Python executable found that can `import neovim`. '
  271. \ . 'Using the first available executable for diagnostics.')
  272. elseif exists('g:'.host_prog_var)
  273. let python_exe = pyname
  274. endif
  275. " No Python executable could `import neovim`, or host_prog_var was used.
  276. if !empty(pythonx_warnings)
  277. call health#report_warn(pythonx_warnings, ['See :help provider-python for more information.',
  278. \ 'You may disable this provider (and warning) by adding `let g:loaded_python3_provider = 0` to your init.vim'])
  279. elseif !empty(pyname) && empty(python_exe)
  280. if !exists('g:'.host_prog_var)
  281. call health#report_info(printf('`g:%s` is not set. Searching for '
  282. \ . '%s in the environment.', host_prog_var, pyname))
  283. endif
  284. if !empty(pyenv)
  285. let python_exe = s:trim(s:system([pyenv, 'which', pyname], '', 1))
  286. if empty(python_exe)
  287. call health#report_warn(printf('pyenv could not find %s.', pyname))
  288. endif
  289. endif
  290. if empty(python_exe)
  291. let python_exe = exepath(pyname)
  292. if exists('$PATH')
  293. for path in split($PATH, has('win32') ? ';' : ':')
  294. let path_bin = s:normalize_path(path.'/'.pyname)
  295. if path_bin != s:normalize_path(python_exe)
  296. \ && index(python_multiple, path_bin) == -1
  297. \ && executable(path_bin)
  298. call add(python_multiple, path_bin)
  299. endif
  300. endfor
  301. if len(python_multiple)
  302. " This is worth noting since the user may install something
  303. " that changes $PATH, like homebrew.
  304. call health#report_info(printf('Multiple %s executables found. '
  305. \ . 'Set `g:%s` to avoid surprises.', pyname, host_prog_var))
  306. endif
  307. if python_exe =~# '\<shims\>'
  308. call health#report_warn(printf('`%s` appears to be a pyenv shim.', python_exe), [
  309. \ '`pyenv` is not in $PATH, your pyenv installation is broken. '
  310. \ .'Set `g:'.host_prog_var.'` to avoid surprises.',
  311. \ ])
  312. endif
  313. endif
  314. endif
  315. endif
  316. if !empty(python_exe) && !exists('g:'.host_prog_var)
  317. if empty(venv) && !empty(pyenv)
  318. \ && !empty(pyenv_root) && resolve(python_exe) !~# '^'.pyenv_root.'/'
  319. call health#report_warn('pyenv is not set up optimally.', [
  320. \ printf('Create a virtualenv specifically '
  321. \ . 'for Nvim using pyenv, and set `g:%s`. This will avoid '
  322. \ . 'the need to install the pynvim module in each '
  323. \ . 'version/virtualenv.', host_prog_var)
  324. \ ])
  325. elseif !empty(venv)
  326. if !empty(pyenv_root)
  327. let venv_root = pyenv_root
  328. else
  329. let venv_root = fnamemodify(venv, ':h')
  330. endif
  331. if resolve(python_exe) !~# '^'.venv_root.'/'
  332. call health#report_warn('Your virtualenv is not set up optimally.', [
  333. \ printf('Create a virtualenv specifically '
  334. \ . 'for Nvim and use `g:%s`. This will avoid '
  335. \ . 'the need to install the pynvim module in each '
  336. \ . 'virtualenv.', host_prog_var)
  337. \ ])
  338. endif
  339. endif
  340. endif
  341. if empty(python_exe) && !empty(pyname)
  342. " An error message should have already printed.
  343. call health#report_error(printf('`%s` was not found.', pyname))
  344. elseif !empty(python_exe) && !s:check_bin(python_exe)
  345. let python_exe = ''
  346. endif
  347. " Diagnostic output
  348. call health#report_info('Executable: ' . (empty(python_exe) ? 'Not found' : python_exe))
  349. if len(python_multiple)
  350. for path_bin in python_multiple
  351. call health#report_info('Other python executable: ' . path_bin)
  352. endfor
  353. endif
  354. if empty(python_exe)
  355. " No Python executable can import 'neovim'. Check if any Python executable
  356. " can import 'pynvim'. If so, that Python failed to import 'neovim' as
  357. " well, which is most probably due to a failed pip upgrade:
  358. " https://github.com/neovim/neovim/wiki/Following-HEAD#20181118
  359. let [pynvim_exe, errors] = provider#pythonx#DetectByModule('pynvim', 3)
  360. if !empty(pynvim_exe)
  361. call health#report_error(
  362. \ 'Detected pip upgrade failure: Python executable can import "pynvim" but '
  363. \ . 'not "neovim": '. pynvim_exe,
  364. \ "Use that Python version to reinstall \"pynvim\" and optionally \"neovim\".\n"
  365. \ . pynvim_exe ." -m pip uninstall pynvim neovim\n"
  366. \ . pynvim_exe ." -m pip install pynvim\n"
  367. \ . pynvim_exe ." -m pip install neovim # only if needed by third-party software")
  368. endif
  369. else
  370. let [majorpyversion, current, latest, status] = s:version_info(python_exe)
  371. if 3 != str2nr(majorpyversion)
  372. call health#report_warn('Unexpected Python version.' .
  373. \ ' This could lead to confusing error messages.')
  374. endif
  375. call health#report_info('Python version: ' . majorpyversion)
  376. if s:is_bad_response(status)
  377. call health#report_info(printf('pynvim version: %s (%s)', current, status))
  378. else
  379. call health#report_info(printf('pynvim version: %s', current))
  380. endif
  381. if s:is_bad_response(current)
  382. call health#report_error(
  383. \ "pynvim is not installed.\nError: ".current,
  384. \ ['Run in shell: '. python_exe .' -m pip install pynvim'])
  385. endif
  386. if s:is_bad_response(latest)
  387. call health#report_warn('Could not contact PyPI to get latest version.')
  388. call health#report_error('HTTP request failed: '.latest)
  389. elseif s:is_bad_response(status)
  390. call health#report_warn(printf('Latest pynvim is NOT installed: %s', latest))
  391. elseif !s:is_bad_response(current)
  392. call health#report_ok(printf('Latest pynvim is installed.'))
  393. endif
  394. endif
  395. endfunction
  396. " Check if pyenv is available and a valid pyenv root can be found, then return
  397. " their respective paths. If either of those is invalid, return two empty
  398. " strings, effectivly ignoring pyenv.
  399. function! s:check_for_pyenv() abort
  400. let pyenv_path = resolve(exepath('pyenv'))
  401. if empty(pyenv_path)
  402. return ['', '']
  403. endif
  404. call health#report_info('pyenv: Path: '. pyenv_path)
  405. let pyenv_root = exists('$PYENV_ROOT') ? resolve($PYENV_ROOT) : ''
  406. if empty(pyenv_root)
  407. let pyenv_root = s:trim(s:system([pyenv_path, 'root']))
  408. call health#report_info('pyenv: $PYENV_ROOT is not set. Infer from `pyenv root`.')
  409. endif
  410. if !isdirectory(pyenv_root)
  411. call health#report_warn(
  412. \ printf('pyenv: Root does not exist: %s. '
  413. \ . 'Ignoring pyenv for all following checks.', pyenv_root))
  414. return ['', '']
  415. endif
  416. call health#report_info('pyenv: Root: '.pyenv_root)
  417. return [pyenv_path, pyenv_root]
  418. endfunction
  419. " Resolves Python executable path by invoking and checking `sys.executable`.
  420. function! s:python_exepath(invocation) abort
  421. return s:normalize_path(system(fnameescape(a:invocation)
  422. \ . ' -c "import sys; sys.stdout.write(sys.executable)"'))
  423. endfunction
  424. " Checks that $VIRTUAL_ENV Python executables are found at front of $PATH in
  425. " Nvim and subshells.
  426. function! s:check_virtualenv() abort
  427. call health#report_start('Python virtualenv')
  428. if !exists('$VIRTUAL_ENV')
  429. call health#report_ok('no $VIRTUAL_ENV')
  430. return
  431. endif
  432. let errors = []
  433. " Keep hints as dict keys in order to discard duplicates.
  434. let hints = {}
  435. " The virtualenv should contain some Python executables, and those
  436. " executables should be first both on Nvim's $PATH and the $PATH of
  437. " subshells launched from Nvim.
  438. let bin_dir = has('win32') ? '/Scripts' : '/bin'
  439. let venv_bins = glob($VIRTUAL_ENV . bin_dir . '/python*', v:true, v:true)
  440. " XXX: Remove irrelevant executables found in bin/.
  441. let venv_bins = filter(venv_bins, 'v:val !~# "python-config"')
  442. if len(venv_bins)
  443. for venv_bin in venv_bins
  444. let venv_bin = s:normalize_path(venv_bin)
  445. let py_bin_basename = fnamemodify(venv_bin, ':t')
  446. let nvim_py_bin = s:python_exepath(exepath(py_bin_basename))
  447. let subshell_py_bin = s:python_exepath(py_bin_basename)
  448. if venv_bin !=# nvim_py_bin
  449. call add(errors, '$PATH yields this '.py_bin_basename.' executable: '.nvim_py_bin)
  450. let hint = '$PATH ambiguities arise if the virtualenv is not '
  451. \.'properly activated prior to launching Nvim. Close Nvim, activate the virtualenv, '
  452. \.'check that invoking Python from the command line launches the correct one, '
  453. \.'then relaunch Nvim.'
  454. let hints[hint] = v:true
  455. endif
  456. if venv_bin !=# subshell_py_bin
  457. call add(errors, '$PATH in subshells yields this '
  458. \.py_bin_basename . ' executable: '.subshell_py_bin)
  459. let hint = '$PATH ambiguities in subshells typically are '
  460. \.'caused by your shell config overriding the $PATH previously set by the '
  461. \.'virtualenv. Either prevent them from doing so, or use this workaround: '
  462. \.'https://vi.stackexchange.com/a/34996'
  463. let hints[hint] = v:true
  464. endif
  465. endfor
  466. else
  467. call add(errors, 'no Python executables found in the virtualenv '.bin_dir.' directory.')
  468. endif
  469. let msg = '$VIRTUAL_ENV is set to: '.$VIRTUAL_ENV
  470. if len(errors)
  471. if len(venv_bins)
  472. let msg .= "\nAnd its ".bin_dir.' directory contains: '
  473. \.join(map(venv_bins, "fnamemodify(v:val, ':t')"), ', ')
  474. endif
  475. let conj = "\nBut "
  476. for error in errors
  477. let msg .= conj.error
  478. let conj = "\nAnd "
  479. endfor
  480. let msg .= "\nSo invoking Python may lead to unexpected results."
  481. call health#report_warn(msg, keys(hints))
  482. else
  483. call health#report_info(msg)
  484. call health#report_info('Python version: '
  485. \.system('python -c "import platform, sys; sys.stdout.write(platform.python_version())"'))
  486. call health#report_ok('$VIRTUAL_ENV provides :!python.')
  487. endif
  488. endfunction
  489. function! s:check_ruby() abort
  490. call health#report_start('Ruby provider (optional)')
  491. if s:disabled_via_loaded_var('ruby')
  492. return
  493. endif
  494. if !executable('ruby') || !executable('gem')
  495. call health#report_warn(
  496. \ '`ruby` and `gem` must be in $PATH.',
  497. \ ['Install Ruby and verify that `ruby` and `gem` commands work.'])
  498. return
  499. endif
  500. call health#report_info('Ruby: '. s:system(['ruby', '-v']))
  501. let [host, err] = provider#ruby#Detect()
  502. if empty(host)
  503. call health#report_warn('`neovim-ruby-host` not found.',
  504. \ ['Run `gem install neovim` to ensure the neovim RubyGem is installed.',
  505. \ 'Run `gem environment` to ensure the gem bin directory is in $PATH.',
  506. \ 'If you are using rvm/rbenv/chruby, try "rehashing".',
  507. \ 'See :help g:ruby_host_prog for non-standard gem installations.',
  508. \ 'You may disable this provider (and warning) by adding `let g:loaded_ruby_provider = 0` to your init.vim'])
  509. return
  510. endif
  511. call health#report_info('Host: '. host)
  512. let latest_gem_cmd = has('win32') ? 'cmd /c gem list -ra "^^neovim$"' : 'gem list -ra ^neovim$'
  513. let latest_gem = s:system(split(latest_gem_cmd))
  514. if s:shell_error || empty(latest_gem)
  515. call health#report_error('Failed to run: '. latest_gem_cmd,
  516. \ ["Make sure you're connected to the internet.",
  517. \ 'Are you behind a firewall or proxy?'])
  518. return
  519. endif
  520. let latest_gem = get(split(latest_gem, 'neovim (\|, \|)$' ), 0, 'not found')
  521. let current_gem_cmd = [host, '--version']
  522. let current_gem = s:system(current_gem_cmd)
  523. if s:shell_error
  524. call health#report_error('Failed to run: '. join(current_gem_cmd),
  525. \ ['Report this issue with the output of: ', join(current_gem_cmd)])
  526. return
  527. endif
  528. if s:version_cmp(current_gem, latest_gem) == -1
  529. call health#report_warn(
  530. \ printf('Gem "neovim" is out-of-date. Installed: %s, latest: %s',
  531. \ current_gem, latest_gem),
  532. \ ['Run in shell: gem update neovim'])
  533. else
  534. call health#report_ok('Latest "neovim" gem is installed: '. current_gem)
  535. endif
  536. endfunction
  537. function! s:check_node() abort
  538. call health#report_start('Node.js provider (optional)')
  539. if s:disabled_via_loaded_var('node')
  540. return
  541. endif
  542. if !executable('node') || (!executable('npm') && !executable('yarn') && !executable('pnpm'))
  543. call health#report_warn(
  544. \ '`node` and `npm` (or `yarn`, `pnpm`) must be in $PATH.',
  545. \ ['Install Node.js and verify that `node` and `npm` (or `yarn`, `pnpm`) commands work.'])
  546. return
  547. endif
  548. let node_v = get(split(s:system(['node', '-v']), "\n"), 0, '')
  549. call health#report_info('Node.js: '. node_v)
  550. if s:shell_error || s:version_cmp(node_v[1:], '6.0.0') < 0
  551. call health#report_warn('Nvim node.js host does not support '.node_v)
  552. " Skip further checks, they are nonsense if nodejs is too old.
  553. return
  554. endif
  555. if !provider#node#can_inspect()
  556. call health#report_warn('node.js on this system does not support --inspect-brk so $NVIM_NODE_HOST_DEBUG is ignored.')
  557. endif
  558. let [host, err] = provider#node#Detect()
  559. if empty(host)
  560. call health#report_warn('Missing "neovim" npm (or yarn, pnpm) package.',
  561. \ ['Run in shell: npm install -g neovim',
  562. \ 'Run in shell (if you use yarn): yarn global add neovim',
  563. \ 'Run in shell (if you use pnpm): pnpm install -g neovim',
  564. \ 'You may disable this provider (and warning) by adding `let g:loaded_node_provider = 0` to your init.vim'])
  565. return
  566. endif
  567. call health#report_info('Nvim node.js host: '. host)
  568. let manager = 'npm'
  569. if executable('yarn')
  570. let manager = 'yarn'
  571. elseif executable('pnpm')
  572. let manager = 'pnpm'
  573. endif
  574. let latest_npm_cmd = has('win32') ?
  575. \ 'cmd /c '. manager .' info neovim --json' :
  576. \ manager .' info neovim --json'
  577. let latest_npm = s:system(split(latest_npm_cmd))
  578. if s:shell_error || empty(latest_npm)
  579. call health#report_error('Failed to run: '. latest_npm_cmd,
  580. \ ["Make sure you're connected to the internet.",
  581. \ 'Are you behind a firewall or proxy?'])
  582. return
  583. endif
  584. try
  585. let pkg_data = json_decode(latest_npm)
  586. catch /E474/
  587. return 'error: '.latest_npm
  588. endtry
  589. let latest_npm = get(get(pkg_data, 'dist-tags', {}), 'latest', 'unable to parse')
  590. let current_npm_cmd = ['node', host, '--version']
  591. let current_npm = s:system(current_npm_cmd)
  592. if s:shell_error
  593. call health#report_error('Failed to run: '. join(current_npm_cmd),
  594. \ ['Report this issue with the output of: ', join(current_npm_cmd)])
  595. return
  596. endif
  597. if s:version_cmp(current_npm, latest_npm) == -1
  598. call health#report_warn(
  599. \ printf('Package "neovim" is out-of-date. Installed: %s, latest: %s',
  600. \ current_npm, latest_npm),
  601. \ ['Run in shell: npm install -g neovim',
  602. \ 'Run in shell (if you use yarn): yarn global add neovim',
  603. \ 'Run in shell (if you use pnpm): pnpm install -g neovim'])
  604. else
  605. call health#report_ok('Latest "neovim" npm/yarn/pnpm package is installed: '. current_npm)
  606. endif
  607. endfunction
  608. function! s:check_perl() abort
  609. call health#report_start('Perl provider (optional)')
  610. if s:disabled_via_loaded_var('perl')
  611. return
  612. endif
  613. let [perl_exec, perl_warnings] = provider#perl#Detect()
  614. if empty(perl_exec)
  615. if !empty(perl_warnings)
  616. call health#report_warn(perl_warnings, ['See :help provider-perl for more information.',
  617. \ 'You may disable this provider (and warning) by adding `let g:loaded_perl_provider = 0` to your init.vim'])
  618. else
  619. call health#report_warn('No usable perl executable found')
  620. endif
  621. return
  622. endif
  623. call health#report_info('perl executable: '. perl_exec)
  624. " we cannot use cpanm that is on the path, as it may not be for the perl
  625. " set with g:perl_host_prog
  626. call s:system([perl_exec, '-W', '-MApp::cpanminus', '-e', ''])
  627. if s:shell_error
  628. return [perl_exec, '"App::cpanminus" module is not installed']
  629. endif
  630. let latest_cpan_cmd = [perl_exec,
  631. \ '-MApp::cpanminus::fatscript', '-e',
  632. \ 'my $app = App::cpanminus::script->new;
  633. \ $app->parse_options ("--info", "-q", "Neovim::Ext");
  634. \ exit $app->doit']
  635. let latest_cpan = s:system(latest_cpan_cmd)
  636. if s:shell_error || empty(latest_cpan)
  637. call health#report_error('Failed to run: '. join(latest_cpan_cmd, " "),
  638. \ ["Make sure you're connected to the internet.",
  639. \ 'Are you behind a firewall or proxy?'])
  640. return
  641. elseif latest_cpan[0] ==# '!'
  642. let cpanm_errs = split(latest_cpan, '!')
  643. if cpanm_errs[0] =~# "Can't write to "
  644. call health#report_warn(cpanm_errs[0], cpanm_errs[1:-2])
  645. " Last line is the package info
  646. let latest_cpan = cpanm_errs[-1]
  647. else
  648. call health#report_error('Unknown warning from command: ' . latest_cpan_cmd, cpanm_errs)
  649. return
  650. endif
  651. endif
  652. let latest_cpan = matchstr(latest_cpan, '\(\.\?\d\)\+')
  653. if empty(latest_cpan)
  654. call health#report_error('Cannot parse version number from cpanm output: ' . latest_cpan)
  655. return
  656. endif
  657. let current_cpan_cmd = [perl_exec, '-W', '-MNeovim::Ext', '-e', 'print $Neovim::Ext::VERSION']
  658. let current_cpan = s:system(current_cpan_cmd)
  659. if s:shell_error
  660. call health#report_error('Failed to run: '. join(current_cpan_cmd),
  661. \ ['Report this issue with the output of: ', join(current_cpan_cmd)])
  662. return
  663. endif
  664. if s:version_cmp(current_cpan, latest_cpan) == -1
  665. call health#report_warn(
  666. \ printf('Module "Neovim::Ext" is out-of-date. Installed: %s, latest: %s',
  667. \ current_cpan, latest_cpan),
  668. \ ['Run in shell: cpanm -n Neovim::Ext'])
  669. else
  670. call health#report_ok('Latest "Neovim::Ext" cpan module is installed: '. current_cpan)
  671. endif
  672. endfunction
  673. function! health#provider#check() abort
  674. call s:check_clipboard()
  675. call s:check_python()
  676. call s:check_virtualenv()
  677. call s:check_ruby()
  678. call s:check_node()
  679. call s:check_perl()
  680. endfunction