Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Cannot make Linting work with Rubocop #229

Closed
damienlethiec opened this issue Dec 4, 2017 · 20 comments
Closed

Cannot make Linting work with Rubocop #229

damienlethiec opened this issue Dec 4, 2017 · 20 comments
Labels
bug Bug reports

Comments

@damienlethiec
Copy link

Your environment

  • vscode-ruby version: 0.15.0
  • Ruby version: 2.4.1
  • VS Code version: 1.18.1
  • Operating System: Mac OS
  • Hardware (optional):

Make sure you have ruby, ruby-debug-ide and ruby-debug-basex19 installed before submitting your issue -- thank you !

Expected behavior

Rubocop linting shows the errors in my files (same errors than when running with the command line)

Actual behavior

There is not linting at all in my VSCode editor

Steps to reproduce the problem

Add the following config to your settings (I tried others but nothing works):

  "ruby.lint": {
    "rubocop": {
      "lint": true,
      "rails": true
    }
  },

Trigger a Rubocop error (for example: wrong order of gems in gemfile). Nothing happens in VSCode but rubocop in the command line find the error.

I do not seem to have any error so I do not know how I can give more info. Please let me know if I can be more useful.

Thank you

@damienlethiec
Copy link
Author

I found the logs and I have 2 errors. They seem to come from the extension but I cannot find how to solve them.

Cannot read property 'then' of undefined : TypeError: Cannot read property 'then' of undefined
	at Locate.listInFile (/Users/damienlethiec/.vscode/extensions/rebornix.ruby-0.15.0/out/locate/locate.js:68:28)
	at Object.provideDocumentSymbols (/Users/damienlethiec/.vscode/extensions/rebornix.ruby-0.15.0/out/ruby.js:225:31)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:433507
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:92953
	at new n.Class.derive._oncancel (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:72291)
	at Object.t.asWinJsPromise (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:92916)
	at e.provideDocumentSymbols (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:433461)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:445675
	at e._withAdapter (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:445314)
	at e.$provideDocumentSymbols (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:445637)
	at t.e.invoke (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:509443)
	at e._invokeHandler (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:354786)
	at e._receiveOneMessage (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:354476)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:353503
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:355422
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:85128
	at e.invoke (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:84411)
	at e.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:86133)
	at Socket.<anonymous> (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/node/extensionHostProcess.js:4:144114)
	at emitOne (events.js:96:13)
	at Socket.emit (events.js:191:7)
	at readableAddChunk (_stream_readable.js:178:18)
	at Socket.Readable.push (_stream_readable.js:136:10)
	at Pipe.onread (net.js:560:20)
e.onUnexpectedError @ shell.ts:459
(anonymous) @ shell.ts:419
e.onUnexpectedExternalError @ errors.ts:125
t.onUnexpectedExternalError @ errors.ts:146
(anonymous) @ quickOpen.ts:31
v @ winjs.base.raw.js:1209
enter @ winjs.base.raw.js:901
_run @ winjs.base.raw.js:1068
_error @ winjs.base.raw.js:1041
v @ winjs.base.raw.js:1209
enter @ winjs.base.raw.js:901
_run @ winjs.base.raw.js:1068
_error @ winjs.base.raw.js:1041
(anonymous) @ async.ts:361
done @ winjs.base.raw.js:1378
v @ winjs.base.raw.js:1224
enter @ winjs.base.raw.js:901
_run @ winjs.base.raw.js:1068
_error @ winjs.base.raw.js:1041
e.resolveErr @ lazyPromise.ts:78
e._receiveOneMessage @ rpcProtocol.ts:68
(anonymous) @ rpcProtocol.ts:32
(anonymous) @ rpcProtocol.ts:157
e.invoke @ callbackList.ts:36
e.fire @ event.ts:123
(anonymous) @ ipc.net.ts:82
emitOne @ events.js:96
emit @ events.js:191
readableAddChunk @ _stream_readable.js:178
Readable.push @ _stream_readable.js:136
onread @ net.js:560
console.ts:123 [Extension Host] Linter error: rubocop SyntaxError: Unexpected token f in JSON at position 3
	at JSON.parse (<anonymous>)
	at RuboCop.processResult (/Users/damienlethiec/.vscode/extensions/rebornix.ruby-0.15.0/out/lint/lib/linters/RuboCop.js:23:25)
	at ChildProcess.spawned.on (/Users/damienlethiec/.vscode/extensions/rebornix.ruby-0.15.0/out/lint/lib/linter.js:75:37)
	at emitTwo (events.js:106:13)
	at ChildProcess.emit (events.js:194:7)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)

@stevebutler2210
Copy link

