provider.vim 20.1 KB
Newer Older
1 2
let s:shell_error = 0

3
function! s:is_bad_response(s) abort
4
  return a:s =~? '\v(^unable)|(^error)|(^outdated)'
5 6 7 8 9 10
endfunction

function! s:trim(s) abort
  return substitute(a:s, '^\_s*\|\_s*$', '', 'g')
endfunction

11 12 13 14 15
" Convert '\' to '/'. Collapse '//' and '/./'.
function! s:normalize_path(s) abort
  return substitute(substitute(a:s, '\', '/', 'g'), '/\./\|/\+', '/', 'g')
endfunction

16 17 18 19 20 21
" Returns TRUE if `cmd` exits with success, else FALSE.
function! s:cmd_ok(cmd) abort
  call system(a:cmd)
  return v:shell_error == 0
endfunction

22 23
" Simple version comparison.
function! s:version_cmp(a, b) abort
24 25
  let a = split(a:a, '\.', 0)
  let b = split(a:b, '\.', 0)
26 27

  for i in range(len(a))
28
    if str2nr(a[i]) > str2nr(b[i])
29
      return 1
30
    elseif str2nr(a[i]) < str2nr(b[i])
31 32 33 34 35 36 37
      return -1
    endif
  endfor

  return 0
endfunction

38
" Handler for s:system() function.
39
function! s:system_handler(jobid, data, event) dict abort
40 41 42 43 44 45
  if a:event ==# 'stderr'
    let self.stderr .= join(a:data, '')
    if !self.ignore_stderr
      let self.output .= join(a:data, '')
    endif
  elseif a:event ==# 'stdout'
46
    let self.output .= join(a:data, '')
47
  elseif a:event ==# 'exit'
48 49 50 51
    let s:shell_error = a:data
  endif
endfunction

52 53 54 55 56 57 58
" Attempts to construct a shell command from an args list.
" Only for display, to help users debug a failed command.
function! s:shellify(cmd) abort
  if type(a:cmd) != type([])
    return a:cmd
  endif
  return join(map(copy(a:cmd),
59
    \'v:val =~# ''\m[^\-.a-zA-Z_/]'' ? shellescape(v:val) : v:val'), ' ')
60 61
endfunction

62 63 64
" Run a system command and timeout after 30 seconds.
function! s:system(cmd, ...) abort
  let stdin = a:0 ? a:1 : ''
65
  let ignore_error = a:0 > 2 ? a:3 : 0
66
  let opts = {
67
        \ 'ignore_stderr': a:0 > 1 ? a:2 : 0,
68
        \ 'output': '',
69
        \ 'stderr': '',
70
        \ 'on_stdout': function('s:system_handler'),
71
        \ 'on_stderr': function('s:system_handler'),
72 73 74 75 76
        \ 'on_exit': function('s:system_handler'),
        \ }
  let jobid = jobstart(a:cmd, opts)

  if jobid < 1
77 78
    call health#report_error(printf('Command error (job=%d): `%s` (in %s)',
          \ jobid, s:shellify(a:cmd), string(getcwd())))
79
    let s:shell_error = 1
80
    return opts.output
81 82 83 84 85 86 87 88
  endif

  if !empty(stdin)
    call jobsend(jobid, stdin)
  endif

  let res = jobwait([jobid], 30000)
  if res[0] == -1
89
    call health#report_error(printf('Command timed out: %s', s:shellify(a:cmd)))
90
    call jobstop(jobid)
91
  elseif s:shell_error != 0 && !ignore_error
92 93
    call health#report_error(printf("Command error (job=%d, exit code %d): `%s` (in %s)\nOutput: %s\nStderr: %s",
          \ jobid, s:shell_error, s:shellify(a:cmd), string(getcwd()), opts.output, opts.stderr))
94 95
  endif

96
  return opts.output
97 98 99 100 101 102 103 104 105 106
endfunction

function! s:systemlist(cmd, ...) abort
  let stdout = split(s:system(a:cmd, a:0 ? a:1 : ''), "\n")
  if a:0 > 1 && !empty(a:2)
    return filter(stdout, '!empty(v:val)')
  endif
  return stdout
endfunction

