You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WSL中通过VS Code扩展配置Ruby on Rails调试器及解释器路径?

Fixing "Extension host terminated unexpectedly" & Configuring Ruby/Rails Debugger in WSL + VS Code

Hey there! I’ve run into this exact setup issue before, so let’s break this down into actionable steps to get your Ruby/Rails debugging working smoothly.

First: Fix the Extension Host Crash

The "Extension host terminated unexpectedly" error usually pops up when VS Code (running on Windows) tries to interact with your WSL Ruby environment without proper remote context. Here’s how to fix it:

  • Use VS Code’s WSL Remote Window: Instead of opening your WSL project directly from Windows File Explorer, do this:
    1. Click the green/blue <> icon in the bottom-left corner of VS Code.
    2. Select New WSL Window using Distro... and pick your WSL distro (e.g., Ubuntu).
    3. Once the WSL window loads, use File > Open Folder to navigate to your Rails project inside WSL (like /home/your-username/your-rails-app).
  • Update Your Ruby Extension: Uninstall any outdated Ruby extensions (like the old "Ruby" extension by Peng Lv) and install the official Ruby LSP extension (maintained by Shopify). It’s far more stable for WSL environments and plays nicely with modern Ruby tooling.

Step 1: Set the Ruby Interpreter Path

Now that you’re in the WSL remote window, setting the interpreter path is straightforward:

  1. Open VS Code settings by pressing Ctrl+, (Windows/Linux) or Cmd+, (Mac).
  2. Search for Ruby LSP: Ruby Path (if using Ruby LSP) or Ruby: Path (for older extensions).
  3. To get your exact Ruby path, run this in your WSL terminal:
    which ruby
    
    This will output something like /home/your-username/.rbenv/shims/ruby (if using rbenv) or /usr/bin/ruby (system Ruby). Paste this path into the setting field.
    • Pro tip: If you use a version manager like rbenv or rvm, make sure the path points to the shim (not the raw Ruby binary) to ensure it picks up your project’s specific Ruby version.

Step 2: Configure Rails Debugger

To debug Rails apps, we’ll use the debug gem (the modern replacement for older debug tools like ruby-debug-ide):

  1. Add the debug gem to your Gemfile:
    gem 'debug', platforms: [:mri, :mingw, :x64_mingw]
    
  2. Run bundle install in your WSL terminal to install it.
  3. Create a debug configuration file:
    • In VS Code, go to the Run and Debug tab (Ctrl+Shift+D).
    • Click create a launch.json file and select Ruby as the environment.
    • Replace the default content with this Rails-specific config:
      {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Rails Server",
            "type": "ruby",
            "request": "launch",
            "program": "${workspaceRoot}/bin/rails",
            "args": ["server"],
            "useBundler": true
          },
          {
            "name": "Rails Console",
            "type": "ruby",
            "request": "launch",
            "program": "${workspaceRoot}/bin/rails",
            "args": ["console"],
            "useBundler": true
          },
          {
            "name": "Debug Current File",
            "type": "ruby",
            "request": "launch",
            "program": "${file}",
            "useBundler": true
          }
        ]
      }
      
  4. Start debugging:
    • Set breakpoints in your Rails code (click the gutter next to line numbers).
    • From the Run and Debug tab, select Rails Server and click the green play button.
    • Your Rails server will start in debug mode, and execution will pause at your breakpoints.

Quick Troubleshooting Tips

  • If you still get extension crashes, try disabling other extensions temporarily to rule out conflicts.
  • Make sure your WSL distro is up to date: run sudo apt update && sudo apt upgrade (for Debian/Ubuntu-based distros).
  • If the debugger doesn’t hit breakpoints, double-check that the debug gem is installed and your launch.json has useBundler: true.

内容的提问来源于stack exchange,提问作者Ankit Ardeshana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:24:30