Debugging, Visual Studio Code, debug gem

  1. The recent October ‘Developer Meetup’ had some brief discussion re debugging.

  2. SU 2024 does not include the standard Ruby files to use the new debug framework.

  3. After a bad day of coding unrelated to SU, I thought I’d see if I could get VSC debugging
    working.

  4. After some code changes in a plugin, it seems to be intermittently working. Oddly, the main change that helped was not using Sketchup.require, but using require if the *.rb file existed. Not.sure.

  5. I did see some ‘source file not available’ messages, so I may have something additional to configure.

So, is anyone else interested in this? I’ve got the needed files on Windows, I could package them up and throw them on GitHub. I haven’t event checked on macOS. I could build the macOS files…

For now, only SU 2024.

Below are instructions for getting Visual Studio Code (VSC) debugging breakpoints to work with Windows SU 2024.

First of all, for debugging in VSC, please install the ‘VSCode rdbg Ruby Debugger` extension, by Koichi Sasada.

I’ve placed the files needed to use Windows SketchUp 2024 with VSC debugging in a zip file.

These files are part of a normal ‘stand-alone’ Ruby, but are not included with SU. Also, the zip file contains the most recent versions, which are newer than the ones included with Ruby 3.2.

It should be installed/extracted to:

C:/Users/<user>/.gem/ruby

You may need to create the path. It’s the location where RubyGems installs gems when the --user-install option is used. On Windows, these can be accessed by both a stand-alone Ruby 3.2 and SU 2024’s Ruby.

Assuming you’ve got a folder with Ruby code that runs in SU, place a file there at .vscode/launch, with the below contents:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "rdbg",
            "name": "SU 2024 Attach with rdbg",
            "request": "attach",
            "debugPort": "40001"
        },
    ]
}

This file is read by VSC.

Then in one of your Ruby files that SU loads, place the following code:

if ENV['RUBY_DEBUG_PORT']
  gem 'debug', '>1.9.0'
  require 'debug/open_nonstop'
  puts "Started debugging"
end

Now, pick a Ruby file, and add a line binding.break, which will cause the debugger to kick in when it’s run. I placed it in a file that’s executed when a menu item is selected/clicked. Note that setting a breakpoint in the VSC GUI doesn’t seem to work.

The above takes care of the setup. Now:

  1. Open VSC, and open your folder with Ruby code. Then, click the debugger (Ctrl+Shift+D). In the upper right, you should see ‘SU 2024 Attach with rdbg’ in the list/combobox. If not, select it.
  2. From a PowerShell terminal in VSC or otherwise, run the command, which should start SU:
    $env:RUBY_DEBUG_PORT = '40001'; &'C:/Program Files/SketchUp/SketchUp 2024/SketchUp.exe'
    
  3. In VSC, click the run triangle that’s on the left of the Debug list/combobox, or hit F5. This starts the debugger.
  4. Now, if you need to perform an action to reach the place where you placed the binding.break line. Do so. You should see the debugger stop your code at that line.

With my code, the debugger also steps thru Ruby files called by the extension, so one sees the RubyGems files used for a require statement.

A few notes:

I haven’t taken that much time working on this, and this is only debugging. Editor related features are part of Ruby LSP.

I’ve been working with Ruby for a long time, and I still use NotePad++ for editing, mostly because I like the ‘Function List’. GitHub’s Web UX has something similar, but VSC does not. Also, I often use what’s called ‘puts’ debugging. I also rely on tests.

ENV['RUBY_DEBUG_PORT'] needs to be set for remote debugging to work. I also used it as a means of determining if the code was running with a desire to use VSC debugging. That’s why the command line above that sets it and starts SU.

To all,

I apologize, today has been full of interruptions. I edited the above post to fix it, but I incorrectly stated that the zip file should be placed in:

C:/<user>/.gem/ruby

It should be placed in:

C:/Users/<user>/.gem/ruby