misogi / vscode-ruby-rubocop

Rubocop extension for Visual Studio Code
https://marketplace.visualstudio.com/items/misogi.ruby-rubocop
MIT License
137 stars 68 forks source link

Use project directory's rvm ruby version #89

Open ctsstc opened 5 years ago

ctsstc commented 5 years ago

The VSCode Rubocop extension should be using the current project's rvm/ruby version instead of the system default. The extension should be utilizing the same dependencies as the current project.

Not sure if #23 is related

Problem

When you are using something like rubocop-rspec it will install to the project's ruby version, but the rubocop extension will not have access to the correct dependant gems because it's using a different version that doesn't have that gem installed.

Error Codes

When the extension tries to run I see something like this pop up:

cannot load such file -- rubocop-rspec /Users/codyswartz/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:120:in `require' /Users/codyswartz/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:120:in `require' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.58.2/lib/rubocop/config_loader_resolver.rb:15:in `block in resolve_requires' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.58.2/lib/rubocop/config_loader_resolver.rb:11:in `each' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.58.2/lib/rubocop/config_loader_resolver.rb:11:in `resolve_requires' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.58.2/lib/rubocop/config_loader.rb:43:in `load_file' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.58.2/lib/rubocop/config_loader.rb:82:in `configuration_from_file' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.58.2/lib/rubocop/config_store.rb:44:in `for' /Users/codyswartz/.rvm/gems/ruby-2.4.1/gems/rubocop-0.5...
ctsstc commented 5 years ago

For now I switched my rvm over to the version the extension is running and installed the gems manually, but this isn't the greatest fix out there : \

gokuu commented 5 years ago

If you're installing rubocop in your app's bundle, maybe the PR I just opened (#91) can be of use to you too.

yelvert commented 4 years ago

This is a very hacky solution, but its working well for me. I created this file in my projects bin directory:

bin/vscode_rubocop

#! /usr/bin/env bash -l
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/.."
cd "${DIR}"
rvm . do bin/rubocop "$@"

Then set the executePath to bin/vscode_, note the missing rubocop on the end, thats because it gets automatically appended to the executePath by the extension.

This assumes you have rubocop binstubbed.

ctsstc commented 4 years ago

@gokuu seems that I'm getting similar errors when I have that enabled. The runner seems to use rmv's system default rather than the directory version.

ctsstc commented 4 years ago

I'm back searching again, I'm back on a project that's not updated.

@gokuu when I use the new Use Bundler it seems to be using the system default of rvm so then I get an error like:

Bundler::RubyVersionMismatch: Your Ruby version is 2.6.2, but your Gemfile specified 2.6.3 

If I try to explicitly point to rubocop with workspace settings via the Execute Path I get:

Setting

Error

env: ruby_executable_hooks: No such file or directory

Might be related: #50

ctsstc commented 4 years ago

Thanks @yelvert that seems to be the only solution I can get to work. I wish I could do this at a system level and not per-project, but other solutions I tried seemed to lack the context of the current environment/folder when nvm tries to determine which version to use.

Maybe the context you're utilizing could be implemented into the extension though to help it run in the proper context. I think part of it is having the login shell, and the other part is executing from the proper directory so that rvm can determine the proper version of ruby to run.

I ended up changing mine to use bundle exec instead, since we don't have rubocop bin stubbed in this project [yet].

bin/vscode_rubocop

#! /usr/bin/env bash -l
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/.."
cd "${DIR}"
rvm . do bin/bundle exec rubocop "$@"

With workspace settings: ruby.rubocop.executePath = bin/vscode_

Make sure to set the file as executable

chmod a+x bin/vscode_rubocop
limcross commented 4 years ago

I think I have found a more maintainable way to fix this among all the projects you have. On the settings set the Execute Path (ruby.rubocop.executePath) with the value rvm . do bin/bundle exec (with a space character at the end). Just that, is really easy. 👌

jeremybdk commented 3 years ago

@limcross tried your solution and I'm still running into an issue and cannot seems to figure it out, I'm using zsh, and when I use your fix I get this error: /bin/sh: rvm: command not found. I wonder if anyone has an idea,

Thanks for the help

Update: in the end a reboot of the computer fixed it.