Was this ever resolved? I'm experiencing the exact same issues myself, but on VS Code 1.19.2 and Ruby 2.5.0

@avandecreme
Copy link

It is actually working on 0.15 for me but does not on 0.17

@wingrunr21
Copy link
Collaborator

How do you all have rubocop installed?

@avandecreme
Copy link

rubocop is in my Gemfile.

$ which rubocop
~/.rbenv/shims/rubocop

I reverted back to 0.15 for now

@rpellerin
Copy link

Any updates on this? I'm affected too.

@wingrunr21
Copy link
Collaborator

I've been out of town for the past week or so and haven't been able to look. I'm also seeing the same issue.

@joseph0x01
Copy link

I am having the same issue with ruby v2.3.5 with RVM and the same setting. Tried to toggle "Developer Tools", showing

[Extension Host] Rubocop not installed

But the gem is actually installed. I open VSCode from the shell that switched to ruby 2.3.5 with RVM.

@gurgeous
Copy link
Contributor

I think this is another part of the docs that could use a refresh. This kind of error is very common. It turns out that getting VS Code and rvm/rbenv/chruby to play nicely together is a non-trivial task.

Can you try investigating/installing rubocop from within VS Code's Integrated Terminal? (View > Integrated Terminal). Try ruby -v, gem env gemdir, gem list | grep rubocop, which rubocop and then rubocop -v.

I have a theory that this might be the easiest way to diagnose and fix gem dependency issues. Personally I have a simple rbenv setup and I'm curious how things look with rvm + VS Code.

@joseph0x01
Copy link

Many thanks. Below are the outputs from Integrated Terminal

$ ruby -v
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-darwin16]
$ gem env gemdir
/Users/joseph/.rvm/gems/ruby-2.3.5
$ gem list | grep rubocop
rubocop (0.49.1)
rubocop-checkstyle_formatter (0.4.0)
rubocop-rspec (1.15.1)
$ which rubocop
/Users/joseph/.rvm/gems/ruby-2.3.5/bin/rubocop
$ rubocop -v
0.49.1

From Visual Studio Code Developer Tools

[Extension Host] Solargraph extension activated.
/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:257 [Extension Host] Rubocop not installed
/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:1325   ERR write EPIPE: Error: write EPIPE
	at exports._errnoException (util.js:1050:11)
	at WriteWrap.afterWrite [as oncomplete] (net.js:813:14)
/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:257 [Extension Host] The Solargraph gem is not available.
/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:1325   ERR write EPIPE: Error: write EPIPE
	at exports._errnoException (util.js:1050:11)
	at WriteWrap.afterWrite [as oncomplete] (net.js:813:14)
log.ts:171   ERR write EPIPE: Error: write EPIPE
	at exports._errnoException (util.js:1050:11)
	at WriteWrap.afterWrite [as oncomplete] (net.js:813:14)

@gurgeous
Copy link
Contributor

Thanks for taking the time to put that together.

Hm, that's strange. I would expect it to work if VS Code can run rubocop in the terminal. I added a new set of error messages to the rubocop activation code, but we haven't released that version of vscode-ruby yet. With 0.17.0 all you get is Rubocop not installed, unfortunately. The unreleased version is more verbose - https://github.com/rubyide/vscode-ruby/pull/297/files.

These kinds of errors are common. That's why I want to add more error messages and improve the docs. We appreciate your patience.

You can wait for the next release to get more debug info (which could take a few days), or if you are feeling ambitious you can try debugging the extension locally. See https://github.com/rubyide/vscode-ruby#contributing. Beware, though - it's easy to get bogged down with install/config issues if you're just dipping your toe into development. Especially if you don't know npm/js/etc. :)

@wingrunr21
Copy link
Collaborator

Closing for issue cleanup. Apologies if this is still an issue. We are working to improve the core extension experience.

@scottohara
Copy link

I'm not sure if this is directly related, but I was (and still are) having trouble getting the new ruby.useBundler option working, and here's a few things I've discovered so far that may be of use.

In my case, I use rvm and have bundler installed as follows:

$ which bundle
/Users/scotto/.rvm/gems/ruby-2.5.0/bin/bundle

Additionally, I have the rubocop gem vendored into my project (i.e. all my gems are installed using bundle install --path 'vendor/bundle').

Outside of vscode, to lint my project I run

cd <path to project>
bundle exec rubocop

In vscode, I get the same Rubocop not installed error; so I decided to throw a few extra console.log commands into the extension source to see what was happening.

First, I noticed the following in the console:

[Extension Host] Linter error: rubocop SyntaxError: Unexpected token b in JSON at position 0
	at JSON.parse (<anonymous>)
	at RuboCop.processResult

So, I opened up the RuboCop.processResult source here:

