33

Recently I installed rubocop on vscode. However, it doesn't work.Error message is below.

rubocop is not excutable 
execute path is empty! please check ruby.rubocop.executePath

How should I fix it? I searched some articles, never solved...

cf. vscode-ruby-rubocop https://github.com/misogi/vscode-ruby-rubocop

Gama11
  • 31,714
  • 9
  • 78
  • 100
ShogoTGM
  • 468
  • 1
  • 4
  • 10

6 Answers6

69

The accepted answer didn't work for me. However, I did find a comment by jdarnok on this GitHub issue that worked for me.

First, to get the user's path of the program file, I ran:

rbenv which rubocop

which gave me this result:

/Users/<your username>/.rbenv/versions/2.6.2/gemsets/Rails4.2_EnergyLink/bin/rubocop

Then I ran:

which rubocop

which gave me this result:

/Users/<your username>/.rbenv/shims/rubocop

SOLUTION

In the settings of VS Code under Ruby > Rubocop: Execute Path I pasted:

/Users/<your username>/.rbenv/shims/

Other potential solutions

This Stackoverflow post refers to a few other potential solutions, such as:

  • Replace bin in the PATH with wrappers
  • Refresh executable hooks
  • Update bundler
  • Update gems
codeinaire
  • 1,682
  • 1
  • 13
  • 26
22

tl;dr

Make sure you installed Rubocop in the first place.

VS Code Rubocop Tutorial

Install Rubocop

gem install rubocop

You can check that it works properly like this:

rubocop -v

Install VS Code extension

Search for ruby-rubocop in the marketplace and install it.

Install Rubocop VS Code marketplace

Configure Rubocop for your project

Add a .rubocop.yml file to your project's root. You can see all the configuration options and how such a file should look like in the default config file. Be aware that if there are outdated or wrong rules in the file, you will get an error and Rubocop won't work. VS Code will alert you about this:

Rubocop config errors

Community
  • 1
  • 1
totymedli
  • 29,531
  • 22
  • 131
  • 165
14

Take a look to the configuration docs.

{
  // If not specified searches for 'rubocop' executable available on PATH (default and recommended)
  "ruby.rubocop.executePath": "",
  ...
}

So, by default executePath won't be setted, because it's expecting you to have the rubocop executable within your PATH.

In a simply way, there are two things you can do, add the rubocop executable path to your PATH, or add it within the package options.

You can check for the rubocop executable directory with which rubocop (then copy and paste).

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
  • 1
    Thanks! I tried `$ which rubocop`, then `rubocop not found`, when `$ rbenv which rubocop`, then `Users/user/.rbenv/versions/2.5.1/bin/rubocop` returned. So, how should I set the path to `ruby.rubocop.executePath` ? – ShogoTGM Nov 19 '18 at 04:47
  • 2
    It would seem your shell isn't running the `rbenv` version of Ruby you have installed. What does `rbenv list` or `rbenv versions` list (I use `rvm`, don't know the commands, sorry) as opposed to `ruby -v` or `which ruby`? I would think that switching to `rbenv 2.5.1` then running `code .` from your terminal would help VScode find `rubocop`. – t56k Nov 19 '18 at 05:32
  • Sorry for replying late... Thanks to your advice! It worked! – ShogoTGM Nov 30 '18 at 08:51
  • 1
    running `code .` from terminal like @t56k mentioned worked for me as well. I am running ruby with RVM and had the similar issue on a M1 air. – F.E.A Jan 10 '22 at 13:10
  • running code . also worked for me. Thank you for it. – Vicente Matus Feb 08 '23 at 18:47
9

Short answer: Try starting VSCode a different way.

Explanation: A process inherits all the environment configuration it was started from. If rubocop is installed in your global environment, then, great no matter how you start VSCode rubocop will be available to the plug-in. On the downside, rubocop is now available to everything, most of which will never need it, and all the things that do use it need to require the same version of rubocop.

If rubocop was installed in a gemset using a ruby manager, or you have something like a dot-file manager that manipulates your environment, rubocop might not be in the PATH when you start VSCode.

I start VSCode from the terminal, always. I open a terminal and cd into my project folder. My ruby manager (rvm) and dot-file manager (direnv) automatically add and change some variables in the environment. Then I run code . to start VSCode with my project. This ensures VSCode is running in the same environment configuration as my app will run.

IAmNaN
  • 10,305
  • 3
  • 53
  • 51
1

When I ran into this issue, it was because the version my bundle wanted and the version that rubocop in vscode wanted were different.

In the vscode rubocop settings, checking `execute rubocop using bundler (ie 'bundle exec rubocop') and eliminating the execute path worked for me. Ruby > Rubocop: Use Bundler checkbox

atlash
  • 11
  • 1
0

Had the same error. Problem is that VS Code hides the detailed errors.

To see the full error and help with solving the issue:

Open Help -> Developer Tools -> Console (see this)

There you will see the actual error.

I had 2 broken cops that had to be updated and also the executable path was wrong.

Very annoying but if you open the Dev tools and check the console, you will be able to debug much better and solve whatever issue arises.

Screenshot of VS Code with Debug Console

Andrei
  • 2,282
  • 26
  • 35