Shopify / vscode-shopify-ruby

An opinionated and auto-configured set of extensions for Ruby development
MIT License
176 stars 13 forks source link

Ruby extension wants to set Shopify.ruby-lsp as default formatter, but then complains it can't format Ruby #443

Closed unikitty37 closed 1 year ago

unikitty37 commented 1 year ago

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")?'

image

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 the Configure… link only offers me ruby-rubocop-revived as a formatter.

image

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 :)

andyw8 commented 1 year ago

Hi @unikitty37, which version of the ruby-lsp gem is in use?

(either in Gemfile.lock or .ruby-lsp/Gemfile.lock)

andyw8 commented 1 year ago

Also can you try disabling the ruby-rubocop-revived extension.

unikitty37 commented 1 year ago

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.

andyw8 commented 1 year ago

Can you try adding this your settings, if not already present:

  "[ruby]": {
    "editor.defaultFormatter": "Shopify.ruby-lsp"
  }
vinistock commented 1 year ago

Is rubyLsp.formatter configured to anything? And in the application you're using, do you have RuboCop or Syntax Tree installed to format files?

unikitty37 commented 1 year ago

@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.

vinistock commented 1 year ago

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.

unikitty37 commented 1 year ago

Thanks — that explains it!

I've added a skeleton Gemfile (containing just the rubocop gem) to the folder and now it works 👍

bibstha commented 6 months ago

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.