RuboCop.prototype.processResult = function (data) {

...and added a console.log(data); as the first line.

Turns out that the data argument (which the function expects to be the JSON results from rubocop) is in fact the following value:

[Extension Host] bundler: command not found: rubocop
Install missing gem executables with `bundle install`

Hmmm, that's odd. I know that all my gems are installed correctly, because I can run bundle exec rubocop outside vscode successfully.

Next, I wanted to see what bundle show rubocop was returning, so I opened up Linter._execLinter and just before it spawns the bundle exec rubocop -s <path to file> -f json command here...

const spawned = cp.spawn(svcPath, args, {

...I added the following:

console.log(cp.execSync("bundle show rubocop", {cmd: cmdOpts.dir}));

This gives even more clues:

Extension Host] Error: Command failed: bundle show rubocop
Ignoring executable-hooks-1.4.2 because its extensions are not built.  Try: gem pristine executable-hooks --version 1.4.2
Ignoring executable-hooks-1.3.2 because its extensions are not built.  Try: gem pristine executable-hooks --version 1.3.2
Ignoring gem-wrappers-1.4.0 because its extensions are not built.  Try: gem pristine gem-wrappers --version 1.4.0
Ignoring gem-wrappers-1.3.2 because its extensions are not built.  Try: gem pristine gem-wrappers --version 1.3.2

Again, it looks like the gem(s) aren't properly installed.

Finally, I opened up the Integrated Terminal in VSCode and ran ruby -v, and sure enough:

$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [universal.x86_64-darwin17]

$ bundle show rubocop
Your Ruby version is 2.3.3, but your Gemfile specified 2.5.0

So it looks like the problem (in my case) is that VSCode hasn't loaded rvm, and is attempting to run bundle exec rubocop using the system ruby 2.3.3; where as the project uses ruby 2.5.0 and has the gems installed/build using that version (i.e. <project root>/vendor/bundle/ruby/2.5.0/gems/rubocop).

Hope this helps.

@rpellerin
Copy link

Had the same issue as you @scottohara. It's a normal behavior. When you open up a new terminal, the environment variable $PATH gets updated with the path to your local version of Ruby loaded through rvm, as well as the path to your gems.
On the other hand, when you open VS code from outside a terminal, the $PATH variable does no contain paths to local Ruby and gems therefore VS code falls back to your system Ruby and system-installed gems, which obviously does not point to Rubocop, which I assume was installed with bundle.
The proper way to launch VS code is from a termimal. This way, everything loads fine since $PATH points to your local Ruby and gems (including Rubocop).
Let me know if something isn't clear. Hope this helps too.

@wingrunr21
Copy link
Collaborator

Yep. There's a bit of code here in the extension that attempts to rectify this problem but I'm not sure how well it works.

For instance, I'm a chruby user. In order for projects to pick up .ruby-version files I need the autoswitch functionality sourced into the shell environment. If I don't start VSCode from the command line, I inevitably get the wrong version within VSCode.

Proper support for rvm, chruby, and rbenv is on my list to look into.

@vinibrsl
Copy link
Contributor

rbenv rehash worked for me!!!

@anildigital
Copy link

@wingrunr21 any fix for this?

@stylergergely
Copy link

I am sorry but I wouldn't call this normal behavior. While I understand that rvm and co are using the PATH variable to configure the version of ruby to use, even opening VScode from the terminal doesn't solve the issue since in my case no matter what the plugin is still using the system version of ruby in the default configuration when I want to run rspec for example (OS X + zsh in my case). There is nothing normal about that. What would be normal though if I could simply control the used ruby version from the launch.json which is not the case.

Other languages (node, python, etc) could solve this problem somehow, so I suspect this is not inherently unsolvable.

@wingrunr21
Copy link
Collaborator

Please open a new issue or comment on the applicable open issue. This issue is out of date given version manager support shipped in the language server support.

There is an outstanding bug related to environment detection (#470) which I haven’t been able to nail down yet.

Also, as you seem rather confident that this is an easy problem to solve across four different version managers plus bundler plus global installs independent of how VSCode is launched, please open a PR with a fix.

@gurgeous
Copy link
Contributor

@wingrunr21 is right - this is hard, unfortunately. Too many configurations and too few people to painstakingly recreate and debug the myriad problems with PATH and ENV.

I do have a brute force workaround to suggest - replace the rubocop executable with a bash script of your own devising. Then you can configure your PATH and ENV in a way that works on your system before launching rubocop. This technique is used to great effect by https://github.com/fohte/rubocop-daemon.

If you decide to go this route, feel free to swing back and share your findings. Ideally with a link to your replacement script!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug reports
Projects
None yet
Development

No branches or pull requests