VS Code launch.json file serves as a debugging configuration file.
It must be stored in PROJET_ROOT/.vscode/launch.json.
Though opinions vary, it is not recommended to commit such files in source control. It is specific to every developer, and commiting it would prevent each developer to configure it according to his needs.
An alternative might be to commit it as launch.json.default, although I haven't tested it yet. I resolve for now on storing it on a project by project basis, in gists such as this one, though not ideal.
- Great step by step guide on Rails debugging π
For rails and rspec multiple configurations are possible, depending on your local environment:
- low risk, bloated, not portable: follow the vscode-recipe step by step.
- It just works βοΈ
- but it has a lot of useless options (i.e. configuration key that are not strictly needed) that bloat your configuration, and make it less understandable.
- it is not portable since it has your absolute paths built-in
- lighter still not portable:
- remove all the non necessary configuration keys
- remove duplicate configurations
- it is not portable since it has your absolute paths built-in
- with binstubs
- generate a
binstubforrspec - portable since it uses the generated
binstubπ¨βπ»
- generate a
- when using rvm gemsets
- VS Code can't find the installed gems inside the gemset.
- You need to specify env variables to give the path to various folders
Follow the official VS Code Recipe
See launch-rails-rspec-lighter.json
Removed useless following configuration keys:
"args": ["server", "-p", "3000"]: it's the default optionspathToBundlerandpathToRDebugIDEsince it seems to find it without"useBundler": true: seems to be working withbundlereven without it"args": ["--pattern", "${workspaceRoot}/spec/**/*_rspec.rb"]: it's the default glob pattern for rspecCAUTION: typo
s/_rspec/_spec/
First you need to generate the binstub for rspec, which is not generated by rails by default. This will create a wrapper in ./bin/rspec that will run the bundled rspec-rails gem.
$ bundle binstubs rspec-railsThis file should be added to source control.
Then you can replace all the absolute paths in your launch.json configuration by "${workspaceRoot}/bin/rspec"
See launch-rails-rspec-binstubs.json
As a prerequisite, you need to generate binstubs (see previous section).
Then specify env variables so VS Code can locate the gems correctly
This can be done by running this shell printf:
printf "\n\"env\": {\n \"PATH\": \"$PATH\",\n \"GEM_HOME\": \"$GEM_HOME\",\n \"GEM_PATH\": \"$GEM_PATH\",\n \"RUBY_VERSION\": \"$RUBY_VERSION\"\n}\n\n"It outputs something like this
"env": {
"PATH": "/Users/nicolasrouanne/.rvm/gems/ruby-2.7.2@cevidentia-api/bin:/Users/nicolasrouanne/.rvm/gems/ruby-2.7.2@global/bin:/Users/nicolasrouanne/.rvm/rubies/ruby-2.7.2/bin:/Users/nicolasrouanne/.rvm/bin:/usr/local/bin/rubocop-daemon-wrapper:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/rubocop-daemon-wrapper",
"GEM_HOME": "/Users/nicolasrouanne/.rvm/gems/ruby-2.7.2@cevidentia-api",
"GEM_PATH": "/Users/nicolasrouanne/.rvm/gems/ruby-2.7.2@cevidentia-api:/Users/nicolasrouanne/.rvm/gems/ruby-2.7.2@global",
"RUBY_VERSION": "ruby-2.7.2"
}