您如何从 Visual Studio 代码 运行 和调试 Rails 上的 Ruby?
How do you run and debug Ruby on Rails from Visual Studio Code?
如何使用内置的 Visual Studio 代码 Launch/Debug 功能在 Rails 上启动 Ruby?
如何修复 Debugger terminal error: Process failed: spawn rdebug-ide ENOENT
错误?
设置和启动
- 安装VS Code Ruby plugin (点击⌘+⇧+P 在 macOS 或 ctrl+⇧+P 其他地方并键入
ext install
在提示中,然后搜索 ruby
)
- 安装一些必需的 Ruby gem
gem install ruby-debug-ide
gem install debase
- 在Visual Studio代码中添加一个launch configuration(示例配置如下)
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/bin/rails",
"env": {
"PATH": "YOUR_PATH_HERE",
"GEM_HOME": "YOUR_GEM_HOME_HERE",
"GEM_PATH": "YOUR_GEM_PATH_HERE",
"RUBY_VERSION": "YOUR_RUBY_VERSION_HERE"
},
"args": [
"server"
]
}
In some cases you might not need to specify the env
section.
In other cases you can launch VS Code using the CLI (i.e. from the terminal), which on some systems automatically sets the correct environment variables.
- 运行!
疑难解答
如果出现以下错误
Debugger terminal error: Process failed: spawn rdebug-ide ENOENT
您的环境变量 (env
) 很可能没有设置,插件找不到必要的二进制文件。
- 确保所有 gem 都已安装,如果您使用捆绑器,请尝试 运行
bundler install --binstubs
。
- 确保在您的启动配置中设置了
env
部分。 运行 以下 shell 命令生成您的 env
:
printf "\n\"env\": {\n \"PATH\": \"$PATH\",\n \"GEM_HOME\": \"$GEM_HOME\",\n \"GEM_PATH\": \"$GEM_PATH\",\n \"RUBY_VERSION\": \"$RUBY_VERSION\"\n}\n\n"
Windows
确保 path
变量的拼写(和大小写)正确,即 Path
on Windows
来源:
- https://github.com/rubyide/vscode-ruby/issues/214#issuecomment-393111908
- https://www.reddit.com/r/vscode/comments/5w1acs/getting_error_debugger_terminal_error_process/
- How to extend $PATH in launch.json in Visual Studio Code?
我花了一天的大部分时间来解决这个问题。
我最终将 launch.json 配置剥离为以下内容:
"configurations": [
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/bin/rails",
"args": [
"server"
],
"useBundler": true,
"pathToRDebugIDE": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/gems/ruby-debug-ide-0.6.1",
"pathToBundler": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/wrappers/bundle",
"showDebuggerOutput": true
}
]
首先,特别是如果您使用 RVM 并且有不同的 Gemset,请确保您的路径与正确的 Gemset 一致。
为我解决问题的是 pathToBundler。
which bundle
/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/bin/bundle
设置 binstubs 捆绑器的路径(如上所示)和从 /wrappers/ 指向的捆绑器(如下所示)似乎有些不兼容,因此将 pathToBundler 更改为:
"pathToBundler": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/wrappers/bundle",
解决了问题。
这里有一种相关的讨论:
https://github.com/rails/rails/issues/31193
其中讨论了 binstubs,但不是专门讨论 VSCode 和调试。
如果您使用 ruby 版本管理器,例如依赖 bash 垫片的 rbenv,请尝试从终端启动 VS Code。这应该允许 VS Code 获取 rbenv 设置的环境变量。或者,您可以在 launch.json
中设置环境变量,但这不是一个易于维护的解决方案。
如何使用内置的 Visual Studio 代码 Launch/Debug 功能在 Rails 上启动 Ruby?
如何修复
Debugger terminal error: Process failed: spawn rdebug-ide ENOENT
错误?
设置和启动
- 安装VS Code Ruby plugin (点击⌘+⇧+P 在 macOS 或 ctrl+⇧+P 其他地方并键入
ext install
在提示中,然后搜索ruby
) - 安装一些必需的 Ruby gem
gem install ruby-debug-ide
gem install debase
- 在Visual Studio代码中添加一个launch configuration(示例配置如下)
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/bin/rails",
"env": {
"PATH": "YOUR_PATH_HERE",
"GEM_HOME": "YOUR_GEM_HOME_HERE",
"GEM_PATH": "YOUR_GEM_PATH_HERE",
"RUBY_VERSION": "YOUR_RUBY_VERSION_HERE"
},
"args": [
"server"
]
}
In some cases you might not need to specify the
env
section. In other cases you can launch VS Code using the CLI (i.e. from the terminal), which on some systems automatically sets the correct environment variables.
- 运行!
疑难解答
如果出现以下错误
Debugger terminal error: Process failed: spawn rdebug-ide ENOENT
您的环境变量 (env
) 很可能没有设置,插件找不到必要的二进制文件。
- 确保所有 gem 都已安装,如果您使用捆绑器,请尝试 运行
bundler install --binstubs
。 - 确保在您的启动配置中设置了
env
部分。 运行 以下 shell 命令生成您的env
:
printf "\n\"env\": {\n \"PATH\": \"$PATH\",\n \"GEM_HOME\": \"$GEM_HOME\",\n \"GEM_PATH\": \"$GEM_PATH\",\n \"RUBY_VERSION\": \"$RUBY_VERSION\"\n}\n\n"
Windows
确保 path
变量的拼写(和大小写)正确,即 Path
on Windows
来源:
- https://github.com/rubyide/vscode-ruby/issues/214#issuecomment-393111908
- https://www.reddit.com/r/vscode/comments/5w1acs/getting_error_debugger_terminal_error_process/
- How to extend $PATH in launch.json in Visual Studio Code?
我花了一天的大部分时间来解决这个问题。
我最终将 launch.json 配置剥离为以下内容:
"configurations": [
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/bin/rails",
"args": [
"server"
],
"useBundler": true,
"pathToRDebugIDE": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/gems/ruby-debug-ide-0.6.1",
"pathToBundler": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/wrappers/bundle",
"showDebuggerOutput": true
}
]
首先,特别是如果您使用 RVM 并且有不同的 Gemset,请确保您的路径与正确的 Gemset 一致。
为我解决问题的是 pathToBundler。
which bundle
/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/bin/bundle
设置 binstubs 捆绑器的路径(如上所示)和从 /wrappers/ 指向的捆绑器(如下所示)似乎有些不兼容,因此将 pathToBundler 更改为:
"pathToBundler": "/Users/mitch/.rvm/gems/ruby-2.3.0@gg_portal/wrappers/bundle",
解决了问题。
这里有一种相关的讨论:
https://github.com/rails/rails/issues/31193
其中讨论了 binstubs,但不是专门讨论 VSCode 和调试。
如果您使用 ruby 版本管理器,例如依赖 bash 垫片的 rbenv,请尝试从终端启动 VS Code。这应该允许 VS Code 获取 rbenv 设置的环境变量。或者,您可以在 launch.json
中设置环境变量,但这不是一个易于维护的解决方案。