107 108 109
" Fetch the contents of a URL.
function! s:download(url) abort
  if executable('curl')
110 111
    let rv = s:system(['curl', '-sL', a:url], '', 1, 1)
    return s:shell_error ? 'curl error with '.a:url.': '.s:shell_error : rv
112
  elseif executable('python')
113 114 115 116 117 118
    let script = "
          \try:\n
          \    from urllib.request import urlopen\n
          \except ImportError:\n
          \    from urllib2 import urlopen\n
          \\n
119 120
          \response = urlopen('".a:url."')\n
          \print(response.read().decode('utf8'))\n
121
          \"
122 123 124
    let rv = s:system(['python', '-c', script])
    return empty(rv) && s:shell_error
          \ ? 'python urllib.request error: '.s:shell_error
125
          \ : rv
126
  endif
127
  return 'missing `curl` and `python`, cannot make pypi request'
128 129
endfunction

130 131
" Check for clipboard tools.
function! s:check_clipboard() abort
132
  call health#report_start('Clipboard (optional)')
133

134
  if !empty($TMUX) && executable('tmux') && executable('pbpaste') && !s:cmd_ok('pbpaste')
135 136 137 138 139 140
    let tmux_version = matchstr(system('tmux -V'), '\d\+\.\d\+')
    call health#report_error('pbcopy does not work with tmux version: '.tmux_version,
          \ ['Install tmux 2.6+.  https://superuser.com/q/231130',
          \  'or use tmux with reattach-to-user-namespace.  https://superuser.com/a/413233'])
  endif

141
  let clipboard_tool = provider#clipboard#Executable()
142 143 144 145 146
  if exists('g:clipboard') && empty(clipboard_tool)
    call health#report_error(
          \ provider#clipboard#Error(),
          \ ["Use the example in :help g:clipboard as a template, or don't set g:clipboard at all."])
  elseif empty(clipboard_tool)
147
    call health#report_warn(
148
          \ 'No clipboard tool found. Clipboard registers (`"+` and `"*`) will not work.',
149
          \ [':help clipboard'])
150 151 152 153
  else
    call health#report_ok('Clipboard tool found: '. clipboard_tool)
  endif
endfunction
154

155
" Get the latest Neovim Python client (pynvim) version from PyPI.
156
function! s:latest_pypi_version() abort
157
  let pypi_version = 'unable to get pypi response'
158
  let pypi_response = s:download('https://pypi.python.org/pypi/pynvim/json')
159 160 161 162 163 164 165
  if !empty(pypi_response)
    try
      let pypi_data = json_decode(pypi_response)
    catch /E474/
      return 'error: '.pypi_response
    endtry
    let pypi_version = get(get(pypi_data, 'info', {}), 'version', 'unable to parse')
166
  endif
167
  return pypi_version
168 169 170 171 172 173 174 175 176 177 178 179 180 181
endfunction

" Get version information using the specified interpreter.  The interpreter is
" used directly in case breaking changes were introduced since the last time
" Neovim's Python client was updated.
"
" Returns: [
"     {python executable version},
"     {current nvim version},
"     {current pypi nvim status},
"     {installed version status}
" ]
function! s:version_info(python) abort
  let pypi_version = s:latest_pypi_version()
182
  let python_version = s:trim(s:system([
183 184 185 186 187 188
        \ a:python,
        \ '-c',
        \ 'import sys; print(".".join(str(x) for x in sys.version_info[:3]))',
        \ ]))

  if empty(python_version)
189
    let python_version = 'unable to parse '.a:python.' response'
190 191
  endif

192
  let nvim_path = s:trim(s:system([
193 194
        \ a:python, '-c',
        \ 'import sys; sys.path.remove(""); ' .
195
        \ 'import neovim; print(neovim.__file__)']))
196
  if s:shell_error || empty(nvim_path)
197
    return [python_version, 'unable to load neovim Python module', pypi_version,
198
          \ nvim_path]
199 200
  endif

201 202
  " Assuming that multiple versions of a package are installed, sort them
  " numerically in descending order.
203
  function! s:compare(metapath1, metapath2) abort
