How to debug Rails running on Ruby 3.1 using VSCode and Dev Containers
Published: 2023-03-17 08:45:25

---
title: How to debug Rails running on Ruby 3.1 using VSCode and Dev Containers
published: true
description: 
tags: 
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-03-17 07:28 +0000
---

# 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](https://dev.to/konyu/how-to-use-docker-containers-for-ruby-on-rails-development-in-visual-studio-code-23np)

# 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](https://github.com/konyu/base_rails/tree/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](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg)**, 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~](https://blog.uzumaki-inc.jp/vscodedockerrailsdev-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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1wga036uuxomk0vuh55.png)



```json
{
    "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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6d087z3e4liryzdjgnq.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qh2qwu1wi0xhmrgkmqxs.png)




### 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1oulyopgqo1u65rg57e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxcxj1ocufz945wnrptk.png)

### Tips Shortcut for debug execution.

F5 executes the currently active one.

You can also use Shift+F5 to execute without stopping in the debugger.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q670d5lx21q10w4xsy2f.png)

## 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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3zojj99zud75dabr272.png)


- 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n155yxmwvk9xntlqanze.png)


### 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p18uhkc7j1mvbwbxq1sa.png)



# 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

- [https://zenn.dev/igaiga/books/rails-practice-note/viewer/ruby_rails_vscode](https://zenn.dev/igaiga/books/rails-practice-note/viewer/ruby_rails_vscode)
- [https://dev.to/st0012/setup-rubydebug-with-vscode-1b7c](https://dev.to/st0012/setup-rubydebug-with-vscode-1b7c)