ruby-lsp: Internal errors occurring for various Layout/* cops

Description

Ruby LSP Information

Ruby LSP Information

VS Code Version

1.95.1

Ruby LSP Extension Version

0.8.13

Ruby LSP Server Version

0.21.3

Ruby LSP Addons

  • Standard Ruby
  • Ruby LSP Rails

Ruby Version

3.1.4

Ruby Version Manager

asdf

Installed Extensions

Click to expand
  • atom-keybindings (3.3.0)
  • azure-repos (0.40.0)
  • cel (0.1.2)
  • circleci (2.8.4)
  • copilot (1.243.0)
  • copilot-chat (0.22.1)
  • even-better-toml (0.19.2)
  • github-vscode-theme (6.3.5)
  • gitlens (15.6.3)
  • go (0.42.1)
  • makefile-tools (0.11.13)
  • markdown-all-in-one (3.6.2)
  • remote-containers (0.388.0)
  • remote-repositories (0.42.0)
  • remotehub (0.64.0)
  • ruby-lsp (0.8.13)
  • sorbet-vscode-extension (0.3.37)
  • terraform (2.33.0)
  • vscode-buf (0.6.2)
  • vscode-docker (1.29.3)
  • vscode-graphql (0.12.1)
  • vscode-graphql-syntax (1.3.8)
  • vscode-jsonnet (0.7.2)
  • vscode-mdx (1.8.11)
  • vscode-proto3 (0.5.5)
  • vscode-pull-request-github (0.100.0)
  • vscode-tailwindcss (0.12.12)
  • vscode-yaml (1.15.0)

Ruby LSP Settings

Click to expand
Workspace
{
  "formatter": "rubocop",
  "linters": [
    "rubocop"
  ]
}
User
{
  "enableExperimentalFeatures": false,
  "enabledFeatures": {
    "codeActions": true,
    "diagnostics": true,
    "documentHighlights": true,
    "documentLink": true,
    "documentSymbols": true,
    "foldingRanges": true,
    "formatting": true,
    "hover": true,
    "inlayHint": true,
    "onTypeFormatting": true,
    "selectionRanges": true,
    "semanticHighlighting": true,
    "completion": true,
    "codeLens": true,
    "definition": true,
    "workspaceSymbol": true,
    "signatureHelp": true,
    "typeHierarchy": true,
    "references": true
  },
  "featuresConfiguration": {},
  "addonSettings": {},
  "rubyVersionManager": {
    "identifier": "auto"
  },
  "customRubyCommand": "",
  "formatter": "rubocop",
  "linters": [
    "rubocop"
  ],
  "bundleGemfile": "",
  "testTimeout": 30,
  "branch": "",
  "pullDiagnosticsOn": "both",
  "useBundlerCompose": false,
  "bypassTypechecker": false,
  "rubyExecutablePath": "",
  "indexing": {},
  "erbSupport": true,
  "useLauncher": false,
  "featureFlags": {}
}

Reproduction steps

  1. Start Ruby LSP with VSCode
  2. Open a Ruby file
  3. Change a line’s indentation so it does not share the same indentation level as the code surrounding it
  4. See an error dialogue:

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

Other Layout/* cops that come standard with StandardRB experience the same issue. Ones I’ve seen include:

  • Layout/IndentationConsistency
  • Layout/FirstArrayElementIndentation
  • Layout/IndentationWidth

I haven’t seen any other cops cause this issue. For instance, with the below code snippet, I can change the strings at the end of the file to use single quotes and they will display inline linting issues correctly and the file will automatically format/fix on save. However, if I add extra indentation to practically any line, I get diagnostic or formatting errors with no other information.

Example code

class NotClass < BasicObject
  instance_methods.grep(/^[^_]/).each { |m| undef_method m }

  def initialize(object)
    @object = object
  end

  def method_missing(name, ...)
    !@object.send(name, ...)
  end

  def respond_to_missing?(name, include_private = false)
    @object.respond_to?(name, include_private)
  end
end

class Object
  def not = NotClass.new(self)
end

puts nil.not.nil?
puts 1.not.eql?(2)
puts [1, 3, 5, 7, 9].not.any? { |i| i % 2 == 0 }
puts "hello".not.start_with?("h")

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (8 by maintainers)

Most upvoted comments

For me that’s a yes, psych was set to 5.2.0. Setting its version to 5.1.0 in the Gemfile fixes the issue for me. I’m on standard.rb 1.41.1. Can’t set rubocop to 1.68 as it creates issues with standard.rb.

Do any of you happen to have the psych gem in your lockfile, specifically at version 5.2.0? If so, this will likely be fixed by upgrading RuboCop to 1.68 which contains https://github.com/rubocop/rubocop/pull/13379. I encountered similar errors for layout cops when running with ruby 3.4-dev.

As of the past few days, my Ruby LSP keeps getting into a weird state where it spews these errors with every key stroke.

Image

I notice in my LSP logs, there’s lots of warning: already initialized constant errors. Is Ruby LSP running something multiple times? Perhaps it’s doing code reloading?

[ERROR][2024-11-12 14:50:39] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"The Gemfile's dependencies are satisfied\n"
[WARN][2024-11-12 14:50:40] .../lua/vim/lsp.lua:94	"method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer"
[ERROR][2024-11-12 14:50:40] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"Ignoring prism-1.2.0 because its extensions are not built. Try: gem pristine prism --version 1.2.0\n"
[ERROR][2024-11-12 14:50:40] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"Ignoring rbs-3.6.1 because its extensions are not built. Try: gem pristine rbs --version 3.6.1\n"
[ERROR][2024-11-12 14:50:51] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.16/lib/ruby_lsp_rails/version.rb:6: warning: already initialized constant RubyLsp::Rails::VERSION\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.26/lib/ruby_lsp_rails/version.rb:6: warning: previous definition of VERSION was here\n"
[ERROR][2024-11-12 14:50:51] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.16/lib/ruby_lsp/ruby_lsp_rails/support/associations.rb:8: warning: already initialized constant RubyLsp::Rails::Support::Associations::ALL\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.26/lib/ruby_lsp/ruby_lsp_rails/support/associations.rb:8: warning: previous definition of ALL was here\n"
[ERROR][2024-11-12 14:50:51] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.16/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:8: warning: already initialized constant RubyLsp::Rails::Support::Callbacks::MODELS\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.26/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:8: warning: previous definition of MODELS was here\n"
[ERROR][2024-11-12 14:50:51] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/nate/.local/share/nvim/mason/bin/ruby-lsp"	"stderr"	"/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.16/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:37: warning: already initialized constant RubyLsp::Rails::Support::Callbacks::CONTROLLERS\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.26/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:37: warning: previous definition of CONTROLLERS was here\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.16/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:55: warning: already initialized constant RubyLsp::Rails::Support::Callbacks::JOBS\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.26/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:55: warning: previous definition of JOBS was here\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.16/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:67: warning: already initialized constant RubyLsp::Rails::Support::Callbacks::ALL\n/Users/nate/workspace/my_app/vendor/bundle/ruby/3.2.0/gems/ruby-lsp-rails-0.3.26/lib/ruby_lsp/ruby_lsp_rails/support/callbacks.rb:67: warning: previous definition of ALL was here\n"

I’m not using Standard RB. rubocop 1.66.1, ruby-lsp 0.21.3, ruby-lsp-rails 0.3.26.

This file is indeed violating Layout/FirstHashElementIndentation (I’m in the middle of edits), but I’m not sure why it’s printing this error instead of just showing the info virtual text at the end of the line, like it usually does.

Thanks for confirming. There’s a PR to update RuboCop to the needed version for standard at https://github.com/standardrb/standard/pull/659.

Same here.

Running bundle exec rubocop through the command line results in no errors, so it doesn’t look like an issue related to the Rubocop but rather an integration between LSP and Rubocop.