cdelledonne / vim-cmake

Vim/Neovim plugin for working with CMake projects
MIT License
259 stars 21 forks source link

`Unknown function: getenv` with Vim 8.0 #99

Open AaronHutchinson opened 2 months ago

AaronHutchinson commented 2 months ago

Bug description

The libs submodule uses getenv, which was not supported until Vim 8.1.1305: https://github.com/vim/vim/commit/691ddeefb545d8488e5a495af61caba2e57b3de9

To Reproduce

Run :CMakeGenerate using a version of Vim earlier than 8.1.1305.

Expected behavior

The vim-cmake plugin code should be parsed without errors.

Behavior with minimal .vimrc

I used the following .vimrc:

set nocompatible

call plug#begin()
  Plug 'cdelledonne/vim-cmake'
call plug#end()

Running :CMakeGenerate using Vim 8.0 gives the following errors:

Error detected while processing function libs#util#FilterNews[1]..<SNR>72_UpdateVersionNumber[15]..529[1]..501:
line   10:
E117: Unknown function: getenv
E15: Invalid expression: getenv('XDG_CACHE_HOME')
Error detected while processing function libs#util#FilterNews[1]..<SNR>72_UpdateVersionNumber:
line   15:
E712: Argument of get() must be a List or Dictionary
Error detected while processing /home/ahutchinson/.vim/plugged/vim-cmake/autoload/cmake.vim:
line   17:
E714: List required
Error detected while processing function 544[6]..530[2]..529[1]..501:
line   10:
E117 Unknown function: getenv
E15: Invalid expression: getenv('XDG_CACHE_HOME')
Error detected while processing function 544:
line    6:
E712: Argument of get() must be a List or Dictionary
Error detected while processing function 544[6]..<SNR>64_SetCurrentConfig[11]..532[1]..501:
line   10:
E117 Unknown function: getenv
E15: Invalid expression: getenv('XDG_CACHE_HOME')
Error detected while processing function cmake#Generate[4]..545[32]..539[16]..534[17]..<SNR>69_CreateBuffer[3]..512[5]..504:
line   23:
E119: Not enough arguments for function: bufnr
E15 Invalid expression: bufnr()
Press ENTER or type command to continue

Additional context

It would be great if vim-cmake could support Vim 8.0. In my case, I don't have administrator privileges on the desired system in order to upgrade Vim, which means I can't use vim-cmake.

At the very least, I think the Requirements section of README.md should list a minimum required Vim version in order for the plugin to work.

cdelledonne commented 2 months ago

Hi, thanks for reporting this. At the moment I don't have access to a setup where I can test this, but it seems that indeed getenv is not part of older Vim versions.

I think we can easily create a wrapper function that uses getenv when available and otherwise emulates getenv (see :help expr-env and :help expr-env-expand).

Would you be interested in creating a PR for this? The required changes should be pretty small (only autoload/libs/system.vim uses getenv). Otherwise I'll work on this in the next few days.

AaronHutchinson commented 2 months ago

Thanks for your reply. After looking at :help expr-env I was able to confirm that I can print environment variables with, e.g., :echo expand("$HOME") even though getenv doesn't exist in my Vim version.

Would you be interested in creating a PR for this?

I've been using Vim for about 2 weeks now and am not very familiar with Vim scripting yet. I'm not sure how to write the wrapper function you mention in a way that avoids the unknown function error.

At the very least, I can definitely test any proposed solutions to confirm whether they work with Vim 8.0.

cdelledonne commented 2 months ago

I've pushed a fix to the libs submodule and updated this repository. Can you try pulling the latest version of Vim-CMake and checking if the plugin works for you?

AaronHutchinson commented 2 months ago

It looks like your patch fixed the issue with getenv. Thanks! Running :CMakeGenerate got a little further, but I still encountered some fatal errors preventing it from completing:

  1. I got the following error related to bufnr:
    Error detected while processing function cmake#Generate[4]..546[32]..540[16]..535[17]..<SNR>70_CreateBuffer[3]..513[5]..505:
    line   23:
    E119: Not enough arguments for function: bufnr
    E15 Invalid expression: bufnr()

    It looks like the buffer id was previously a required argument. I got around this error by replacing all 4 occurrences of bufnr() with bufnr('%') in libs/system.vim.

  2. After the above change I got:
    Error detected while processing function cmake#Generate[4]..546[32]..540[16]..535[17]..<SNR>70_CreateBuffer[13]..509:
    line   5:
    E521: Number required: &buflisted = 'v:false'

    I tried searching the help regarding the buflisted option but couldn't figure out what the issue was here. As a temporary hack in an attempt to proceed, I deleted this line and got around the error. I'm not sure what repercussions this had.

  3. After using the above hack I got:
    Error detected while processing function cmake#Generate[4]..546[32]..540[16]..535[17]..<SNR>70_CreateBuffer[14]..510[12]..<SNR>67_BufferExecute:
    line   4:
    E117: Unknown function: win_execute

    The win_execute function appears to have been added in Vim 8.1.1418. Another wrapper could probably be used to emulate win_execute when it's not available similar to getenv, but implementing this was beyond my knowledge. This is as far as I got.

Given the last error above, I went through all of the *.vim files of vim-cmake and vim-libs looking at the various call statements. I found that the following functions are used, but don't appear to exist in Vim 8.0: win_execute, deletebufline, appendbufline. I think these latter two would need wrappers as well.

If at any point this seems like too much work to support an older version of Vim, I completely understand and won't feel offended.

cdelledonne commented 2 months ago

Oh wow, that's more hurdles than expected :laughing:

Thanks a lot for the in-depth research and tests, these help a lot. I will need to see how to get around these limitations of old Vim, and perhaps install it myself to avoid going back and forth with you. Will keep you posted.

By the way, given you can't upgrade Vim, have you perhaps considered using a pre-compiled binary or an appimage from the Neovim release page?

AaronHutchinson commented 2 months ago

Thanks for looking into it!

have you perhaps considered using a pre-compiled binary or an appimage

Thanks for the suggestions. I will look into these options and see how they work for my circumstances.