Help us learn about your current experience with the documentation. Take the survey.

VS Code 调试

本文档介绍如何使用 GitLab Development Kit (GDK)Visual Studio Code (VS Code) 中设置 Rails 调试。

设置

以下示例包含 rails-webrails-background-jobs 的启动配置。

  1. 在你的 gitlab 文件夹中运行 gem install debug 来安装 debug gem。

  2. 安装 VS Code Ruby rdbg 调试器 扩展,为 VS Code 添加对 rdbg 调试器类型的支持。

  3. 如果你希望自动停止和启动 GitLab 及其相关的 Ruby Rails/Sidekiq 进程,可以在 .vscode/tasks.json 文件中的配置下添加以下 VS Code 任务:

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "start rdbg for rails-web",
          "type": "shell",
          "command": "gdk stop rails-web && GITLAB_RAILS_RACK_TIMEOUT_ENABLE_LOGGING=false PUMA_SINGLE_MODE=true rdbg --open -c bin/rails server",
          "isBackground": true,
          "problemMatcher": {
            "owner": "rails",
            "pattern": {
              "regexp": "^.*$",
            },
            "background": {
              "activeOnStart": false,
              "beginsPattern": "^(ok: down:).*$",
              "endsPattern": "^(DEBUGGER: wait for debugger connection\.\.\.)$"
            }
          }
        },
        {
          "label": "start rdbg for rails-background-jobs",
          "type": "shell",
          "command": "gdk stop rails-background-jobs && rdbg --open -c bundle exec sidekiq",
          "isBackground": true,
          "problemMatcher": {
            "owner": "sidekiq",
            "pattern": {
              "regexp": "^(DEBUGGER: wait for debugger connection\.\.\.)$"
            },
            "background": {
              "activeOnStart": false,
              "beginsPattern": "^(ok: down:).*$",
              "endsPattern": "^(DEBUGGER: wait for debugger connection\.\.\.)$"
            }
          }
        }
      ]
    }
  4. 将以下配置添加到你的 .vscode/launch.json 文件中:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "rdbg",
          "name": "Attach rails-web with rdbg",
          "request": "attach",
    
          // remove the following "preLaunchTask" if you do not wish to stop and start
          // GitLab via VS Code but manually on a separate terminal.
          "preLaunchTask": "start rdbg for rails-web"
        },
        {
          "type": "rdbg",
          "name": "Attach rails-background-jobs with rdbg",
          "request": "attach",
    
          // remove the following "preLaunchTask" if you do not wish to stop and start
          // GitLab via VS Code but manually on a separate terminal.
          "preLaunchTask": "start rdbg for rails-background-jobs"
        }
      ]
    }

VS Code Ruby 扩展可能在查找正确的 Ruby 安装和适当的 rdbg 命令时遇到问题。在这种情况下,将 "rdbgPath": "/home/user/.asdf/shims/(在 asdf 的情况下)添加到上面的启动配置中。

调试

前置条件

  • 你必须有一个正在运行的 GDK 实例。

要开始调试,请执行以下任一操作:

  • F5 键。
  • 运行 Debug: Start Debugging 命令。
  • 打开 运行和调试视图,选择其中一个启动配置文件,然后选择 播放 ( play )。