Vscode-ruby: "rubocop failed with code=2" error when saving a file in VSCode

Created on 12 Apr 2019  路  16Comments  路  Source: rubyide/vscode-ruby

Your environment

  • vscode-ruby version: 0.22.3
  • Ruby version: tried with 2.6.2 and 2.5.1
  • Ruby version manager (if any): rvm 1.29.7 (latest)
  • VS Code version: 1.33.1
  • Operating System: MacOS Mojave 10.14.4
  • Using language server? I believe I am. :)

Expected behavior

Rubocop lints the file and autofixes

Actual behavior

Screen Shot 2019-04-12 at 10 06 03 AM
error popup when running on 2.6.2

No linting/autofixing when running on 2.5.1 or 2.6.2

Here is my settings.json:

{
  "editor.wordWrap": "on",
  "editor.tabSize": 2,
  "git.confirmSync": false,
  "editor.detectIndentation": false,
  "editor.formatOnSave": true,
  "workbench.startupEditor": "newUntitledFile",
  // "editor.parameterHints": false,
  "git.autofetch": true,
  "window.zoomLevel": 0,
  "ruby.intellisense": "rubyLocate",
  "ruby.lint": {
    "rubocop": {
      "lint": true,
      "rails": true
    }
  },
  "ruby.useLanguageServer": true,
  "[ruby]": {
    "editor.formatOnSave": true
  },
  "ruby.format": "rubocop",
  "editor.formatOnSaveTimeout": 1500
}

When I'm in a workspace for a ruby 2.6.2 project, I get the error popup and it doesn't lint. When I'm in a workspace for a ruby 2.5.1 project, I don't get the error popup, but it still doesn't lint.

stale

Most helpful comment

I was getting this error as well "rubocop failed with code=2" when trying to format a document.

I fixed it by adding "ruby.useBundler": true, to my settings.json file.

All 16 comments

Code 2 is a RuboCop error. Is there any log output and the command that is output in the log output also works in your terminal?

This extension uses stdin for input to rubocop so you'll need to use cat file.rb | <cmd from logs> to test

Okay so I solved my error popup problem, but apparently not my autolinting.

Output tab from VSCode:

Format: executing rubocop -s /Users/kate/Projects/canonical_data_service/app/jobs/send_message_job.rb -a...
[Error - 10:52:32 AM] Request textDocument/formatting failed.
  Message: Request textDocument/formatting failed with message: 
    unable to execute rubocop -s /Users/kate/Projects/canonical_data_service/app/jobs/send_message_job.rb -a:
    Error: Failed with exit code: 2 - unknown error
  Code: -32603 

in the terminal:

$ cat app/jobs/send_message_job.rb | rubocop -s /Users/kate/Projects/canonical_data_service/app/jobs/send_message_job.rb -a
Inspecting 1 file
C

Offenses:

app/jobs/send_message_job.rb:10:74: C: [Corrected] Layout/SpaceInsideStringInterpolation: Missing space around string interpolation detected.
    error_message = "Message for ID #{ id } failed to publish to queue #{queue}. #{ e.message }"
                                                                         ^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
class SendMessageJob < ApplicationJob
  @queue = :now_internal

  def self.perform_safely(queue_name, id)
    channel = MESSAGING_SERVICE.channel
    queue = channel.queue(queue_name, durable: true)
    exchange = channel.default_exchange
    exchange.publish(id, routing_key: queue_name)
  rescue StandardError => e
    error_message = "Message for ID #{ id } failed to publish to queue #{ queue }. #{ e.message }"
    Rollbar.error(error_message)
    Rails.logger.error(error_message)
  end
end

So it seems to work in the terminal but it's still throwing the error?

Do you have autocorrect turned on in your RuboCop configuration files?

Do you have autocorrect turned on in your RuboCop configuration files?

Not sure what you mean - none of my cops specifically have auto-correct turned off in .rubocop.yml as described here: https://rubocop.readthedocs.io/en/latest/configuration/#autocorrect

When I ran the command in the terminal, cating as you requested, it shows the spacing with the brackets being autocorrected, so the -a flag that's being run is autocorrecting it. I also ran
rubocop <file> -a directly in the terminal and it autocorrected.

Ya, when you run in the terminal, do you get an exit code of 2?

RuboCop will return a code of 2 when it encounters some kind of internal error (see docs. So, I'm wondering if the extension is actually getting the same output you get in the terminal but then seeing an exit code of 2 and erroring.

Was there a resolution to this? I'm seeing the same behaviour. When I run in the terminal, it corrects the problem and exits with a code of 0. I haven't figured out how to turn on the verbose logging (fairly new to vscode) so I don't see anything in the output tab.

^ I agree. @wingrunr21 is it possible to view what command VSCode Ruby is running and its output?

This issue isn't really "rubocop failed", it's actually "when rubocop fails, I can't troubleshoot why".

Ideally, a bit of the error message could show up in the notifications, with maybe the full message in the Output panel.

@bmalehorn

You mean besides the docs that specify how to do exactly that?
https://github.com/rubyide/vscode-ruby/blob/master/docs/troubleshooting.md#viewing-logs

@wingrunr21 Ah, didn't know about that page. Still, on a failing rubocop format:

image

...Ruby: Show Language Server Output Channel doesn't seem to be defined:

image

...and Ruby: Show Output Channel show an empty page:

image

If you want I can try and make a reproduction. Specifically this repo requires bundler, so I set "ruby.useBundler": false, to get this error.

I faced the same issue, and this solution here helped me.
Rubocop: rubocop returned exit code 2

Maybe you could check it out

I faced the issue and I resolved it by installing by rubocop dependencies outside my gemset as vscode uses the default gemset.

For me, dependencies needed were rubocop-airbnb, rubocop-i18n. So I installed them from the VSCode command line.

gem install rubocop-airbnb
gem install rubocop-i18n

We saw this problem and it was nothing to do with the VS Code plugin itself; but moreso the custom Rubocop cops/extensions we had built for our project.

Our problem was that our bundle exec rubocop ... would only work when called from the root directory of our project.

If you called it from a nested folder in the project, it would error with error code 2.

Once we'd made the necessary changes to our usage of requires and File.read(...) in our custom Cops, then we could run bundle exec rubocop ... in any nested folder in the project. In doing so, the VSCode integration started working too.

Inspecting the Developer Tools window of VS Code revealed more context on why Rubocop was erroring out.

image

image

I was getting this error as well "rubocop failed with code=2" when trying to format a document.

I fixed it by adding "ruby.useBundler": true, to my settings.json file.

This issue has not had activity for 30 days. It will be automatically closed in 7 days.

I'm getting lots of errors still. I've set my bundle path, but vscode seems to be running it in a strange environment and it's not loading properly.

rubocop stderr Bundler::Dsl::DSLError: [!] There was an error parsing `Gemfile`: No such file or directory @ rb_sysopen - .ruby-version. Bundler cannot continue.

Getting this error, but it's weird, because it works in my normal zsh termainl - I read the .ruby-version file into the Gemfile.

Was this page helpful?
0 / 5 - 0 ratings