ruby/vscode-rdbg

I have to restart debug session for breakpoints to work after code is edited

rockorequin opened this issue · 10 comments

Whenever I save a file in VSCode, eg to make a code change, breakpoints no longer work until I restart the debug session.

I'm running VSCode in Windows 11 with the application in a WSL Ubuntu 22.04 container running ruby 3.1.2 (installed via rvm), and I'm using the master branch of the debug gem from https://github.com/ruby/debug.

To reproduce this, I created a new rails project with rails 7.0.3 (rails new testdebug) and then added:

config/routes.rb:

resource :things, only: :index
root "things#index"

app/controllers/things_controller.rb:


class ThingsController < ApplicationController
  def index
    j=1
  end
end

and a view, app/views/things/index.erb.

My launch.json file is:


{
  // 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": [
    {
      "type": "rdbg",
      "name": "Debug Rails",
      "request": "launch",
      "script": "${workspaceRoot}/bin/rails",
      "cwd": "${workspaceRoot}",
      "args": [ "server" ],
      "askParameters": false,
      "serverReadyAction": {
        "pattern": "listening on http://127.0.0.1:([0-9]+)",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    },
    {
      "type": "rdbg",
      "name": "Attach with rdbg",
      "request": "attach"
    }
  ]
}

Then I set a breakpoint on the line j=1 in things_controller.rb#index. When I run the debug session, it stops at this line as expected, and whenever I refresh the webpage, it also stops at this line as expected.

But as soon as I make a code change, or even just save the things_controller.rb file without making any changes, when I refresh the page the code no longer stops at the breakpoint. The breakpoint still shows as enabled in the breakpoints window as "things_controller.rb app/controllers".

Have I misconfigured something?

ko1 commented

Wow.

This is explanation what happens:

  1. load file 'app/controllers/things_controller.rb:'
  2. build bytecode for the file (=> BC1)
  3. setup breakpoint into BC1
  4. run BC1
  5. stop at the breakpoint
  6. edit the file and save
  7. load file too
  8. build bytecode for the file (=> BC2)
  9. run BC2

On this case BC2 does not have an breakpoint.

I made an issue: ruby/debug#662

ko1 commented

If we modify the file (and insert/delete the lines) the existing breakpoints points strange lines.
Maybe on this case desired solution should be reset breakpoints specified on the editor, but I'm not sure we can do it.

I'm curious how ruby-debug-ide/debase manages resetting the breakpoints after an edit? I don't need to restart the debug session in VSCode for breakpoints to persist if I use these with the VSCode Ruby Debug plugin.

ko1 commented

ruby/debug#685 may solve the issue. Please reopen if it is not solved.

Thanks, with the change, it doesn't ignore breakpoints any more, and if you run code again everything is fine.

But if you insert lines while on a breakpoint, there is an issue with mismatched line numbers.

For instance, in my ThingsController I put this code:

  def index
    j=1
    if j == 0  # Set breakpoint here
      puts 'j equals 0'
    else
      puts 'j does not equal 0'
      puts 'Something else'
    end
  end

I then set a breakpoint at "if j == 0" and run to it.

Then while I'm stopped at the breakpoint, I insert a line before the "else" and save the file, like so:

  def index
    j=1
    if j == 0  # Set breakpoint here
      puts 'j equals 0'
      k=1 # New line inserted here
    else
      puts 'j does not equal 0'
      puts 'Something else'
    end
  end

then when I press "step over" to step to the next line, the "else" line is highlighted instead of "puts 'j does not equal 0'". When I press "step over" again, the output terminal shows "j does not equal 0", ie debug has executed the line after what it highlighted. So debug thinks the line numbers are the same as before I saved it.

If I repeat the above, ie stop at the first breakpoint, insert the "k=1" line, and set a breakpoint lower down eg on the "puts 'j does not equal 0'" line, when when I select continue it doesn't stop at the second breakpoint.

I don't see how to re-open this issue, do I not have permission or have I missed something?

ko1 commented

Maybe this is another issue and known limitation.

While the code is running (in this case, index method is running) the code of index is not changed and the breakpoints points strange location by adding new lines.

So the short answer is "should not change the code at stopping on the breakpoint". It is current limitation (and not sure how to implement it).

If I misunderstood (I misunderstand frequently), could you give me a step-by-step reproducible process with a short script (without Rails is easy to try for me)?

Thanks.

I can reproduce it with the same code but not using Rails:

  1. Create a new folder.
  2. In VSCode, open the folder, go to the debug menu and create a launch.json file for rdbg.
  3. Create a new ruby file and enter the code as above.
  4. Set a breakpoint on the "if j==0" line.
  5. Run the debugger so it stops at the breakpoint.
  6. Insert the "k=1" line before else.
  7. Click the Step Over button (or F10).
  8. The code should stop at the "else" line.
  9. Do Step Over again and observe the puts line is executed.

But thinking about this further, I don't think it's reasonable to expect the debugger to match up the line numbers after the file is saved, so please ignore my comments and keep this closed. Sorry for confusing the issue.

The original issue is certainly resolved because if you change the file and re-run it, the breakpoints work as expected.

ko1 commented

Thank you for repro steps and I think I didn't misunderstand.