February 4, 2020

RuboCop & rbenv & vscode

Finally figured out how to get RuboCop linting in vscode working consistently with my rbenv setup. 🎉

This is a quick one, but I'm so happy right now, I need to write this down to have it saved for future reference.

For the longest time I've failed to properly set up vscode with the ruby extension in a way that made things like linting with RuboCop work consistently.
RuboCop linting in vscode always ever worked for me when opening up vscode from a terminal (i.e. running code . from a project's directory), which was maddening.
I just couldn't figure out why it wouldn't work otherwise. It was clearly an issue with vscode not able to find the right rubocop binary, but I just couldn't fix it and didn't find a solution to make it work with my rbenv setup consistently.

Until just now. Muahahaahaa. 🎉
Customizing what command the vscode ruby extension tries to run when it needs to fire up rubocop did the trick. I tried that before, but I couldn't figure out the right setting before. Not sure why, in hindsight it's rather obvious.

Here's what I've in my settings.json now:

// ...
"ruby.useLanguageServer": true,
"ruby.lint": {
  "rubocop": {
    "command": "/Users/wr/.rbenv/shims/rubocop"
  }
},
"ruby.format": false,
// ...
snippet: vscode's settings.json

Mind the command. It tells the ruby extension to use /Users/wr/.rbenv/shims/rubocop to execute rubocop, which will do some rbenv magic to find the right rubocop executable for your current project.

/Users/wr/.rbenv is my rbenv root, obviously. You can find out where your's is by running rbenv root in your terminal.

I'm still not able to use the ruby extension's "format" feature, because it seems like you can't customize the command it runs. It's hardcoded to the default rubocop there.