Shopify / ruby-lsp-rails

A Ruby LSP addon for Rails
https://shopify.github.io/ruby-lsp-rails/
MIT License
518 stars 21 forks source link

Failure for `sorbet-runtime` on new Rails app #336

Closed andyw8 closed 2 months ago

andyw8 commented 2 months ago

I noticed when testing on a new Rails app:

2024-04-16 13:17:57.057 [info] (rails7) Ruby LSP Rails failed to initialize server: <internal:/opt/rubies/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require': cannot load such file -- sorbet-runtime (LoadError)

I suspect this wasn't noticed previously because sorbet-runtime was already being required by something else.

vinistock commented 2 months ago

This is strange. First, sorbet-runtime is required by ruby-lsp way before we even get close to activating any addons.

Second, this is saying that requiring sorbet-runtime has failed, which can only happen if the gem is not in the $LOAD_PATH, which should be impossible given that we have a dependency on sorbet-runtime.

andyw8 commented 2 months ago

Here's more of the stacktrace:

2024-04-16 13:17:57.057 [info] (rails7) Ruby LSP Rails failed to initialize server: <internal:/opt/rubies/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require': cannot load such file -- sorbet-runtime (LoadError)
    from <internal:/opt/rubies/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /Users/andyw8/.gem/ruby/3.2.1/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
    from /Users/andyw8/.gem/ruby/3.2.1/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
    from /Users/andyw8/.gem/ruby/3.2.1/gems/ruby-lsp-rails-0.3.5/lib/ruby_lsp/ruby_lsp_rails/server.rb:4:in `<main>'

So it seems this is because in server.rb we're wrongly assuming that sorbet-runtime will be available in the main Gemfile.lock, when it may only be in .ruby-lsp/Gemfile.lock.

So either:

vinistock commented 2 months ago

Oh, I see the problem now. I'd say we include BUNDLE_GEMFILE when booting Rails runner. We can read the BUNDLE_GEMFILE from the current process and pass that to Rails runner.

andyw8 commented 2 months ago

Something like this? https://github.com/Shopify/ruby-lsp-rails/pull/340

andyw8 commented 2 months ago

FYI @vinistock :

puts "1: #{ENV["BUNDLE_GEMFILE"]}"

Bundler.with_unbundled_env do
  puts "2: #{ENV["BUNDLE_GEMFILE"]}"
end

Bundler.with_original_env do
  puts "3: #{ENV["BUNDLE_GEMFILE"]}"
end
BUNDLE_GEMFILE=.ruby-lsp/Gemfile bundle exec ruby x.rb
1: /Users/andyw8/src/github.com/andyw8/rails7/.ruby-lsp/Gemfile
2: 
3: .ruby-lsp/Gemfile
j15e commented 2 months ago

We have this problem too where we do not use Sorbet in our application and get his error when the rails lsp server starts

Any way around other than adding the Sorbet gem to our application Gemfile eheh?

Ref: I reported my error on the ruby-lsp repo at first (https://github.com/Shopify/ruby-lsp/issues/1959) you can see my whole stracktrace there

vinistock commented 2 months ago

Any way around other than adding the Sorbet gem to our application Gemfile eheh?

I wouldn't recommend doing that unless you're actually adopting Sorbet. The only thing you're currently missing out on is the column information on hover (which is the feature that depends on the runtime). The team is looking into fixing this.

andyw8 commented 2 months ago

I've updated the Bundler docs to mention the bundle check behaviour we observed while investigating this: https://github.com/rubygems/rubygems/pull/7613