How to debug Rails running on Ruby 3.1 using VSCode and Dev Containers
@kon_yu
Posted on March 17, 2023
Overview
Docker and Docker compose are now commonly used to build development environments on developers' machines. Until now, developers have been running the docker compose command from the terminal to test and verify their work, but we propose a more convenient way to develop with VS Code using the Dev Containers extension of VS Code.
In this article, I will introduce how to use VS Code's debugging features with Dev Containers in a Rails project using the debug.gem of Ruby 3.1.
Please refer to the previous article in the link below to learn how to set up the features we will be introducing.
How to use Docker containers for Ruby on Rails development in Visual Studio Code
Purpose
To make it more convenient to develop Rails using VS Code and Docker (Containers below refers to Docker containers and containers launched with Docker Compose)
- Use VS Code's debugging feature to set breakpoints in the GUI for a debugging experience similar to RubyMine's debugger.
- Establish a debug execution method through debug.gem (rdbg) in Rails projects running on Ruby 3.1 or later.
Body
Preparation
The sample code for this article can be found at the following URL (vscode tag)
GitHub - konyu/base_rails at vscode
Prerequisites
We tested the following environment
- Mac with M2 CPU
- Docker desktop for mac: 4.16.2
- Ruby: 3.2.1
- Rails: 7.0.4
- MySQL: 8
Detailed version information can be found in the Dockerfile and docker-compose.yml.
Create an appropriate Rails project with the rails new command and create Post and Comment models with scaffold.
Please refer to the previous Blog for details on environment setup.
We have also installed VSCode rdbg Ruby Debugger, which is a necessary extension for this project. The following is a list of the extensions that are required for this project.
Let's develop Rails using Docker with VSCode ~Dev Containers~
Debug configuration using debug.gem
This section describes settings using debug.gem introduced in Ruby 3.1.
Please note that this is a work-around and there are some unclear points. (There are still some things that are unclear, such as the fact that bundle exec rails does not work well with the commands in launch.json
, but bin/rails does work.
First, create a .vscode/launch.json
file for use in the debugging part of VS Code in the right panel.
{
"version": "0.2.0",
"configurations": [
{
//Start Rails server
"name": "Debug Rails",
"type": "rdbg",
"request": "launch",
// "command": "bundle exec rails", #bundle exec rails won't stop in the debugger
"command": "bin/rails",
"script": "s",
"args": ["-b","0.0.0.0"],
},
{
// Run tests on the active rspec file
"name": "Debug Rspec with current file",
"type": "rdbg",
"request": "launch",
"command": "bundle exec rspec",
"script": "${file}",
"args": [],
// Confirm the executed command in the window. Make it easy to specify options such as execution of
"askParameters": true
}
]
}
Debug Rails startup
Select Debug Rails in Run and Debug to launch Rails and stop it at a breakpoint.
Debug Rspec
Display the file with the target test code in the forefront of the editor, then select Debug Rspec with current file
to run that test code.
Tips Shortcut for debug execution.
F5 executes the currently active one.
You can also use Shift+F5 to execute without stopping in the debugger.
Considerations
Compare Pros/Cons of starting Rails server and running Rspec in Terminal
Pros
- It allows you to temporarily enable/disable breakpoints, so if you insert a
binding.debug
line into your code, it will always stop, making it easier to investigate.- This is especially useful when starting the Rails server to investigate the cause of a bug.
- Useful in situations where you want to disable a breakpoint once inside a loop
- The GUI is intuitive because you can perform Step Over and other functions necessary for debugging, and there are also shortcuts such as F10, which is also convenient.
Cons
- In the debug console, it's a pain to navigate to variables, panels to execute methods, or to use the mouse to display the terminal.
- In fact, we have adjusted the shortcuts for VS code on the Mac to counteract this problem.
Conclusion
I proposed a method of debugging Rails using debug.gem introduced in Ruby 3.1 in a local development environment for Rails projects using Docker (Docker Compose).
I have been looking for a way to debug Rails using debug.gem, but I couldn't find it, so I was able to establish a way to do it myself.
Reference link
Posted on March 17, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024