Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run the LSP before doing bundle install #824

Closed
mateusdeap opened this issue Jul 19, 2023 · 8 comments · Fixed by #826
Closed

Unable to run the LSP before doing bundle install #824

mateusdeap opened this issue Jul 19, 2023 · 8 comments · Fixed by #826
Labels
bug Something isn't working

Comments

@mateusdeap
Copy link
Contributor

Ruby version

ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]

Code snippet

No response

Description

I'm still unsure if this is a bug, but I haven't found anything in the issues relating to this.

It's a very simple thing. I was trying to configure ruby-lsp with sublime following the configuration describede in #559. However, sublime was complaining the language server was crashing. When I tried to run the server in the command line, manually, this is what I got:

/Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/lib/ruby_lsp/requests/support/dependency_detector.rb:41:in `direct_dependency?': undefined method `dependencies' for nil:NilClass (NoMethodError)

        Bundler.locked_gems.dependencies.keys.grep(gem_pattern).any?
                           ^^^^^^^^^^^^^
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `bind_call'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/lib/ruby_lsp/requests/support/dependency_detector.rb:25:in `detected_test_library'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `bind_call'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/lib/ruby_lsp/executor.rb:16:in `initialize'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `bind_call'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/lib/ruby_lsp/server.rb:31:in `new'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/lib/ruby_lsp/server.rb:31:in `initialize'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `bind_call'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/sorbet-runtime-0.5.10921/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/exe/ruby-lsp:89:in `new'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/ruby-lsp-0.7.3/exe/ruby-lsp:89:in `<top (required)>'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/bin/ruby-lsp:25:in `load'
	from /Users/mateus/.asdf/installs/ruby/3.1.2/bin/ruby-lsp:25:in `<main>'

Since it was complaining that there was no dependencies method for nil, I assumed that Bundler.locked_gems was nil because I had not bundle installed. Indeed, running bundle install fixes the issue.

Expected output

I'm unsure, honestly. I'm not super familiar on how LSPs are supposed to work, especially a ruby LSP, but I'd kind of expect the LSP not to crash due to missing locked gems, I suppose. Or rather, due to not bundle installing, even because not all ruby projects use Gemfiles and there is the case of when you have a brand new bundler project where you have yet to generate a lockfile.

But, again, I'm not familiar on how these things are supposed to work. Any guidance would be greatly appreciated.

@mateusdeap mateusdeap added the bug Something isn't working label Jul 19, 2023
@andyw8
Copy link
Contributor

andyw8 commented Jul 19, 2023

This is expected - you would need to run bundle install. But we may be able to improve the error message to be more informative.

@vinistock
Copy link
Member

The Ruby LSP should work without a Gemfile, but if you have a Gemfile in the project, then you need to have a Gemfile.lock generated.

The Ruby LSP connects to your application's dependencies, which would be impossible if you had not generated a Gemfile.lock. One example is RuboCop: we can't require it if there's no Gemfile.lock, because bundler wouldn't know which version the application is using.

That said, I agree with Andy, we can probably display a friendlier message and not crash.

@mateusdeap
Copy link
Contributor Author

I think I could help with that, if you're currently accepting PRs. I'm just wondering: this would be merely a message to be printed out to the console if the user calls ruby-lsp on the command line? I'm asking because I know most people will be using this integrated with some editor, so I'm wondering if there's a standard way to emit these error messages

@mateusdeap
Copy link
Contributor Author

mateusdeap commented Jul 19, 2023

Also, as for why the lsp works this way, wouldn't it still be better that it work without depending on the lockfile? I ask because I think there would still be features it could provide independent of the project's dependencies, right? So it would connect to dependencies if there were a lockfile, but otherwise treat the codebase as if there were no Gemfile. Or is it that that wouldn't really work?

I guess I'm wondering if connecting to the app's dependencies is necessary, and if it is, why so. I mean, I can see how, if you have the lockfile and all, you'd want to connect to the deps, but if it doesn't I still kind of assume that it would just go on it's merry way, and connect to the dependencies once it found a lockfile

Unless the way isn't merry at all 🤣

Nevermind what I said here. After some mulling it over, I think I get it. The offer to help with the message still stands, though

@vinistock
Copy link
Member

I think the way to go for this one is to raise a more descriptive error when booting the server to warn users they forgot to bundle install for the first time.

Basically just check in the executable if Gemfile exists and Gemfile.lock doesn't and then raise with a more descriptive message.

@mateusdeap
Copy link
Contributor Author

Alright. Will give it a shot

@vinistock
Copy link
Member

Which snippet?

@mateusdeap
Copy link
Contributor Author

Damn. I wrote that, gave up on what I was going to say, accidentally submited, forgot to erase lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants