Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.45k stars 132 forks source link

Debugger: support remote debugging by mapping local paths to server paths #1765

Open Nowaker opened 6 months ago

Nowaker commented 6 months ago

This feature is VS Code specific

Use case

The goal is to be able to debug a Ruby process that is run on a different machine than the current one.

Description

To do that, there must be a mechanism to translate server paths to local paths.

  1. Set up socket forwarding: ssh -L /tmp/ruby-lsp-debug-sockets/ruby-debug-worldmodern-0.sock:/tmp/ruby.sock user@server
  2. Start Ruby on the remote server: bundle exec rdbg --open --command --sock-path=/tmp/ruby.sock -- puma
  3. Attach a debugger (request=attach) as if it's a local server.

This works. However, no breakpoint will trip because breakpoints are set as /home/nowaker/projects/worldmodern/app/controllers/whatever_controller.rb:123 but the server sees it as /var/www/worldmodern/app/controllers/whatever_controller.rb:123.

We need a way to define path mappings, similar to how other VS Code debuggers support it (see PHP):

    {
      "name": "Rails (attach)",
      "type": "ruby_lsp",
      "request": "attach",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}",
          "remoteRoot": "/var/www/worldmodern"
        },
        {
          "localRoot": "${env:GEM_HOME}",
          "remoteRoot": "/home/ec2-user/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0"
        }
      ]
    },

Mappings for project directory and gem directory are a must-have to make it work.

NOTE: Debugging as such works. Nothing needs fixing here. I can pause (F6) and I'll see wherever Puma was processing a request. I can them step over, continue, etc no problem.

It's just the path mapping that needs to be provided to visually set up breakpoints locally, and make them trip on the server.

Implementation

vinistock commented 5 months ago

Thank you for the feature suggestion! Looks like the debug server has support for this, so it's a matter of the extension supporting the configuration and passing that to the server.

I believe that reading those configurations and passing it as a command line argument to the debug server here is probably going to be enough.