I'm using RVM to manage Ruby versions where I work. We have two Rails repos, one Desktop and one Mobile. The Desktop repo uses Ruby 2.2.4 and the Mobile repo uses 2.2.2.
I know that RVM has the functionality of automatically switching the appropriate Ruby version which is specified in the Gemfile (which is specified in our case in both the Gemfiles).
Now, when I use the default terminal, and run the ruby -v
command, inside individual project root directories, it displays the appropriate versions. But when I do the same in VS terminal, it displays the default version, which is 2.2.4.
Now my main problem was that the Rails debugger was giving me the following error: Your Ruby version is 2.2.4, but your Gemfile specified 2.2.2
for the Mobile repo whenever I tried to use the VS Debugger. And I suspect that it is because of whatever I've mentioned above.
Is that the case or is it a different issue? Also provide the solution for whatever the issue is.
VSCode supports multiple languages like C, C++, Python, Ruby, etc. it includes features like debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git.
Go to the debugger view of VS Code and hit the gear icon. Choose Ruby or Ruby Debugger from the prompt window, then you'll get the sample launch config in . vscode/launch.
You can't really have rbenv and rvm coexist. With rvm, it overrides the 'gem' command, so that would make rbenv useless. If you want to use rbenv for both, you'd have to avoid using gemsets and instead use bundler to handle dependencies.
Found the solution for the version issue. Turns out that if you open VS Code through the GUI, then VS Code uses the default version of Ruby. But if you open it with the CLI by going inside the directory of the project and then typing code .
, it works with the version specified in the Gemfile.
My guess is that RVM does a Ruby version switch when you cd
into the project directory and because of which VS Code uses that switched version because the instance of VS Code was created by the same process which did the switch. Note: I don't know much about Linux processes so feel free to correct me if I'm wrong.
With rvm
you have two choices:
code .
command. This ensures that appropriate paths are set. But when you open another workspace from UI, appropriate paths will not be set.launch.json
yourself as follows:{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"program": "${workspaceRoot}/bin/rails",
"env": {
"PATH": "",
"GEM_HOME": "",
"GEM_PATH": "",
"RUBY_VERSION": "ruby-2.5.3"
},
"args": [
"server"
]
}
Both solutions are cumbersome and what I recommend is to uninstall rvm
and use rbenv
for ruby versions management instead.
With rbenv
you don't have to specify paths and you can open your worskpaces directly from UI - correct ruby versions and paths will be always set. Using rbenv
even resolved other issues with vscode I previously had with rubocop and solargraph extensions.
Interesting article for other benefits replacing rvm
with rbenv
:
https://dev.to/krtb/why-and-how-i-replaced-rvm-with-rbenv-23ad
There is a setting for the command path of the ruby interpreter that defaults to just "ruby". I'm just getting into vscode, but it may be possible to use a vscode variable interpolation to the rvm command and set the ruby interpreter value to the rvm result.
Has anyone used https://github.com/ngetahun/vscode-rvm?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With