'Visual Studio Code terminal shell vs task shell missing rvm
I use RVM to change Ruby versions in my Mac-based development environment.
Inside Visual Studio Code, when I open a regular Terminal tab, I'm dropped into a bash login shell with the -l option, per the standard default configuration, and as documented here:
// VSCode default settings
{
"terminal.integrated.shell.osx": "/bin/bash",
"terminal.integrated.shellArgs.osx": [
"-l"
]
}
The RVM command executed manually from VSCode Terminal gives me the expected ruby version for this project by default.
$ rvm list
ruby-2.0.0-p648 [ x86_64 ]
ruby-2.1.10 [ x86_64 ]
ruby-2.1.5 [ x86_64 ]
ruby-2.2.10 [ x86_64 ]
ruby-2.2.5 [ x86_64 ]
ruby-2.3.0 [ x86_64 ]
* ruby-2.3.1 [ x86_64 ]
=> ruby-2.3.7 [ x86_64 ]
# => - current
# =* - current && default
# * - default
However, when I setup a .vscode/tasks.json file to execute that same command the Ruby version is NOT the correct version, but the default one on the system. Futhermore, I'm unable to actually use rvm use to switch versions (see error message below)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Check for RVM",
"type": "shell",
"command": "rvm list && rvm use 2.3.7",
"group": {
"kind": "test",
"isDefault": true
}
}
}
}
Output of task execution, with error message about not having a correct login shell.
> Executing task: rvm list && rvm use 2.3.7 <
ruby-2.0.0-p648 [ x86_64 ]
ruby-2.1.10 [ x86_64 ]
ruby-2.1.5 [ x86_64 ]
ruby-2.2.10 [ x86_64 ]
ruby-2.2.5 [ x86_64 ]
ruby-2.3.0 [ x86_64 ]
=* ruby-2.3.1 [ x86_64 ]
ruby-2.3.7 [ x86_64 ]
# => - current
# =* - current && default
# * - default
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.
Terminal will be reused by tasks, press any key to close it.
I've even tried specifically adding the -l bash command option as an argument in the Task's configuration, but this does not work.
"options": {
"shell": {
"args": "-l"
}
}
My understanding, after reading through this issue is that the Terminal shell config and the Task shell config is one and the same, so is there some other underlying inconsistency between Terminal shell and Task shell that I'm missing? If not, then what is it about RVM that prevents it from working inside a Task shell?
Solution 1:[1]
RVM overrides the 'cd' command to detect the .ruby-version and .ruby-gemset files and automatically set up your environment. A newly initiated vscode terminal doesn't trigger that. It only uses the default or current settings from when vscode was started, NOT what you have defined in your .ruby* files.
So I usually run cd $PWD when starting a new terminal in vscode.
When defining a rake task in tasks.json, my command line is like so:
{
"label": "rake db:migrate",
"type": "shell",
"command": "cd $PWD; rake db:migrate",
"problemMatcher": []
},
Note the cd $PWD prepended to the command line, to hook into rvm.
It's kludgy. But it's working for me so far.
I suspect this non-awareness of rvm/.ruby-gemset/.ruby-version is also preventing vscode's automatic detection of rake tasks that the Ruby plugin is supposed to do, as mentioned here https://medium.com/hack-visual-studio-code/rake-task-auto-detection-in-vscode-ce548488755e. So any rake tasks I want to run via vscode tasks have to be defined manually by me this way.
Hope this helps.
Solution 2:[2]
I had similar issue with starting up Rails inside VS Code tasks on MacOS, with zsh as default shell.
Fix was to use --login vs -l. For example:
{
"label": "Rails Sample",
"type": "shell",
"command": "cd rails-sample && bin/rails server",
"options": {
"shell": {
"args": ["--login"]
}
},
"isBackground": true,
"problemMatcher": [],
"presentation": {
"group": "rails-sample"
}
}
When I was using -l it would show me error like:
Your Ruby version is 2.6.3, but your Gemfile specified 3.0.2
The terminal process "/bin/zsh '-c', 'cd rails-sample && bin/rails server'" terminated with exit code: 18.
The reason I tried --login was because I came across this issue, even though I have the latest version of VS Code. It could be due to setup on my machine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mshiltonj |
| Solution 2 |
