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

Rubocop errors are not displayed in the VSCode #2992

Open
cc-harashima opened this issue Dec 19, 2024 · 4 comments
Open

Rubocop errors are not displayed in the VSCode #2992

cc-harashima opened this issue Dec 19, 2024 · 4 comments
Labels
bug Something isn't working vscode This pull request should be included in the VS Code extension's release notes

Comments

@cc-harashima
Copy link

Description

Ruby LSP Information

VS Code Version

1.96.0

Ruby LSP Extension Version

0.8.16

Ruby Version Manager

rbenv

gem Versions

rubocop 1.68.0
rubocop-rails 2.20.2

Reproduction steps

  1. Start the Ruby LSP using a VSCode
  2. Open or save a Specific Ruby file
  3. An error notification within VSCode occurs as shown in the image below. Rubocop errors and warnings in that file are not displayed.

Image

Code snippet or error message

Error running diagnostics: An internal error occurred for the Rails/UniqueValidationWithoutIndex cop. Updating to a newer version of RuboCop may solve this. For more details, run RuboCop on the command line.

Supplement

The same issue occurred with Rubocop 1.41.1 when using Ruby-LSP version 0.8.13. At that time, I upgraded Rubocop to version 1.68.0.

Constantly upgrading the Rubocop version increases the operational burden, which is challenging.

@cc-harashima cc-harashima added bug Something isn't working vscode This pull request should be included in the VS Code extension's release notes labels Dec 19, 2024
@andyw8
Copy link
Contributor

andyw8 commented Dec 19, 2024

For more details, run RuboCop on the command line.

Did you try this?

@cc-harashima
Copy link
Author

For more details, run RuboCop on the command line.
Did you try this?

Yes. The rubocop command works correctly.
Some offenses are listed, but Rails/UniqueValidationWithoutIndex is not detected.

We use bundle exec command.

bundle exec rubocop app/target_file.rb

# ~~

1 file inspected, xx offenses detected, zz offenses autocorrectable

@vinistock
Copy link
Member

Keep in mind that running RuboCop in the terminal is not representative of what's happening in the editor. When you're editing files, the Ruby LSP will constantly see files in their intermediate state as you are in the middle of typing.

When you run RuboCop in the terminal, you are likely already done editing, so in that environment RuboCop always sees the final form of the file.

These errors mean that the cop fails in some intermediate state. Maybe the implementation of the cop is making assumptions about certain parts of the code being present, which works on the command line, but will fail if you are in the middle of typing.

For example, imagine someone created a cop to verify that Rails' validate is always invoked with a method that is defined in that same model. If the cop assumes that the symbol representing the name of the method is always there, then it will certainly fail when running in the editor.

# The command line sees this. The complete version of the code
class Foo
  validate :something
end

# The LSP sees the entire progression of typing, one keypress at a time
# At a certain moment in time, no matter how fast you are typing, the LSP
# will see this code.
#
# In this case, you are typing the validate call, but haven't finished yet.
# If the cop assumes that validate will always have one argument with a symbol
# then it will crash in the editor
class Foo
  validate
end

If you can figure out under which conditions the cop crashes, then it would be nice to report it to rubocop-rails so that they can fix it.

From our side, I think we need to make the message clearer about when this notification pops up. And maybe it should be converted to a window log message, rather than a window show message.

@cc-harashima
Copy link
Author

@vinistock

Thank you for the information.
I have confirmed that even when not in the middle of typing, RuboCop runs successfully on the command line, but an error dialog appears in the editor.

The Rails/UniqueValidationWithoutIndex cop should not be executed when db/schema.rb does not exist.
https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsuniquevalidationwithoutindex

It is strange that this error is showing up on the LSP side, as db/schema.rb is not present in the current Rails project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vscode This pull request should be included in the VS Code extension's release notes
Projects
None yet
Development

No branches or pull requests

3 participants