204 205 206 207 208
    let a = matchstr(fnamemodify(a:metapath1, ':p:h:t'), '[0-9.]\+')
    let b = matchstr(fnamemodify(a:metapath2, ':p:h:t'), '[0-9.]\+')
    return a == b ? 0 : a > b ? 1 : -1
  endfunction

209
  " Try to get neovim.VERSION (added in 0.1.11dev).
210
  let nvim_version = s:system([a:python, '-c',
211
        \ 'from neovim import VERSION as v; '.
212 213 214
        \ 'print("{}.{}.{}{}".format(v.major, v.minor, v.patch, v.prerelease))'],
        \ '', 1, 1)
  if empty(nvim_version)
215
    let nvim_version = 'unable to find neovim Python module version'
216 217 218 219 220 221 222 223 224 225 226 227 228 229
    let base = fnamemodify(nvim_path, ':h')
    let metas = glob(base.'-*/METADATA', 1, 1)
          \ + glob(base.'-*/PKG-INFO', 1, 1)
          \ + glob(base.'.egg-info/PKG-INFO', 1, 1)
    let metas = sort(metas, 's:compare')

    if !empty(metas)
      for meta_line in readfile(metas[0])
        if meta_line =~# '^Version:'
          let nvim_version = matchstr(meta_line, '^Version: \zs\S\+')
          break
        endif
      endfor
    endif
230
  endif
231

232 233
  let nvim_path_base = fnamemodify(nvim_path, ':~:h')
  let version_status = 'unknown; '.nvim_path_base
234 235
  if !s:is_bad_response(nvim_version) && !s:is_bad_response(pypi_version)
    if s:version_cmp(nvim_version, pypi_version) == -1
236
      let version_status = 'outdated; from '.nvim_path_base
237 238 239 240 241 242 243 244 245 246
    else
      let version_status = 'up to date'
    endif
  endif

  return [python_version, nvim_version, pypi_version, version_status]
endfunction

" Check the Python interpreter's usability.
function! s:check_bin(bin) abort
247
  if !filereadable(a:bin) && (!has('win32') || !filereadable(a:bin.'.exe'))
248 249 250 251 252 253 254 255 256 257
    call health#report_error(printf('"%s" was not found.', a:bin))
    return 0
  elseif executable(a:bin) != 1
    call health#report_error(printf('"%s" is not executable.', a:bin))
    return 0
  endif
  return 1
endfunction

function! s:check_python(version) abort
258
  call health#report_start('Python ' . a:version . ' provider (optional)')
259

260
  let pyname = 'python'.(a:version == 2 ? '' : '3')
261
  let pyenv = resolve(exepath('pyenv'))
262
  let pyenv_root = exists('$PYENV_ROOT') ? resolve($PYENV_ROOT) : ''
263
  let venv = exists('$VIRTUAL_ENV') ? resolve($VIRTUAL_ENV) : ''
264
  let host_prog_var = pyname.'_host_prog'
265
  let loaded_var = 'g:loaded_'.pyname.'_provider'
266 267 268
  let python_bin = ''
  let python_multiple = []

269
  if exists(loaded_var) && !exists('*provider#'.pyname.'#Call')
270
    call health#report_info('Disabled ('.loaded_var.'='.eval(loaded_var).').  This might be due to some previous error.')
271 272
  endif

273 274
  if !empty(pyenv)
    if empty(pyenv_root)
275 276 277
      call health#report_info(
            \ 'pyenv was found, but $PYENV_ROOT is not set. `pyenv root` will be used.'
            \ .' If you run into problems, try setting $PYENV_ROOT explicitly.'
278
            \ )
279 280 281 282 283
      let pyenv_root = s:trim(s:system([pyenv, 'root']))
    endif

    if !isdirectory(pyenv_root)
      call health#report_error('Invalid pyenv root: '.pyenv_root)
284
    else
285 286
      call health#report_info(printf('pyenv: %s', pyenv))
      call health#report_info(printf('pyenv root: %s', pyenv_root))
287 288 289
    endif
  endif

290 291 292 293
  if exists('g:'.host_prog_var)
    call health#report_info(printf('Using: g:%s = "%s"', host_prog_var, get(g:, host_prog_var)))
  endif

294 295
  let [pyname, pythonx_errs] = provider#pythonx#Detect(a:version)
  if empty(pyname)
