Search code examples
rubyvisual-studio-coderubocop

How to auto-correct Ruby linting errors in VS Code (in 2023)


I'm using VS Code and writing Ruby code on MacOS 12.6. I have installed several extensions for linting. I can see the errors, but I cannot get any of them to provide automatic correction, even for trivial mistakes like single vs double quotes. It always says "no quick fixes available".

enter image description here

If I use the command palette to run "Format document" it will correct these kinds of mistakes. So somehow VS Code knows how to fix these problems. It just won't do it in a convenient way.

You can see in the screenshot which extensions I have installed / active:

  • Ruby by Peng Lv
  • VSCode Ruby by Stafford Bunk
  • Ruby Solargraph by Castwide
  • ruby-rubocop by misogi

I don't really care which extensions are used. I would be happy with any working configuration that provides this basic functionality.

Here is my entire settings.json

{
  "workbench.tree.indent": 16,
  "editor.formatOnSaveMode": "modifications",
  "editor.formatOnSaveTimeout": 5000,
  // "ruby.rubocop.onSave": true,
  "ruby.useBundler": true, //run non-lint commands with bundle exec
  "ruby.useLanguageServer": true, // use the internal language server (see below)
  "ruby.lint": {

    "rubocop": {
      "useBundler": true // enable rubocop via bundler
    },
    "reek": {
      "useBundler": true // enable reek via bundler
    }
  },
  "ruby.format": "rubocop", // use rubocop for formatting
  "eslint.format.enable": true,
  "eslint.options": {
    "extensions": [ ".html", ".js", ".vue", ".jsx" ]
  },
  "eslint.validate": [
    "html",
    "javascript",
    "vue"
  ],  
  "[vue]":  {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  // "[ruby]": {
  //   "editor.defaultFormatter": "misogi.ruby-rubocop",
  //   "editor.formatOnSave": true
  // },
  "[json]": {},
  "ruby.codeCompletion": "rcodetools",
  "ruby.intellisense": "rubyLocate",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "editor.tabSize": 2,
  "[ruby]": {
    "editor.defaultFormatter": "misogi.ruby-rubocop"
  
  },
  "editor.formatOnSave": true,
  "ruby.rubocop.useBundler": true,
}

Note

This question sounds similar to: vscode( vscode-ruby + rubocop ) how to auto correct on save?

But that question is old and the answers reference config settings that aren’t recognized anymore.


Solution

  • There is a revived Rubocop for Visual Studio Code extension by Loran Kloeze available on the Visual Studio Marketplace.

    This extension is a fork of the original Rubocop extension and adds a few features, such as Rubocop server support for improved performance and specifically, a feature to Implement quick fixes for Rubocop.

    Even with the original extension by m1sogi, you should have been able to use the global auto correct function. The new quick fix feature is probably more useful though...