Vscode-ruby: Launch and attach?

Created on 12 Oct 2017  路  8Comments  路  Source: rubyide/vscode-ruby

I'm working with a desktop application which embeds an Ruby interpreter. I'm trying to configure a smooth debug workflow.

At the moment I have a task to start the application in debug mode - using the ruby-debug-ide protocol it will wait for a client to attach itself.

From VSCode I can then start a "Listen for rdebug-ide" debug session.

However, I was hoping to be able to configure something like "launch and attach" - basically in a single command start the application in debug mode and tell VSCode's debugger to start listening.

Is this something that can be done already? (Have I missed some options?) Or would that have to be added functionality to this extension?

question

Most helpful comment

So after a sleepless night @thomthom I got something working here! while I am not entirely sure if there is a better way this works, it will require using a preStartTask with a problemMatcher.

The following example will allow you to debug any active file, now while this does it remotely for local files the example can be further enhanced to work with something like docker-compose (which my version does but I wanted to simplify this example).

The key is the endsPattern that watches for the debugger to be started, you might have to look for another pattern with Sketchup.

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start-debug",
            "command": "/path/to/rdebug-ide",
            "args": [
                "-p",
                "1234",
                "-h",
                "0.0.0.0",
                "--",
                "${file}"
            ],
            "isBackground": true,
            "presentation": {
                "panel": "new"
            },
            "problemMatcher": {
                "owner": "custom",
                "pattern": {
                    "regexp": "____"
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "____",
                    "endsPattern": "Fast Debugger"
                }
            }
        }
    ]
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "Ruby",
            "request": "attach",
            "cwd": "${workspaceRoot}",
            "remoteHost": "127.0.0.1",
            "remotePort": "1234",
            "remoteWorkspaceRoot": "${workspaceRoot}",
            "preLaunchTask": "start-debug"
        }
    ]
}

All 8 comments

How about using preLaunchTask to launch the desktop application in debug mode?

The problem is that vscode-ruby doesn't appear to wait for the preLaunchTask. The application will be launched, but before it's ready the Ruby Debugger will already have attempted to connect, but failed.

RubyMine for instance solve this by having the debugger wait for a connection. I'm guessing it keeps retrying to a while.

Would this be possible with vscode-ruby?

Closing for issue cleanup. Apologies if this is still an issue. We are working to improve the core extension experience.

Does anyone have any fresh ideas on how to achieve this? something I would love to solve to improve our debug experience!

In RubyMine I'm able to set a command that launch along with the start of Remote Debug. This command will launch SketchUp in debug more, which takes a few seconds - but meanwhile RubyMine waits for a connection to be established. With this VSCode extension it appeared that it tried to make a connection only once and then give up.

I'm not familiar with exactly how the remote debug connections work in ruby-ide-debug - whether it's possible to tell it to retry for a while, or if that's something that would have to be done by the VSCode extension...

So after a sleepless night @thomthom I got something working here! while I am not entirely sure if there is a better way this works, it will require using a preStartTask with a problemMatcher.

The following example will allow you to debug any active file, now while this does it remotely for local files the example can be further enhanced to work with something like docker-compose (which my version does but I wanted to simplify this example).

The key is the endsPattern that watches for the debugger to be started, you might have to look for another pattern with Sketchup.

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start-debug",
            "command": "/path/to/rdebug-ide",
            "args": [
                "-p",
                "1234",
                "-h",
                "0.0.0.0",
                "--",
                "${file}"
            ],
            "isBackground": true,
            "presentation": {
                "panel": "new"
            },
            "problemMatcher": {
                "owner": "custom",
                "pattern": {
                    "regexp": "____"
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "____",
                    "endsPattern": "Fast Debugger"
                }
            }
        }
    ]
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "Ruby",
            "request": "attach",
            "cwd": "${workspaceRoot}",
            "remoteHost": "127.0.0.1",
            "remotePort": "1234",
            "remoteWorkspaceRoot": "${workspaceRoot}",
            "preLaunchTask": "start-debug"
        }
    ]
}

Here's my working example using docker-compose. Note the ${relativeFile} is very useful for getting the right file on the remote host.

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start-debug",
            "command": "/usr/local/bin/docker-compose",
            "args": [
                "exec",
                "app",
                "bash",
                "-c",
                // variable because of https://github.com/ruby-debug/ruby-debug-ide/pull/76
                "RUBYLIB=\"\" rdebug-ide --port 1234 --host 0.0.0.0 --dispatcher-port 26162 -- /usr/local/bin/bundle exec rspec ${relativeFile}"
            ],
            "isBackground": true,
            "presentation": {
                "panel": "new"
            },
            "problemMatcher": {
                "owner": "custom",
                "pattern": {
                    "regexp": "____"
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "____",
                    "endsPattern": "Fast Debugger"
                }
            }
        }
    ]
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker RSpec Debug",
            "type": "Ruby",
            "request": "attach",
            "remoteHost": "127.0.0.1",
            "remotePort": "1234",
            "remoteWorkspaceRoot": "/usr/src/app",
            "showDebuggerOutput": true,
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "start-debug"
        }
    ]
}

PLEASE will someone write details about this Task solution on how to start the ruby-debug-ide (for local debugging) in this way into the home page of this projects. I have spent hours wondering why RubyMine debugs easily where as I was getting nowhere with VsCode until I reverse engineered what was supposed to happen!!
The Wiki entry
https://github.com/rubyide/vscode-ruby/wiki/2.-Launching-from-VS-Code
does not work for me for local debugging and I dont know whether that is a bug (with my system) or not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webmastak picture webmastak  路  4Comments

rebornix picture rebornix  路  3Comments

rachsmithcodes picture rachsmithcodes  路  5Comments

ghost picture ghost  路  4Comments

clxy picture clxy  路  3Comments