ruby-lsp: Format on save is not working.

Operating System

Mac OS 13.0

Ruby version

3.2.2

Project has a bundle

  • Has bundle

Ruby version manager being used

rbenv

Description

I’m having a few issues with Ruby LSP that I can’t seem to fix.

First, the indexing is stuck at 0%. No matter how long I wait, the settings I use, I never see anything else than Ruby LSP: indexing files: 0% completed

But the main issue is the formatting that doesn’t work on save (it kinda works when I format manually, but it only fixes indentation).

I obviously checked my VSCode configuration and nothing should prevent the format on save (it’s working for other languages).

Here’s the relevant settings I use for ruby :

  "rubyLsp.rubyVersionManager": "rbenv",
  "[ruby]": {
	"editor.defaultFormatter": "Shopify.ruby-lsp",
	"editor.formatOnSave": true,
	"editor.formatOnType": true,
	"editor.tabSize": 2,
	"editor.insertSpaces": true,
	"files.trimTrailingWhitespace": true,
	"files.insertFinalNewline": true,
	"files.trimFinalNewlines": true,
	"editor.rulers": [120],
	"editor.semanticHighlighting.enabled": true
  },

I noticed I had both rvm & rbenv installed so I removed rvm and made sure rbenv was properly configured. I also made sure $SHELL was available : i’m using zsh so its value is /bin/zsh

I also removed everything ruby or rails related in my VScode extensions.

Here’s the logs from Ruby LSP :

Ruby LSP> Skipping custom bundle setup since /Users/thib/code/myproject/api/.ruby-lsp/Gemfile.lock already exists and is up to date
Ruby LSP> Running bundle install for the custom bundle. This may take a while...
The Gemfile's dependencies are satisfied
Starting Ruby LSP...

Also, in the Output tab, Window is logging the following which may be related ?

2023-10-25 10:01:00.488 [error] [Extension Host] Received response message without id: Error is: 
{
    "code": -32603,
    "message": "#<NoMethodError: undefined method `dependencies' for nil:NilClass>",
    "data": {
        "errorClass": "NoMethodError",
        "errorMessage": "undefined method `dependencies' for nil:NilClass",
        "backtrace": "~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/configuration.rb:197:in `block in initial_excluded_gems'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/configuration.rb:194:in `each'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/configuration.rb:194:in `initial_excluded_gems'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/configuration.rb:21:in `initialize'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/ruby_indexer.rb:20:in `new'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/ruby_indexer.rb:20:in `configuration'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/visitor.rb:248:in `block in collect_comments'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/visitor.rb:243:in `downto'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/visitor.rb:243:in `collect_comments'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `bind_call'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/visitor.rb:222:in `add_class_entry'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `bind_call'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/visitor.rb:25:in `visit_class_node'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `bind_call'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/node.rb:2876:in `accept'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/visitor.rb:23:in `block in visit_child_nodes'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/visitor.rb:23:in `each'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/visitor.rb:23:in `visit_child_nodes'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/node.rb:14835:in `accept'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/visitor.rb:23:in `block in visit_child_nodes'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/visitor.rb:23:in `each'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/visitor.rb:23:in `visit_child_nodes'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/prism-0.15.1/lib/prism/node.rb:12985:in `accept'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_indexer/lib/ruby_indexer/index.rb:184:in `index_single'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `bind_call'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/executor.rb:198:in `block in did_change_watched_files'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/executor.rb:184:in `each'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/executor.rb:184:in `did_change_watched_files'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `bind_call'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sorbet-runtime-0.5.11090/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/executor.rb:172:in `run'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/executor.rb:26:in `execute'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/server.rb:135:in `block (2 levels) in new_worker'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/server.rb:123:in `loop'\n~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/ruby-lsp-0.12.0/lib/ruby_lsp/server.rb:123:in `block in new_worker'"
    }
}

That’s about it, let me know if I can provide anything else 😃

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 20 (6 by maintainers)

Most upvoted comments

I found the line in my Gemfile, it is

gem "capistrano3-puma", "> 5", require: false

if i comment out that line, ruby-lsp is booting.

Yes, that was it - thank you. For no great reason we were on an earlier bundler in this container. Updating to latest worked.

Okay, I dig a bit into the rubygems code again to try to understand the root cause. I suspect this is the step that’s causing to_spec to return nil.

You said removing rubocop-rails fixes the issue. Can you share which version of rubocop-rails your project is locked to? And what is your Gemfile requirement constraints? Is it a prerelease version by any chance?

On that line I shared, rubygems takes all specs with prereleases and tries to place them at the end of the list. But it only does so if the requirement matches. If it doesn’t, then matches will be an empty array and the method will return nil.

I noticed a concurrency issue from your backtrace, which I addressed in the linked PR. I’m not 100% sure it’ll fix the issue yet, but let’s give it a try and revisit.