Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RVM not working properly in VS Code Debugger

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.

like image 444
radtemporary Avatar asked Dec 19 '18 10:12

radtemporary


People also ask

Does VS code support ruby?

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.

Can you debug ruby in VSCode?

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.

Can I use both RVM and Rbenv?

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.


3 Answers

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.

like image 80
radtemporary Avatar answered Oct 24 '22 07:10

radtemporary


With rvm you have two choices:

  1. Open workspace from your terminal with code . command. This ensures that appropriate paths are set. But when you open another workspace from UI, appropriate paths will not be set.
  2. Set paths inside your 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

like image 4
tomkra Avatar answered Oct 24 '22 09:10

tomkra


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?

like image 1
Steve Avatar answered Oct 24 '22 07:10

Steve