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

Spawned app instance uses ruby-lsp's bundler context instead of the app's bundler context #349

Closed alexpls closed 2 months ago

alexpls commented 2 months ago

When ruby-lsp-rails spawns the Rails instance using rails runner, it passes on the bundler context of the ruby-lsp bundle instead of the application bundle.

This leads to issues when application code references Bundler.root or BUNDLE_GEMFILE expecting to get a path to the application root, but instead gets a path to the .ruby-lsp dir.

For a minimal repro, you can try this:

rails new --minimal --edge rails_lsp_test
cd rails_lsp_test
# Access a file based on the Bundler root
echo 'File.read(Bundler.root.join("config/application.rb"))' > config/initializers/access_bundler_root.rb

Running rails s will start the application just fine, however opening the app's dir in an editor with ruby-lsp configured will lead to the ruby-lsp-rails failing to initialize the server with this error:

Ruby LSP Rails booting server
Ruby LSP Rails failed to initialize server: /Users/alex.plescan/Code/experiments/rails_lsp_test/config/initializers/access_bundler_root.rb:1:in `read': No such file or directory @ rb_sysopen - /Users/alex.plescan/Code/experiments/rails_lsp_test/.ruby-lsp/config/application.rb (Errno::ENOENT)
from /Users/alex.plescan/Code/experiments/rails_lsp_test/config/initializers/access_bundler_root.rb:1:in `<top (required)>'
...
Server dependent features will not be available

In my case, I've monkey-patched runner_client.rb to set ENV['BUNDLE_GEMFILE'] to my app's Gemfile and have verified that after that, things work fine.

If you agree this is an issue that should be addressed and I'm not doing anything silly, I can look at opening a PR implementing a proper fix.

(versions: ruby-lsp-rails 0.3.5, ruby-lsp 0.16.4)

vinistock commented 2 months ago

Thank you for the bug report! This is indeed a bug, which is reported in #336. We're looking into fixing, but thank you for offering the contribution!

I'll close this issue to keep discussions centralized in the other one.

alexpls commented 2 months ago

Thanks! I'll keep an eye on #336.