296
    call health#report_warn('No Python interpreter was found with the pynvim '
297
            \ . 'module.  Using the first available for diagnostics.')
298 299
  elseif exists('g:'.host_prog_var)
    let python_bin = pyname
300 301 302 303 304
  endif

  if !empty(pythonx_errs)
    call health#report_error('Python provider error', pythonx_errs)

305
  elseif !empty(pyname) && empty(python_bin)
306 307
    if !exists('g:'.host_prog_var)
      call health#report_info(printf('`g:%s` is not set.  Searching for '
308
            \ . '%s in the environment.', host_prog_var, pyname))
309 310 311
    endif

    if !empty(pyenv)
312
      let python_bin = s:trim(s:system([pyenv, 'which', pyname], '', 1))
313 314

      if empty(python_bin)
315
        call health#report_warn(printf('pyenv could not find %s.', pyname))
316 317 318 319
      endif
    endif

    if empty(python_bin)
320
      let python_bin = exepath(pyname)
321 322

      if exists('$PATH')
323
        for path in split($PATH, has('win32') ? ';' : ':')
324 325 326
          let path_bin = s:normalize_path(path.'/'.pyname)
          if path_bin != s:normalize_path(python_bin)
                \ && index(python_multiple, path_bin) == -1
327 328 329 330 331 332 333 334
                \ && executable(path_bin)
            call add(python_multiple, path_bin)
          endif
        endfor

        if len(python_multiple)
          " This is worth noting since the user may install something
          " that changes $PATH, like homebrew.
335
          call health#report_info(printf('Multiple %s executables found.  '
336
                \ . 'Set `g:%s` to avoid surprises.', pyname, host_prog_var))
337 338 339
        endif

        if python_bin =~# '\<shims\>'
340
          call health#report_warn(printf('`%s` appears to be a pyenv shim.', python_bin), [
341 342
                      \ '`pyenv` is not in $PATH, your pyenv installation is broken. '
                      \ .'Set `g:'.host_prog_var.'` to avoid surprises.',
343 344 345 346 347 348
                      \ ])
        endif
      endif
    endif
  endif

349 350
  if !empty(python_bin) && !exists('g:'.host_prog_var)
    if empty(venv) && !empty(pyenv)
351 352
          \ && !empty(pyenv_root) && resolve(python_bin) !~# '^'.pyenv_root.'/'
      call health#report_warn('pyenv is not set up optimally.', [
353 354
            \ printf('Create a virtualenv specifically '
            \ . 'for Neovim using pyenv, and set `g:%s`.  This will avoid '
355
            \ . 'the need to install the pynvim module in each '
356 357
            \ . 'version/virtualenv.', host_prog_var)
            \ ])
358
    elseif !empty(venv)
359 360 361 362 363 364 365 366
      if !empty(pyenv_root)
        let venv_root = pyenv_root
      else
        let venv_root = fnamemodify(venv, ':h')
      endif

      if resolve(python_bin) !~# '^'.venv_root.'/'
        call health#report_warn('Your virtualenv is not set up optimally.', [
367 368
              \ printf('Create a virtualenv specifically '
              \ . 'for Neovim and use `g:%s`.  This will avoid '
369
              \ . 'the need to install the pynvim module in each '
370 371 372 373 374 375
              \ . 'virtualenv.', host_prog_var)
              \ ])
      endif
    endif
  endif

376
  if empty(python_bin) && !empty(pyname)
377
    " An error message should have already printed.
378
    call health#report_error(printf('`%s` was not found.', pyname))
379 380 381 382
  elseif !empty(python_bin) && !s:check_bin(python_bin)
    let python_bin = ''
  endif

383
  " Check if $VIRTUAL_ENV is valid.
384 385 386 387
  if exists('$VIRTUAL_ENV') && !empty(python_bin)
    if $VIRTUAL_ENV ==# matchstr(python_bin, '^\V'.$VIRTUAL_ENV)
      call health#report_info('$VIRTUAL_ENV matches executable')
    else
388 389 390 391 392
      call health#report_warn(
        \ '$VIRTUAL_ENV exists but appears to be inactive. '
        \ . 'This could lead to unexpected results.',
        \ [ 'If you are using Zsh, see: http://vi.stackexchange.com/a/7654' ])
    endif
