robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local health =vim . health 2local iswin =vim . fn . has ( 'win32' ) == 1 3 4local M ={} 5 6---@param cmd string[] 7---@return boolean 8---@return string 9local function cmd_ok ( cmd ) 10local result =vim . system ( cmd , { text =true } ): wait () 11return result . code == 0 , assert ( result . stdout ) 12end 13 14---@param cmd string[] 15local function cli_version ( cmd ) 16local ok , out =cmd_ok ( cmd ) 17return ok , vim . version . parse ( out , { strict =false } ) 18end 19 20-- Attempts to construct a shell command from an args list. 21-- Only for display, to help users debug a failed command. 22--- @param cmd string|string[] 23local function shellify ( cmd ) 24if type ( cmd ) ~= 'table' then 25return cmd 26end 27local escaped ={} --- @type string[] 28for i , v in ipairs ( cmd ) do 29escaped [ i ] =v : match ( '[^A-Za-z_/.-]' ) and vim . fn . shellescape ( v ) or v 30end 31return table . concat ( escaped , ' ' ) 32end 33 34-- Handler for s:system() function. 35--- @param self {output: string, stderr: string, add_stderr_to_output: boolean} 36--- @param data string[] 37--- @param event 'stdout'|'stderr' 38local function system_handler ( self , _ , data , event ) 39if event == 'stderr' then 40if self . add_stderr_to_output then 41self . output =self . output .. table . concat ( data , '' ) 42else 43self . stderr =self . stderr .. table . concat ( data , '' ) 44end 45elseif event == 'stdout' then 46self . output =self . output .. table . concat ( data , '' ) 47end 48end 49 50--- @param cmd string|string[] List of command arguments to execute 51--- @param args? table Optional arguments: 52--- - stdin (string): Data to write to the job's stdin 53--- - stderr (boolean): Append stderr to stdout 54--- - ignore_error (boolean): If true, ignore error output 55--- - timeout (number): Number of seconds to wait before timing out (default 30) 56local function system ( cmd , args ) 57args =args or {} 58local stdin =args . stdin or '' 59local stderr =args . stderr or false 60local ignore_error =args . ignore_error or false 61 62local shell_error_code =0 63local opts ={ 64add_stderr_to_output =stderr , 65output ='' , 66stderr ='' , 67on_stdout =system_handler , 68on_stderr =system_handler , 69on_exit =function ( _ , data ) 70shell_error_code =data 71end , 72} 73local jobid =vim . fn . jobstart ( cmd , opts ) 74 75if jobid < 1 then 76local message = 77string . format ( 'Command error (job=%d): %s (in %s)' , jobid , shellify ( cmd ), vim . uv . cwd ()) 78error ( message ) 79return opts . output , 1 80end 81 82if stdin : find ( '^%s$' ) then 83vim . fn . chansend ( jobid , stdin ) 84end 85 86local res =vim . fn . jobwait ( { jobid } , math . floor ( vim . nonnil ( args . timeout , 30 ) * 1000 )) 87if res [ 1 ] == - 1 then 88error ( 'Command timed out: ' .. shellify ( cmd )) 89vim . fn . jobstop ( jobid ) 90elseif shell_error_code ~= 0 and not ignore_error then 91local emsg =string . format ( 92'Command error (job=%d, exit code %d): %s (in %s)' , 93jobid , 94shell_error_code , 95shellify ( cmd ), 96vim . uv . cwd () 97) 98if opts . output : find ( '%S' ) then 99emsg =string . format ( '%s\noutput: %s' , emsg , opts . output ) 100end 101if opts . stderr : find ( '%S' ) then 102emsg =string . format ( '%s\nstderr: %s' , emsg , opts . stderr ) 103end 104error ( emsg ) 105end 106 107return vim . trim ( vim . fn . system ( cmd )), shell_error_code 108end 109 110---@param provider string 111local function provider_disabled ( provider ) 112local loaded_var ='loaded_' .. provider .. '_provider' 113local v =vim . g [ loaded_var ] 114if v == 0 then 115health . info ( 'Disabled (' .. loaded_var .. '=' .. v .. ').' ) 116return true 117end 118return false 119end 120 121--- Checks the hygiene of a `g:loaded_xx_provider` variable. 122--- @param var string 123local function check_loaded_var ( var ) 124if vim . g [ var ] == 1 then 125health . error (( '`g:%s=1` may have been set by mistake.' ): format ( var ), { 126( 'Remove `vim.g.%s=1` from your config.' ): format ( var ), 127'To disable the provider, set this to 0, not 1.' , 128'If you want to enable the provider but skip automatic detection, set the respective `g:…_host_prog` var. See :help provider' , 129} ) 130end 131end 132 133local function clipboard () 134health . start ( 'Clipboard (optional)' ) 135 136check_loaded_var ( 'loaded_clipboard_provider' ) 137 138if 139os . getenv ( 'TMUX' ) 140and vim . fn . executable ( 'tmux' ) == 1 141and vim . fn . executable ( 'pbpaste' ) == 1 142and not cmd_ok ( { 'pbpaste' } ) 143then 144local _ , tmux_version =cli_version ( { 'tmux' , '-V' } ) 145local advice ={ 146'Install tmux 2.6+. https://superuser.com/q/231130' , 147'or use tmux with reattach-to-user-namespace. https://superuser.com/a/413233' , 148} 149health . error ( 'pbcopy does not work with tmux version: ' .. tostring ( tmux_version ), advice ) 150end 151 152local clipboard_tool =vim . fn [ 'provider#clipboard#Executable' ]() ---@type string 153if vim . g . clipboard ~= nil and clipboard_tool == '' then 154local error_message =vim . fn [ 'provider#clipboard#Error' ]() ---@type string 155health . error ( 156error_message , 157"Use the example in :help g:clipboard as a template, or don't set g:clipboard at all." 158) 159elseif clipboard_tool : find ( '^%s*$' ) then 160health . warn ( 161'No clipboard tool found. Clipboard registers (`"+` and `"*`) will not work.' , 162':help clipboard' 163) 164else 165health . ok ( 'Clipboard tool found: ' .. clipboard_tool ) 166end 167end 168 169local function node () 170health . start ( 'Node.js provider (optional)' ) 171 172check_loaded_var ( 'loaded_node_provider' ) 173 174if provider_disabled ( 'node' ) then 175return 176end 177 178if 179vim . fn . executable ( 'node' ) == 0 180or ( 181vim . fn . executable ( 'npm' ) == 0 182and vim . fn . executable ( 'yarn' ) == 0 183and vim . fn . executable ( 'pnpm' ) == 0 184and vim . fn . executable ( 'bun' ) == 0 185) 186then 187health . warn ( 188'`node` and `npm` (or `yarn`, `pnpm`, `bun`) must be in $PATH.' , 189'Install Node.js and verify that `node` and `npm` (or `yarn`, `pnpm`, `bun`) commands work.' 190) 191return 192end 193 194local ok , node_v =cli_version ( { 'node' , '-v' } ) 195health . info ( 'Node.js: ' .. tostring ( node_v )) 196if not ok or vim . version . lt ( assert ( node_v ), '6.0.0' ) then 197health . warn ( 'Nvim node.js host does not support Node ' .. node_v ) 198-- Skip further checks, they are nonsense if nodejs is too old. 199return 200end 201if vim . fn [ 'provider#node#can_inspect' ]() == 0 then 202health . warn ( 203'node.js on this system does not support --inspect-brk so $NVIM_NODE_HOST_DEBUG is ignored.' 204) 205end 206 207local node_detect_table =vim . fn [ 'provider#node#Detect' ]() ---@type [string, string] 208local host =node_detect_table [ 1 ] 209if host : find ( '^%s*$' ) then 210health . warn ( 'Missing "neovim" npm (or yarn, pnpm, bun) package.' , { 211'Run in shell: npm install -g neovim' , 212'Run in shell (if you use yarn): yarn global add neovim' , 213'Run in shell (if you use pnpm): pnpm install -g neovim' , 214'Run in shell (if you use bun): bun install -g neovim' , 215'You may disable this provider (and warning) by adding `let g:loaded_node_provider = 0` to your init.vim' , 216} ) 217return 218end 219health . info ( 'Nvim node.js host: ' .. host ) 220 221-- only npm/pnpm query the registry here: `yarn info` errors outside a 222-- project on yarn 2+ (#20924), and bun isn't used for this check. 223local manager --- @type string? 224if vim . fn . executable ( 'npm' ) == 1 then 225manager ='npm' 226elseif vim . fn . executable ( 'pnpm' ) == 1 then 227manager ='pnpm' 228end 229 230if not manager then 231health . info ( 'Skipping latest "neovim" package check: npm or pnpm not in $PATH.' ) 232return 233end 234 235local latest_npm_cmd ={ manager , 'info' , 'neovim' , '--json' } 236local latest_npm 237ok , latest_npm =cmd_ok ( latest_npm_cmd ) 238if not ok or latest_npm : find ( '^%s$' ) then 239health . error ( 240'Failed to run: ' .. shellify ( latest_npm_cmd ), 241{ "Make sure you're connected to the internet." , 'Are you behind a firewall or proxy?' } 242) 243return 244end 245 246local pcall_ok , pkg_data =pcall ( vim . json . decode , latest_npm ) 247if not pcall_ok then 248return 'error: ' .. latest_npm 249end 250local latest_npm_subtable =pkg_data [ 'dist-tags' ] or {} 251latest_npm =latest_npm_subtable [ 'latest' ] or 'unable to parse' 252 253local current_npm_cmd ={ 'node' , host , '--version' } 254local current_npm 255ok , current_npm =cmd_ok ( current_npm_cmd ) 256if not ok then 257health . error ( 258'Failed to run: ' .. shellify ( current_npm_cmd ), 259{ 'Report this issue with the output of: ' , shellify ( current_npm_cmd ) } 260) 261return 262end 263 264if latest_npm ~= 'unable to parse' and vim . version . lt ( current_npm , latest_npm ) then 265local message ='Package "neovim" is out-of-date. Installed: ' 266.. current_npm : gsub ( '%\n$' , '' ) 267.. ', latest: ' 268.. latest_npm : gsub ( '%\n$' , '' ) 269 270health . warn ( message , { 271'Run in shell: npm install -g neovim' , 272'Run in shell (if you use yarn): yarn global add neovim' , 273'Run in shell (if you use pnpm): pnpm install -g neovim' , 274'Run in shell (if you use bun): bun install -g neovim' , 275} ) 276else 277health . ok ( 'Latest "neovim" npm/yarn/pnpm/bun package is installed: ' .. current_npm ) 278end 279end 280 281local function perl () 282health . start ( 'Perl provider (optional)' ) 283 284check_loaded_var ( 'loaded_perl_provider' ) 285 286if provider_disabled ( 'perl' ) then 287return 288end 289 290local perl_exec , perl_warnings =vim . provider . perl . detect () 291 292if not perl_exec then 293health . warn ( assert ( perl_warnings ), { 294'See :help provider-perl for more information.' , 295'You can disable this provider (and warning) by adding `let g:loaded_perl_provider = 0` to your init.vim' , 296} ) 297health . warn ( 'No usable perl executable found' ) 298return 299end 300 301health . info ( 'perl executable: ' .. perl_exec ) 302 303-- we cannot use cpanm that is on the path, as it may not be for the perl 304-- set with g:perl_host_prog 305local ok =cmd_ok ( { perl_exec , '-W' , '-MApp::cpanminus' , '-e' , '' } ) 306if not ok then 307return { perl_exec , '"App::cpanminus" module is not installed' } 308end 309 310local latest_cpan_cmd ={ 311perl_exec , 312'-MApp::cpanminus::fatscript' , 313'-e' , 314'my $app = App::cpanminus::script->new; $app->parse_options ("--info", "-q", "Neovim::Ext"); exit $app->doit' , 315} 316local latest_cpan 317ok , latest_cpan =cmd_ok ( latest_cpan_cmd ) 318if not ok or latest_cpan : find ( '^%s*$' ) then 319health . error ( 320'Failed to run: ' .. shellify ( latest_cpan_cmd ), 321{ "Make sure you're connected to the internet." , 'Are you behind a firewall or proxy?' } 322) 323return 324elseif latest_cpan [ 1 ] == '!' then 325local cpanm_errs =vim . split ( latest_cpan , '!' ) 326if assert ( cpanm_errs [ 1 ]): find ( "Can't write to " ) then 327local advice ={} ---@type string[] 328for i =2 , # cpanm_errs do 329advice [ # advice + 1 ] =cpanm_errs [ i ] 330end 331 332health . warn ( assert ( cpanm_errs [ 1 ]), advice ) 333-- Last line is the package info 334latest_cpan =cpanm_errs [ # cpanm_errs ] 335else 336health . error ( 'Unknown warning from command: ' .. latest_cpan_cmd , cpanm_errs ) 337return 338end 339end 340latest_cpan =tostring ( vim . fn . matchstr ( latest_cpan , [[\(\.\?\d\)\+]] )) 341if latest_cpan : find ( '^%s*$' ) then 342health . error ( 'Cannot parse version number from cpanm output: ' .. latest_cpan ) 343return 344end 345 346local current_cpan_cmd ={ perl_exec , '-W' , '-MNeovim::Ext' , '-e' , 'print $Neovim::Ext::VERSION' } 347local current_cpan 348ok , current_cpan =cmd_ok ( current_cpan_cmd ) 349if not ok then 350health . error ( 351'Failed to run: ' .. shellify ( current_cpan_cmd ), 352{ 'Report this issue with the output of: ' , shellify ( current_cpan_cmd ) } 353) 354return 355end 356 357if vim . version . lt ( current_cpan , latest_cpan ) then 358local message ='Module "Neovim::Ext" is out-of-date. Installed: ' 359.. current_cpan 360.. ', latest: ' 361.. latest_cpan 362health . warn ( message , 'Run in shell: cpanm -n Neovim::Ext' ) 363else 364health . ok ( 'Latest "Neovim::Ext" cpan module is installed: ' .. current_cpan ) 365end 366end 367 368--- @param path string? 369--- @param ty string 370local function is ( path , ty ) 371if not path then 372return false 373end 374local stat =vim . uv . fs_stat ( path ) 375if not stat then 376return false 377end 378return stat . type == ty 379end 380 381-- Resolves Python executable path by invoking and checking `sys.executable`. 382--- @param invocation string? 383local function python_exepath ( invocation ) 384if invocation == '' or invocation == nil then 385return nil 386end 387local p =vim . system ( { invocation , '-c' , 'import sys; sys.stdout.write(sys.executable)' } ): wait () 388if p . code ~= 0 then 389health . warn ( assert ( p . stderr )) 390return nil 391end 392return vim . fs . normalize ( vim . trim ( assert ( p . stdout ))) 393end 394 395--- Check if pyenv is available and a valid pyenv root can be found, then return 396--- their respective paths. If either of those is invalid, return two empty 397--- strings, effectively ignoring pyenv. 398--- 399--- @return [string, string] 400local function check_for_pyenv () 401local pyenv_path =vim . fn . resolve ( vim . fn . exepath ( 'pyenv' )) 402 403if pyenv_path == '' then 404return { '' , '' } 405end 406 407health . info ( 'pyenv: Path: ' .. pyenv_path ) 408 409local pyenv_root =vim . fn . resolve ( os . getenv ( 'PYENV_ROOT' ) or '' ) 410 411if pyenv_root == '' then 412local p =vim . system ( { pyenv_path , 'root' } ): wait () 413if p . code ~= 0 then 414local message =string . format ( 415'pyenv: Failed to infer the root of pyenv by running `%s root` : %s. Ignoring pyenv for all following checks.' , 416pyenv_path , 417p . stderr 418) 419health . warn ( message ) 420return { '' , '' } 421end 422pyenv_root =vim . trim ( assert ( p . stdout )) 423health . info ( 'pyenv: $PYENV_ROOT is not set. Infer from `pyenv root`.' ) 424end 425 426if not is ( pyenv_root , 'directory' ) then 427local message =string . format ( 428'pyenv: Root does not exist: %s. Ignoring pyenv for all following checks.' , 429pyenv_root 430) 431health . warn ( message ) 432return { '' , '' } 433end 434 435health . info ( 'pyenv: Root: ' .. pyenv_root ) 436 437return { pyenv_path , pyenv_root } 438end 439 440-- Check the Python interpreter's usability. 441--- @param bin string 442local function check_bin ( bin ) 443if not is ( bin , 'file' ) and ( not iswin or not is ( bin .. '.exe' , 'file' )) then 444health . error ( '"' .. bin .. '" was not found.' ) 445return false 446elseif vim . fn . executable ( bin ) == 0 then 447health . error ( '"' .. bin .. '" is not executable.' ) 448return false 449end 450return true 451end 452 453--- Fetch the contents of a URL. 454--- 455--- @param url string 456local function download ( url ) 457local has_curl =vim . fn . executable ( 'curl' ) == 1 458if has_curl then 459local ok , out =cmd_ok ( { 'curl' , '-V' } ) 460if ok and out : find ( 'Protocols:.*https' ) then 461local content , rc =system ( { 'curl' , '-sL' , url } , { stderr =true , ignore_error =true } ) 462if rc ~= 0 then 463return 'curl error with ' .. url .. ': ' .. rc 464else 465return content 466end 467end 468elseif vim . fn . executable ( 'python' ) == 1 then 469local script =( [[ 470try: 471from urllib.request import urlopen 472except ImportError: 473from urllib2 import urlopen 474 475response = urlopen('%s') 476print(response.read().decode('utf8')) 477]] ): format ( url ) 478local out , rc =system ( { 'python' , '-c' , script } ) 479if out == '' and rc ~= 0 then 480return 'python urllib.request error: ' .. rc 481else 482return out 483end 484end 485 486local message ='missing `curl` ' 487 488if has_curl then 489message =message .. '(with HTTPS support) ' 490end 491message =message .. 'and `python`, cannot make web request' 492 493return message 494end 495 496--- Get the latest Nvim Python client (pynvim) version from PyPI. 497local function latest_pypi_version () 498local pypi_version ='unable to get pypi response' 499local pypi_response =download ( 'https://pypi.org/pypi/pynvim/json' ) 500if pypi_response ~= '' then 501local pcall_ok , output =pcall ( vim . fn . json_decode , pypi_response ) 502if not pcall_ok then 503return 'error: ' .. pypi_response 504end 505 506local pypi_data =output 507local pypi_element =pypi_data [ 'info' ] or {} 508pypi_version =pypi_element [ 'version' ] or 'unable to parse' 509end 510return pypi_version 511end 512 513--- @param s string 514local function is_bad_response ( s ) 515local lower =s : lower () 516return vim . startswith ( lower , 'unable' ) 517or vim . startswith ( lower , 'error' ) 518or vim . startswith ( lower , 'outdated' ) 519end 520 521--- Get version information using the specified interpreter. The interpreter is 522--- used directly in case breaking changes were introduced since the last time 523--- Nvim's Python client was updated. 524--- 525--- @param python string 526--- 527--- Returns: { 528--- {python executable version}, 529--- {current nvim version}, 530--- {current pypi nvim status}, 531--- {installed version status} 532--- } 533local function version_info ( python ) 534local pypi_version =latest_pypi_version () 535 536local python_version , rc =system ( { 537python , 538'-c' , 539'import sys; print(".".join(str(x) for x in sys.version_info[:3]))' , 540} ) 541 542if rc ~= 0 or python_version == '' then 543python_version ='unable to parse ' .. python .. ' response' 544end 545 546local nvim_path 547nvim_path , rc =system ( { 548python , 549'-c' , 550'import sys; sys.path = [p for p in sys.path if p != ""]; import neovim; print(neovim.__file__)' , 551} ) 552if rc ~= 0 or nvim_path == '' then 553return { python_version , 'unable to load neovim Python module' , pypi_version , nvim_path } 554end 555 556-- Assuming that multiple versions of a package are installed as 557-- `<semver>/<metapath>`, sort them on semantic version in descending order. 558--- @param metapath1 string 559--- @param metapath2 string 560local function compare ( metapath1 , metapath2 ) 561local dir1 =vim . fs . basename ( vim . fs . dirname ( vim . fs . abspath ( metapath1 ))) 562local dir2 =vim . fs . basename ( vim . fs . dirname ( vim . fs . abspath ( metapath2 ))) 563return vim . version . gt ( dir1 , dir2 ) 564end 565 566-- Try to get neovim.VERSION (added in 0.1.11dev). 567local nvim_version 568nvim_version , rc =system ( { 569python , 570'-c' , 571'from neovim import VERSION as v; print("{}.{}.{}{}".format(v.major, v.minor, v.patch, v.prerelease))' , 572} , { stderr =true , ignore_error =true } ) 573if rc ~= 0 or nvim_version == '' then 574nvim_version ='unable to find pynvim module version' 575local base =vim . fs . dirname ( nvim_path ) 576local metas =vim . fn . glob ( base .. '-*/METADATA' , true , true ) 577vim . list_extend ( metas , vim . fn . glob ( base .. '-*/PKG-INFO' , true , true )) 578vim . list_extend ( metas , vim . fn . glob ( base .. '.egg-info/PKG-INFO' , true , true )) 579table . sort ( metas , compare ) 580 581if metas and next ( metas ) ~= nil then 582for line in io . lines ( metas [ 1 ]) do 583--- @cast line string 584local version =line : match ( '^Version: (%S+)' ) 585if version then 586nvim_version =version 587break 588end 589end 590end 591end 592 593-- vim.fs.relpath does not prepend '~/' while fnamemodify does 594local nvim_path_base =vim . fn . fnamemodify ( nvim_path , [[:~:h]] ) 595local version_status ='unknown; ' .. nvim_path_base 596if not is_bad_response ( nvim_version ) and not is_bad_response ( pypi_version ) then 597if vim . version . lt ( nvim_version , pypi_version ) then 598version_status ='outdated; from ' .. nvim_path_base 599else 600version_status ='up to date' 601end 602end 603 604return { python_version , nvim_version , pypi_version , version_status } 605end 606 607local function python () 608health . start ( 'Python 3 provider (optional)' ) 609 610check_loaded_var ( 'loaded_python3_provider' ) 611 612local python_exe ='' 613local virtual_env =os . getenv ( 'VIRTUAL_ENV' ) 614local venv =virtual_env and vim . fn . resolve ( virtual_env ) or '' 615local host_prog_var ='python3_host_prog' 616local python_multiple ={} ---@type string[] 617 618if provider_disabled ( 'python3' ) then 619return 620end 621 622local pyenv_table =check_for_pyenv () 623local pyenv =pyenv_table [ 1 ] 624local pyenv_root =pyenv_table [ 2 ] 625 626if vim . g [ host_prog_var ] then 627local message =string . format ( 'Using: g:%s = "%s"' , host_prog_var , vim . g [ host_prog_var ]) 628health . info ( message ) 629end 630 631local pyname , pythonx_warnings =vim . provider . python . detect_by_module ( 'neovim' ) 632 633if not pyname then 634health . warn ( 635'No Python executable found that can `import neovim`. ' 636.. 'Using the first available executable for diagnostics.' 637) 638elseif vim . g [ host_prog_var ] then 639python_exe =pyname 640end 641 642-- No Python executable could `import neovim`, or host_prog_var was used. 643if pythonx_warnings then 644health . warn ( pythonx_warnings , { 645'See :help provider-python for more information.' , 646'You can disable this provider (and warning) by adding `let g:loaded_python3_provider = 0` to your init.vim' , 647} ) 648elseif pyname and pyname ~= '' and python_exe == '' then 649if not vim . g [ host_prog_var ] then 650local message =string . format ( 651'`g:%s` is not set. Searching for %s in the environment.' , 652host_prog_var , 653pyname 654) 655health . info ( message ) 656end 657 658if pyenv ~= '' then 659python_exe =system ( { pyenv , 'which' , pyname } , { stderr =true } ) 660if python_exe == '' then 661health . warn ( 'pyenv could not find ' .. pyname .. '.' ) 662end 663end 664 665if python_exe == '' then 666python_exe =vim . fn . exepath ( pyname ) 667 668if os . getenv ( 'PATH' ) then 669local path_sep =iswin and ';' or ':' 670local paths =vim . split ( os . getenv ( 'PATH' ) or '' , path_sep ) 671 672for _ , path in ipairs ( paths ) do 673local path_bin =vim . fs . normalize ( path .. '/' .. pyname ) 674if 675path_bin ~= vim . fs . normalize ( python_exe ) 676and vim . tbl_contains ( python_multiple , path_bin ) 677and vim . fn . executable ( path_bin ) == 1 678then 679python_multiple [ # python_multiple + 1 ] =path_bin 680end 681end 682 683if vim . tbl_count ( python_multiple ) > 0 then 684-- This is worth noting since the user may install something 685-- that changes $PATH, like homebrew. 686local message =string . format ( 687'Multiple %s executables found. Set `g:%s` to avoid surprises.' , 688pyname , 689host_prog_var 690) 691health . info ( message ) 692end 693 694if python_exe : find ( 'shims' ) then 695local message =string . format ( '`%s` appears to be a pyenv shim.' , python_exe ) 696local advice =string . format ( 697'`pyenv` is not in $PATH, your pyenv installation is broken. Set `g:%s` to avoid surprises.' , 698host_prog_var 699) 700health . warn ( message , advice ) 701end 702end 703end 704end 705 706if python_exe ~= '' and not vim . g [ host_prog_var ] then 707if 708venv == '' 709and pyenv ~= '' 710and pyenv_root ~= '' 711and vim . startswith ( vim . fn . resolve ( python_exe ), pyenv_root .. '/' ) 712then 713local advice =string . format ( 714'Create a virtualenv specifically for Nvim using pyenv, and set `g:%s`. This will avoid the need to install the pynvim module in each version/virtualenv.' , 715host_prog_var 716) 717health . warn ( 'pyenv is not set up optimally.' , advice ) 718elseif venv ~= '' then 719local venv_root =pyenv_root ~= '' and pyenv_root or vim . fs . dirname ( venv ) 720 721if vim . startswith ( vim . fn . resolve ( python_exe ), venv_root .. '/' ) then 722local advice =string . format ( 723'Create a virtualenv specifically for Nvim and use `g:%s`. This will avoid the need to install the pynvim module in each virtualenv.' , 724host_prog_var 725) 726health . warn ( 'Your virtualenv is not set up optimally.' , advice ) 727end 728end 729end 730 731if pyname and python_exe == '' and pyname ~= '' then 732-- An error message should have already printed. 733health . error ( '`' .. pyname .. '` was not found.' ) 734elseif python_exe ~= '' and not check_bin ( python_exe ) then 735python_exe ='' 736end 737 738-- Diagnostic output 739health . info ( 'Executable: ' .. ( python_exe == '' and 'Not found' or python_exe )) 740if vim . tbl_count ( python_multiple ) > 0 then 741for _ , path_bin in ipairs ( python_multiple ) do 742health . info ( 'Other python executable: ' .. path_bin ) 743end 744end 745 746if python_exe == '' then 747-- No Python executable can import 'neovim'. Check if any Python executable 748-- can import 'pynvim'. If so, that Python failed to import 'neovim' as 749-- well, which is most probably due to a failed pip upgrade: 750-- https://github.com/neovim/neovim/wiki/Following-HEAD#20181118 751local pynvim_exe =vim . provider . python . detect_by_module ( 'pynvim' ) 752if pynvim_exe then 753local message ='Detected pip upgrade failure: Python executable can import "pynvim" but not "neovim": ' 754.. pynvim_exe 755local advice ={ 756'Use that Python version to uninstall any "pynvim" or "neovim", e.g.:' , 757pynvim_exe .. ' -m pip uninstall pynvim neovim' , 758'Then see :help provider-python for "pynvim" installation steps.' , 759} 760health . error ( message , advice ) 761end 762else 763local version_info_table =version_info ( python_exe ) 764local pyversion =version_info_table [ 1 ] 765local current =version_info_table [ 2 ] 766local latest =version_info_table [ 3 ] 767local status =version_info_table [ 4 ] 768 769if not assert ( vim . version . range ( '~3' )): has ( pyversion ) then 770health . warn ( 'Unexpected Python version. This could lead to confusing error messages.' ) 771end 772 773health . info ( 'Python version: ' .. pyversion ) 774 775if is_bad_response ( status ) then 776health . info ( 'pynvim version: ' .. current .. ' (' .. status .. ')' ) 777else 778health . info ( 'pynvim version: ' .. current ) 779end 780 781if is_bad_response ( current ) then 782health . error ( 783'pynvim is not installed.\nError: ' .. current , 784'See :help provider-python for "pynvim" installation steps.' 785) 786end 787 788if is_bad_response ( latest ) then 789health . warn ( 'Could not contact PyPI to get latest version.' ) 790health . error ( 'HTTP request failed: ' .. latest ) 791elseif is_bad_response ( status ) then 792health . warn ( 'Latest pynvim is NOT installed: ' .. latest ) 793elseif not is_bad_response ( current ) then 794health . ok ( 'Latest pynvim is installed.' ) 795end 796end 797 798health . start ( 'Python virtualenv' ) 799if not virtual_env then 800health . ok ( 'no $VIRTUAL_ENV' ) 801return 802end 803local errors ={} ---@type string[] 804-- Keep hints as dict keys in order to discard duplicates. 805local hints ={} ---@type table<string, boolean> 806-- The virtualenv should contain some Python executables, and those 807-- executables should be first both on Nvim's $PATH and the $PATH of 808-- subshells launched from Nvim. 809local bin_dir =iswin and 'Scripts' or 'bin' 810local venv_bins =vim . fn . glob ( string . format ( '%s/%s/python*' , virtual_env , bin_dir ), true , true ) 811--- @param v string 812venv_bins =vim . tbl_filter ( function ( v ) 813-- XXX: Remove irrelevant executables found in bin/. 814return not v : match ( 'python.*%-config' ) and not v : match ( 'python%-argcomplete' ) 815end , venv_bins ) 816if vim . tbl_count ( venv_bins ) > 0 then 817for _ , venv_bin in pairs ( venv_bins ) do 818venv_bin =vim . fs . normalize ( venv_bin ) 819local py_bin_basename =vim . fs . basename ( venv_bin ) 820local nvim_py_bin =python_exepath ( vim . fn . exepath ( py_bin_basename )) 821if nvim_py_bin then 822local subshell_py_bin =python_exepath ( py_bin_basename ) 823local bintable ={ 824[ 'nvim' ] ={ 825[ 'path' ] =nvim_py_bin , 826[ 'hint' ] ='$PATH ambiguities arise if the virtualenv is not ' 827.. 'properly activated prior to launching Nvim. Close Nvim, activate the virtualenv, ' 828.. 'check that invoking Python from the command line launches the correct one, ' 829.. 'then relaunch Nvim.' , 830} , 831[ 'subshell' ] ={ 832[ 'path' ] =subshell_py_bin , 833[ 'hint' ] ='$PATH ambiguities in subshells typically are ' 834.. 'caused by your shell config overriding the $PATH previously set by the ' 835.. 'virtualenv. Either prevent them from doing so, or use this workaround: ' 836.. 'https://vi.stackexchange.com/a/34996' , 837} , 838} 839for bintype , bin in pairs ( bintable ) do 840if not bin . path or vim . fn . resolve ( venv_bin ) ~= vim . fn . resolve ( bin . path ) then 841local type_of_path =bintype == 'subshell' and '$PATH' or '$PATH in subshell' 842errors [ # errors + 1 ] =type_of_path 843.. ' yields this ' 844.. py_bin_basename 845.. ' executable: ' 846.. tostring ( bin . path ) 847hints [ bin [ 'hint' ]] =true 848end 849end 850end 851end 852else 853errors [ # errors + 1 ] ='no Python executables found in the virtualenv ' 854.. bin_dir 855.. ' directory.' 856end 857 858local msg ='$VIRTUAL_ENV is set to: ' .. virtual_env 859if vim . tbl_count ( errors ) > 0 then 860if vim . tbl_count ( venv_bins ) > 0 then 861msg =string . format ( 862'%s\nAnd its %s directory contains: %s' , 863msg , 864bin_dir , 865table . concat ( 866--- @param v string 867vim . tbl_map ( function ( v ) 868return vim . fs . basename ( v ) 869end , venv_bins ), 870', ' 871) 872) 873end 874local conj ='\nBut ' 875local msgs ={} --- @type string[] 876for _ , err in ipairs ( errors ) do 877msgs [ # msgs + 1 ] =msg 878msgs [ # msgs + 1 ] =conj 879msgs [ # msgs + 1 ] =err 880msgs [ # msgs + 1 ] ='\n' 881conj ='\nAnd ' 882end 883msgs [ # msgs + 1 ] ='\nSo invoking Python may lead to unexpected results.' 884health . warn ( table . concat ( msgs ), vim . tbl_keys ( hints )) 885else 886health . info ( msg ) 887health . info ( 888'Python version: ' 889.. system ( 'python -c "import platform, sys; sys.stdout.write(platform.python_version())"' ) 890) 891health . ok ( '$VIRTUAL_ENV provides :!python.' ) 892end 893end 894 895local function ruby () 896health . start ( 'Ruby provider (optional)' ) 897 898check_loaded_var ( 'loaded_ruby_provider' ) 899 900if provider_disabled ( 'ruby' ) then 901return 902end 903 904if vim . fn . executable ( 'ruby' ) == 0 or vim . fn . executable ( 'gem' ) == 0 then 905health . warn ( 906'`ruby` and `gem` must be in $PATH.' , 907'Install Ruby and verify that `ruby` and `gem` commands work.' 908) 909return 910end 911local _ , ruby_v =cli_version ( { 'ruby' , '-v' } ) 912health . info ( 'Ruby: ' .. tostring ( ruby_v )) 913 914local host , _ =vim . provider . ruby . detect () 915if ( not host ) or host : find ( '^%s*$' ) then 916health . warn ( '`neovim-ruby-host` not found.' , { 917'Run `gem install neovim` to ensure the neovim RubyGem is installed.' , 918'Run `gem environment` to ensure the gem bin directory is in $PATH.' , 919'If you are using rvm/rbenv/chruby, try "rehashing".' , 920'See :help g:ruby_host_prog for non-standard gem installations.' , 921'You can disable this provider (and warning) by adding `let g:loaded_ruby_provider = 0` to your init.vim' , 922} ) 923return 924end 925health . info ( 'Host: ' .. host ) 926 927local latest_gem_cmd ={ 'gem' , 'list' , '-ra' , '^neovim$' } 928local ok , latest_gem =cmd_ok ( latest_gem_cmd ) 929if not ok or latest_gem : find ( '^%s*$' ) then 930health . error ( 931'Failed to run: ' .. shellify ( latest_gem_cmd ), 932{ "Make sure you're connected to the internet." , 'Are you behind a firewall or proxy?' } 933) 934return 935end 936local gem_split =vim . split ( latest_gem , [[neovim (\|, \|)$]] ) 937latest_gem =gem_split [ 1 ] or 'not found' 938 939local current_gem_cmd ={ host , '--version' } 940local current_gem 941ok , current_gem =cmd_ok ( current_gem_cmd ) 942if not ok then 943health . error ( 944'Failed to run: ' .. shellify ( current_gem_cmd ), 945{ 'Report this issue with the output of: ' , shellify ( current_gem_cmd ) } 946) 947return 948end 949 950if vim . version . lt ( current_gem , latest_gem ) then 951local message ='Gem "neovim" is out-of-date. Installed: ' 952.. current_gem 953.. ', latest: ' 954.. latest_gem 955health . warn ( message , 'Run in shell: gem update neovim' ) 956else 957health . ok ( 'Latest "neovim" gem is installed: ' .. current_gem ) 958end 959end 960 961function M . check () 962clipboard () 963node () 964perl () 965python () 966ruby () 967end 968 969return M