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

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

Closed
unikitty37 opened this issue May 12, 2023 · 9 comments

Comments

@unikitty37
Copy link

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
Copy link
Contributor

andyw8 commented May 12, 2023

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

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

@andyw8
Copy link
Contributor

andyw8 commented May 12, 2023

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

@unikitty37
Copy link
Author

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
Copy link
Contributor

andyw8 commented May 23, 2023

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

  "[ruby]": {
    "editor.defaultFormatter": "Shopify.ruby-lsp"
  }

@vinistock
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

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.

@vinistock vinistock closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2023
@unikitty37
Copy link
Author

Thanks — that explains it!

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

@bibstha
Copy link

bibstha commented Apr 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants