An Interest In:
Web News this Week
- March 5, 2024
- March 4, 2024
- March 3, 2024
- March 2, 2024
- March 1, 2024
- February 29, 2024
- February 28, 2024
Easy way to setup Debugger, Autocomplete, & Autoformat for Ruby on Rails project in VS Code and boost your productivity!
This article explain how configure Visual Studio Code debugger and language server for your Ruby or Ruby on Rails project. After setup you will be able to debug and navigate through Ruby code like an in RubyMine but for free :).
Motivation
When I tried to configure Ruby debugger for the first time, I read many articles and tutorials but don't found how get things done when Ruby installed through asdf / rbenv or another version manager. Problems with relative paths, terminal environments, conflicts with existed ruby and gems versions.
Lets try to fix that and build isolated environment for our project.
Requirements
Project setup
Dependencies
Add to your Gemfile:
group :development, :test do gem "ruby-debug-ide", require: false gem "debase", require: false gem 'solargraph', require: falseend
and run
$ bundle install
Ruby version
Create .ruby-version
file in root of project if it not already exists.
$ echo "3.0.2" > .ruby-version
Binstubs
Generate bundler bintubs which will be very useful later:
$ bundle binstubs bundler ruby-debug-ide solargraph
Binstub for test library (rspec for example):
$ bundle binstubs rspec-core
After all in bin
folder you will see 4 new files: rdebug-ide
, gdb_wrapper
, bundle
, rspec
, which will be used in VS Code configuration files.
Launch and Settings
Copy to .vscode/launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Start rails server", "type": "Ruby", "request": "launch", "cwd": "${workspaceRoot}", "program": "${workspaceRoot}/bin/rails", "useBundler": true, "pathToBundler": "${workspaceRoot}/bin/bundle", "showDebuggerOutput": true, "pathToRDebugIDE": "/${workspaceRoot}/bin/rdebug-ide", "args": ["s"] }, { "name": "Run tests", "type": "Ruby", "request": "launch", "cwd": "${workspaceRoot}", "program": "${workspaceRoot}/bin/rspec", "useBundler": true, "pathToBundler": "${workspaceRoot}/bin/bundle", "showDebuggerOutput": true, "pathToRDebugIDE": "/${workspaceRoot}/bin/rdebug-ide", "args": [] } ]}
Copy to settings.json
{ "solargraph.commandPath": "bin/solargraph", "solargraph.formatting": true, "editor.formatOnSave": true}
Result
We configure debugger,language server for autocompletion, format, they depends only on project binstubs and works as expected.
In the future, the config can be easily transferred to another project
Press CTRL+SHIFT+D, choose configuration, fix bugs faster!
Original Link: https://dev.to/abstractart/easy-way-to-setup-debugger-and-autocomplete-for-ruby-in-visual-studio-code-2gcc
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To