393 394 395 396 397 398 399 400 401 402
  endif

  " Diagnostic output
  call health#report_info('Executable: ' . (empty(python_bin) ? 'Not found' : python_bin))
  if len(python_multiple)
    for path_bin in python_multiple
      call health#report_info('Other python executable: ' . path_bin)
    endfor
  endif

403 404
  let pip = 'pip' . (a:version == 2 ? '' : '3')

405 406 407
  if !empty(python_bin)
    let [pyversion, current, latest, status] = s:version_info(python_bin)
    if a:version != str2nr(pyversion)
408
      call health#report_warn('Unexpected Python version.' .
409 410 411 412 413 414
                  \ ' This could lead to confusing error messages.')
    endif
    if a:version == 3 && str2float(pyversion) < 3.3
      call health#report_warn('Python 3.3+ is recommended.')
    endif

415
    call health#report_info('Python version: ' . pyversion)
416
    if s:is_bad_response(status)
417
      call health#report_info(printf('pynvim version: %s (%s)', current, status))
418
    else
419
      call health#report_info(printf('pynvim version: %s', current))
420 421 422 423 424 425 426 427 428
      let [module_found, _msg] = provider#pythonx#CheckForModule(python_bin,
            \ 'neovim', a:version)
      if !module_found
        call health#report_error('Importing "neovim" failed.',
              \ "Reinstall \"pynvim\" and optionally \"neovim\" packages.\n" .
              \    pip ." uninstall pynvim neovim\n" .
              \    pip ." install pynvim\n" .
              \    pip ." install neovim # only if needed by third-party software")
      endif
429
    endif
430 431 432

    if s:is_bad_response(current)
      call health#report_error(
433
        \ "pynvim is not installed.\nError: ".current,
434
        \ ['Run in shell: '. pip .' install pynvim'])
435 436 437
    endif

    if s:is_bad_response(latest)
438
      call health#report_warn('Could not contact PyPI to get latest version.')
439
      call health#report_error('HTTP request failed: '.latest)
440
    elseif s:is_bad_response(status)
441
      call health#report_warn(printf('Latest pynvim is NOT installed: %s', latest))
442
    elseif !s:is_bad_response(current)
443
      call health#report_ok(printf('Latest pynvim is installed.'))
444 445 446 447 448 449
    endif
  endif

endfunction

function! s:check_ruby() abort
450
  call health#report_start('Ruby provider (optional)')
451

452 453 454
  let loaded_var = 'g:loaded_ruby_provider'
  if exists(loaded_var) && !exists('*provider#ruby#Call')
    call health#report_info('Disabled. '.loaded_var.'='.eval(loaded_var))
455 456 457
    return
  endif

458 459
  if !executable('ruby') || !executable('gem')
    call health#report_warn(
460 461
          \ '`ruby` and `gem` must be in $PATH.',
          \ ['Install Ruby and verify that `ruby` and `gem` commands work.'])
462
    return
463
  endif
464 465 466 467
  call health#report_info('Ruby: '. s:system('ruby -v'))

  let host = provider#ruby#Detect()
  if empty(host)
468
    call health#report_warn('`neovim-ruby-host` not found.',
469 470 471 472
          \ ['Run `gem install neovim` to ensure the neovim RubyGem is installed.',
          \  'Run `gem environment` to ensure the gem bin directory is in $PATH.',
          \  'If you are using rvm/rbenv/chruby, try "rehashing".',
          \  'See :help g:ruby_host_prog for non-standard gem installations.'])
473 474 475 476
    return
  endif
  call health#report_info('Host: '. host)

477
  let latest_gem_cmd = has('win32') ? 'cmd /c gem list -ra ^^neovim$' : 'gem list -ra ^neovim$'
478 479 480 481
  let latest_gem = s:system(split(latest_gem_cmd))
  if s:shell_error || empty(latest_gem)
    call health#report_error('Failed to run: '. latest_gem_cmd,
          \ ["Make sure you're connected to the internet.",
482
          \  'Are you behind a firewall or proxy?'])
483 484
    return
  endif
