williamboman / mason.nvim

Portable package manager for Neovim that runs everywhere Neovim runs. Easily install and manage LSP servers, DAP servers, linters, and formatters.
Apache License 2.0
7.95k stars 281 forks source link

Ruby LSP does not work when using different versions of Ruby #1777

Open klaustopher opened 2 months ago

klaustopher commented 2 months ago

I've searched open issues for similar requests

I've manually reviewed logs to find potential errors

I've recently downloaded the latest plugin version of mason.nvim

Problem description

First, I use rbenv to manage my ruby versions.

My system ruby is set to 3.3.4 (most current), and then in my project specific folders I am using different ruby versions using the .ruby-version file. When I install the ruby-lsp package using mason, it is installed with the system ruby version, that includes compiling the prism gem that is a natively built C extension that is being linked against the specific ruby version it was built with (so in this case 3.3.4)

When I now start neovim in one of my project folders that has a .ruby-version file, nvim tries to run ruby-lsp with the project specific ruby (i.e. 3.3.1), but since the installed version in ~/.local/share/nvim/mason/ruby-lsp was built against the system ruby (3.3.4 here). Log file shows:

~/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require': linked to incompatible ~/.rbenv/versions/3.3.4/lib/libruby.3.3.dylib - ~/.local/share/nvim/mason/packages/ruby-lsp/gems/prism-0.30.0/lib/prism/prism.bundle (LoadError)
    from ~/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
    from ~/.local/share/nvim/mason/packages/ruby-lsp/gems/prism-0.30.0/lib/prism.rb:78:in `<top (required)>'
    from ~/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
    from ~/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
    from ~/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/ruby-lsp-0.17.15/lib/ruby_lsp/internal.rb:19:in `<top (required)>'
    from ~/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
    from ~/.rbenv/versions/3.3.1/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
    from ~/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/ruby-lsp-0.17.15/exe/ruby-lsp:82:in `<top (required)>'
    from ~/.rbenv/versions/3.3.1/bin/ruby-lsp:25:in `load'
    from ~/.rbenv/versions/3.3.1/bin/ruby-lsp:25:in `<main>'
    from ~/.rbenv/versions/3.3.1/bin/ruby_executable_hooks:22:in `eval'
    from ~/.rbenv/versions/3.3.1/bin/ruby_executable_hooks:22:in `<main>'

I have currently worked around this in my neovim config, by hardcoding the paths for ruby-lsp and rubocop in my neovim-lspconfig.lua file, and skipping mason:

return {
    "neovim/nvim-lspconfig",
    opts = {
        servers = {
            ruby_lsp = {
                mason = false,
                cmd = { vim.fn.expand("~/.rbenv/shims/ruby-lsp") },
            },
            rubocop = {
                mason = false,
                cmd = { vim.fn.expand("~/.rbenv/shims/rubocop"), "--lsp" },
            }
        },
    },
}

Expected behavior

Personally, I think that mason should support installing the ruby-lsp gem for multiple ruby versions, one way how this could be done, is that instead of installing the gem into ~/.local/share/nvim/mason/packages/ruby-lsp, the current ruby version could be included in the path i.e. ~/.local/share/nvim/mason/packages/ruby-3.3.4/ruby-lsp or similar. This way, the package would be reinstalled with a freshly compiled version of prism for the currently used ruby version for each project.

Steps to reproduce

  1. Switch to a specific ruby version (i.e. 3.3.4)
  2. Open Neovim and install the ruby-lsp package using mason
  3. Close Neovim
  4. Switch to a different ruby version (i.e. 3.2.5, but every version works)
  5. Open Neovim and open a Ruby file
  6. See that LSP cannot work because the error message above is shown

Affected packages

ruby-lsp

Neovim version (>= 0.7)

NVIM v0.10.1 Build type: Release LuaJIT 2.1.1724512491

Operating system/version

Darwin mymachine.internal 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6030 arm64

Healthcheck

==============================================================================
mason: require("mason.health").check()

mason.nvim ~
- OK mason.nvim version v1.10.0
- OK PATH: prepend
- OK Providers: 
    mason.providers.registry-api
    mason.providers.client
- OK neovim version >= 0.7.0

mason.nvim [Registries] ~
- OK Registry `github.com/mason-org/mason-registry version: 2024-08-26-cute-join` is installed.

mason.nvim [Core utils] ~
- OK unzip: `UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.  Send`
- OK wget: `GNU Wget 1.24.5 built on darwin23.2.0.`
- OK curl: `curl 8.7.1 (x86_64-apple-darwin23.0) libcurl/8.7.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.61.0`
- OK gzip: `Apple gzip 430.140.2`
- OK tar: `bsdtar 3.5.3 - libarchive 3.5.3 zlib/1.2.12 liblzma/5.4.3 bz2lib/1.0.8 `
- OK bash: `GNU bash, version 5.2.32(1)-release (aarch64-apple-darwin23.4.0)`
- OK sh: `Ok`

mason.nvim [Languages] ~
- WARNING Go: not available
  - ADVICE:
    - spawn: go failed with exit code - and signal -. go is not executable
- WARNING Composer: not available
  - ADVICE:
    - spawn: composer failed with exit code - and signal -. composer is not executable
- WARNING PHP: not available
  - ADVICE:
    - spawn: php failed with exit code - and signal -. php is not executable
- WARNING cargo: not available
  - ADVICE:
    - spawn: cargo failed with exit code - and signal -. cargo is not executable
- WARNING luarocks: not available
  - ADVICE:
    - spawn: luarocks failed with exit code - and signal -. luarocks is not executable
- WARNING javac: not available
  - ADVICE:
    - spawn: javac failed with exit code 1 and signal 0. The operation couldn’t be completed. Unable to locate a Java Runtime.
      Please visit http://www.java.com for information on installing Java.

- OK node: `v18.20.1`
- WARNING julia: not available
  - ADVICE:
    - spawn: julia failed with exit code - and signal -. julia is not executable
- OK python: `Python 3.12.5`
- WARNING java: not available
  - ADVICE:
    - spawn: java failed with exit code 1 and signal 0. The operation couldn’t be completed. Unable to locate a Java Runtime.
      Please visit http://www.java.com for information on installing Java.

- OK Ruby: `ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]`
- OK RubyGem: `3.5.17`
- OK pip: `pip 24.2 from /opt/homebrew/lib/python3.12/site-packages/pip (python 3.12)`
- OK npm: `10.5.0`
- OK python venv: `Ok`

mason.nvim [GitHub] ~
- OK GitHub API rate limit. Used: 5. Remaining: 55. Limit: 60. Reset: Mon Aug 26 13:34:52 2024.
  Install and authenticate via gh-cli to increase rate limit.


### Screenshots or recordings

_No response_
alexis commented 2 months ago

I'm struggling with this issue too

fidalgo commented 4 weeks ago

I guess you need to update mason to solve this issue. :MasonUpdate solved it in my case

klaustopher commented 4 weeks ago

Was there an update to Mason that includes a fix for this?

fidalgo commented 4 weeks ago

@klaustopher I guess the update triggered a new installation of the dependencies, so in my case because I was already using a different ruby version it worked.

klaustopher commented 4 weeks ago

Ok, but manually running this every time is not really an option for me as I am often switching projects with different versions. So my fix mentioned above and manually installing ruby-lsp (or automating it with rbenv-default-gems plugin)

Chrispycode commented 3 weeks ago

I override the BUNDLE_GEMFILE env to a global gemfile to prevent this issue.

ruby_lsp = {
          cmd_env = { BUNDLE_GEMFILE = vim.fn.getenv('GLOBAL_GEMFILE')},
          cmd = { 'ruby-lsp' },
          filetypes = { 'ruby', 'eruby' },
          root_dir = function()
            return vim.loop.cwd()
          end,
        },
rossw01 commented 3 weeks ago

Same issue for me - hardcoding the path for ruby-lsp worked for now though, thanks 🎉