Closed unikitty37 closed 1 year ago
Hi @unikitty37, which version of the ruby-lsp
gem is in use?
(either in Gemfile.lock
or .ruby-lsp/Gemfile.lock
)
Also can you try disabling the ruby-rubocop-revived
extension.
Thanks — the installed version is ruby-lsp (0.5.1)
I've disabled ruby-rubocop-revived
and now don't get the error — but I also can't format the Ruby code; I just get "There is no formatter for 'ruby' files installed." when I try.
Can you try adding this your settings, if not already present:
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp"
}
Is rubyLsp.formatter
configured to anything? And in the application you're using, do you have RuboCop or Syntax Tree installed to format files?
@andyw8 Thanks — that was already present in the settings.
@vinistock rubyLsp.formatter
is configured to rubocop
. RuboCop is installed globally; if I save a file as /tmp/foo.rb
and run rubocop /tmp/foo.rb
, it runs. But opening that file in VSCode and running the format command still produces "There is no formatter for 'ruby' files installed."
It also comes up with "Ruby LSP formatter is set to rubocop
but RuboCop was not found in the bundle". This is odd, as this is just a .rb file in a folder — Bundler is not being used in this case.
I'm using rbenv, if that matters.
The Ruby LSP doesn't support running RuboCop from a global installation. It must be a part of your Gemfile
or else it just gets ignored.
The reason for this is consistency. If multiple developers are working on the same codebase and they're all running global installations of RuboCop, there's no guarantee that the developers aren't running different versions, which may lead to different formatting results.
Additionally, if there's a version mismatch between the global installation and CI, then linting will fail on CI despite formatting being enabled in the editor.
Thanks — that explains it!
I've added a skeleton Gemfile (containing just the rubocop gem) to the folder and now it works 👍
The Ruby LSP doesn't support running RuboCop from a global installation. It must be a part of your Gemfile or else it just gets ignored.
@vinistock can we add a documentation somewhere with this instruction. I read many docs and only this comment made it clear why formatting wasn't working despite all the trials and errors.
When I open a Ruby file, I get the error 'No configuration found for editor.defaultFormatter. Would you like to apply the suggested default ("Shopify.ruby-lsp")?'
If I click Apply, then open the command palette and run
Developer: Reload Window
I get the formatter error icon in the status bar. Clicking on it shows a message saying "Formatting — Extension 'Ruby-LSP' is configured as formatter but it cannot format 'Ruby'-files'. Clicking theConfigure…
link only offers meruby-rubocop-revived
as a formatter.Why would the extension offer to set Ruby-LSP as the default formatter if it can't format Ruby? (I'm not sure if the problem is that it's offering when it shouldn't, or if VSCode thinks Ruby-LSP can't format when it can :)