485
  let latest_gem = get(split(latest_gem, 'neovim (\|, \|)$' ), 1, 'not found')
486 487 488 489 490

  let current_gem_cmd = host .' --version'
  let current_gem = s:system(current_gem_cmd)
  if s:shell_error
    call health#report_error('Failed to run: '. current_gem_cmd,
491
          \ ['Report this issue with the output of: ', current_gem_cmd])
492
    return
493 494
  endif

495 496 497 498 499 500
  if s:version_cmp(current_gem, latest_gem) == -1
    call health#report_warn(
          \ printf('Gem "neovim" is out-of-date. Installed: %s, latest: %s',
          \ current_gem, latest_gem),
          \ ['Run in shell: gem update neovim'])
  else
501
    call health#report_ok('Latest "neovim" gem is installed: '. current_gem)
502
  endif
503 504
endfunction

505
function! s:check_node() abort
506
  call health#report_start('Node.js provider (optional)')
507 508 509 510 511 512 513

  let loaded_var = 'g:loaded_node_provider'
  if exists(loaded_var) && !exists('*provider#node#Call')
    call health#report_info('Disabled. '.loaded_var.'='.eval(loaded_var))
    return
  endif

514
  if !executable('node') || (!executable('npm') && !executable('yarn'))
515
    call health#report_warn(
516 517
          \ '`node` and `npm` (or `yarn`) must be in $PATH.',
          \ ['Install Node.js and verify that `node` and `npm` (or `yarn`) commands work.'])
518 519
    return
  endif
520 521 522 523
  let node_v = get(split(s:system('node -v'), "\n"), 0, '')
  call health#report_info('Node.js: '. node_v)
  if !s:shell_error && s:version_cmp(node_v[1:], '6.0.0') < 0
    call health#report_warn('Neovim node.js host does not support '.node_v)
524 525
    " Skip further checks, they are nonsense if nodejs is too old.
    return
526 527 528 529
  endif
  if !provider#node#can_inspect()
    call health#report_warn('node.js on this system does not support --inspect-brk so $NVIM_NODE_HOST_DEBUG is ignored.')
  endif
530 531 532

  let host = provider#node#Detect()
  if empty(host)
533
    call health#report_warn('Missing "neovim" npm (or yarn) package.',
534
          \ ['Run in shell: npm install -g neovim',
535
          \  'Run in shell (if you use yarn): yarn global add neovim'])
536 537
    return
  endif
538
  call health#report_info('Neovim node.js host: '. host)
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

  let latest_npm_cmd = has('win32') ? 'cmd /c npm info neovim --json' : 'npm info neovim --json'
  let latest_npm = s:system(split(latest_npm_cmd))
  if s:shell_error || empty(latest_npm)
    call health#report_error('Failed to run: '. latest_npm_cmd,
          \ ["Make sure you're connected to the internet.",
          \  'Are you behind a firewall or proxy?'])
    return
  endif
  if !empty(latest_npm)
    try
      let pkg_data = json_decode(latest_npm)
    catch /E474/
      return 'error: '.latest_npm
    endtry
    let latest_npm = get(get(pkg_data, 'dist-tags', {}), 'latest', 'unable to parse')
  endif

557
  let current_npm_cmd = ['node', host, '--version']
558 559
  let current_npm = s:system(current_npm_cmd)
  if s:shell_error
560 561
    call health#report_error('Failed to run: '. string(current_npm_cmd),
          \ ['Report this issue with the output of: ', string(current_npm_cmd)])
562 563 564 565 566 567 568
    return
  endif

  if s:version_cmp(current_npm, latest_npm) == -1
    call health#report_warn(
          \ printf('Package "neovim" is out-of-date. Installed: %s, latest: %s',
          \ current_npm, latest_npm),
569
          \ ['Run in shell: npm install -g neovim'])
570
  else
571
    call health#report_ok('Latest "neovim" npm/yarn package is installed: '. current_npm)
572 573 574
  endif
endfunction

575
function! health#provider#check() abort
576
  call s:check_clipboard()
577 578 579
  call s:check_python(2)
  call s:check_python(3)
  call s:check_ruby()
580
  call s:check_node()
581
endfunction