Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging rails app #1731

Closed
1 task done
stage-rl opened this issue Oct 13, 2023 · 14 comments · Fixed by Shopify/vscode-ruby-lsp#876
Closed
1 task done

Debugging rails app #1731

stage-rl opened this issue Oct 13, 2023 · 14 comments · Fixed by Shopify/vscode-ruby-lsp#876
Labels
bug Something isn't working pinned This issue or pull request is pinned and won't be marked as stale transferred This issue was transferred from vscode-ruby-lsp

Comments

@stage-rl
Copy link
Contributor

Operating System

Debian Linux

Ruby version

3.2.2

Project has a bundle

  • Has bundle

Ruby version manager being used

rbenv

Description

Hello, tried to use the debugger for rails apps in "attach mode" and gave up several times returning to vscode-rdbg

Only after seeing this comment I tried running my rails app as bundle exec rdbg --open --command --sock-path=/tmp/ruby-lsp-debug-sockets/test.sock -- bin/rails server -p 3000 and it works. This still required to manually create /tmp/ruby-lsp-debug-sockets folder. Still this is probably not the required approach.

Can you pls add info on proper procedure to README? Thanks a lot and keep up the good work

@stage-rl stage-rl added the bug Something isn't working label Oct 13, 2023
@vinistock
Copy link
Member

Thank you for the bug report! I'm puzzled as to how it worked without any manual intervention in vscode-rdbg.

The only way to attach to an existing Ruby process is if that process was started with the debugger in the first place. Do you have any rdbg configuration?

When you attach with rdbg, how do you launch the server?

@stage-rl
Copy link
Contributor Author

stage-rl commented Oct 13, 2023

With vscode-rdbg it was slightly simpler rdbg -O -n -c -- bin/rails server -p 3000

The key puzzling part for me is the --sock-path=/tmp/ruby-lsp-debug-sockets/test.sock part, for which I haven't found any mention in readme

@vinistock
Copy link
Member

The idea of the socket path is that we create those automatically for you when launching the debugger, so that you don't have to configure or select anything.

For attach, it's a bit different though. The extension has no way of knowing where the socket was created, so that it can offer as an option for you to select.

In terms of the command, I'm pretty they are equivalent, we're just using the long names for the arguments (i.e.: --open instead of -O).

The Ruby LSP debug client will default to look for sockets inside /tmp/ruby-lsp-debug-sockets.

@st0012, in retrospect that was a bit of an odd decision from our part. If you don't specify a sock-path, does Ruby debug always put the sockets in the same folder? Maybe we should change this to use whatever it does by default.

@st0012
Copy link
Member

st0012 commented Oct 16, 2023

If you don't specify a sock-path, does Ruby debug always put the sockets in the same folder?

It'd create a tmp folder and use it to host socket files, like:

/var/folders/lf/4xqm_gk10ts83_mxrhsz9qf80000gn/T/ruby-debug-sock-501/ruby-debug-hung-wulo-2316

The ruby-debug-sock-501 comes from "ruby-debug-sock-#{Process.uid}". So yes, technically it's always the same folder under the same UNIX user, regardless of the projects.

I think our original intention was to make sure this extension would only search and connect to the ones that are handled by it? And also /tmp/ruby-lsp-debug-socks will be much shorter than the one debug generates.

Now back to the main question, I think we can probably just provide an example config entry for Rails apps, like:

    {
      "type": "ruby_lsp",
      "name": "Debug Rails Server",
      "request": "launch",
      "program": "bin/rails server -p 3000"
    },

And when users want to debug Rails apps, they use this action to start the server, which will automatically be attached with VS Code. @stage-rl would that work for you?

@stage-rl
Copy link
Contributor Author

Thanks for proposal. Actually it would probably take a more skilled Rails developer than myself to answer this.

Currently my workflow based on recommendations I've seen is running bin/dev. This runs foreman which executes the following Procfile:

web: rdbg -O -n -c --sock-path=/tmp/ruby-lsp-debug-sockets/test.sock -- bin/rails server -p 3000
css: bin/rails tailwindcss:watch
js: yarn build --watch

This is running basically all day (or several days). For debugging I can attach with your extension and it works as expected (with occasional need to stop and rerun when it somehow doesn't break on breakpoints).

I'm not sure I would be able to achieve the same with your proposal if what you plan to do is add additional arguments to rails server ... specified in launch config

@vinistock
Copy link
Member

FWIW, it's already possible to launch a Rails server with the debugger using the configuration Stan mentioned. But in this attach scenario, it's definitely not clear.

I think we should

  • Mention in the debugging documentation that the sock-path needs to be inside /tmp/ruby-lsp-debug-sockets
  • Enhance our quick pick logic so that if there are no sockets to select, we inform the user. Maybe show a message saying No sockets found in /tmp/ruby-lsp-debug-sockets. See [debugging]() for more information. and then we link to the README from that message

@stage-rl are you interested in taking a stab at any of these two?

@tomascco
Copy link
Contributor

Now back to the main question, I think we can probably just provide an example config entry for Rails apps, like:

    {
      "type": "ruby_lsp",
      "name": "Debug Rails Server",
      "request": "launch",
      "program": "bin/rails server -p 3000"
    },

And when users want to debug Rails apps, they use this action to start the server, which will automatically be attached with VS Code. @stage-rl would that work for you?

What I'm currently doing in my Rails app is to use the following initializer:

# frozen_string_literal: true

if Rails.env.development?
  FileUtils.mkdir_p("/tmp/ruby-lsp-debug-sockets/")
  DEBUGGER__.open(nonstop: true, sock_path: "/tmp/ruby-lsp-debug-sockets/development.sock")
end

I like this approach because the debugger is always ready to be attached, I don't need to stop the server and start it again to investigate some issue. It's similar to what @stage-rl is doing, but I also like the fact that I don't need to write a complicated command or use an alias, just plain rails s.

@stage-rl
Copy link
Contributor Author

stage-rl commented Oct 18, 2023

I see, that @vinistock already implemented part of the solution. I can take a look and try to contribute, but it won't be very quick. While looking at other implementation I found they're invoking rbdg --util=list-socks to find the list. If only one is returned, it's immediately used. Is it something to explore further?

@vinistock
Copy link
Member

It's definitely worth checking what debug is doing for listing sockets. It must know which folder they are being put somehow.

@stage-rl
Copy link
Contributor Author

stage-rl commented Oct 21, 2023

Hi, my first implementation (with no error handling) that works for implicitly generated sockets for remote debugging can be found here:
Shopify/vscode-ruby-lsp@86d3aa7 in my fork https://github.com/stage-rl/vscode-ruby-lsp/tree/stage-rl/standard_debug_sockets

I have never modified a vscode extension, so I'm kind of lost on how to proceed. Pls point me in the right direction. Thanks

@vinistock
Copy link
Member

I think that's pretty close. There seems to be a few formatting issues, which you can fix with yarn run format.

Do you want to open the PR already? It's easier to provide specific feedback there.

@stage-rl
Copy link
Contributor Author

I will do that next week when I return from holiday

Copy link
Contributor

This issue is being marked as stale because there was no activity in the last 2 months

@github-actions github-actions bot added the Stale label Dec 26, 2023
@vinistock vinistock added pinned This issue or pull request is pinned and won't be marked as stale and removed Stale labels Jan 2, 2024
@mikehale
Copy link

The simplest way I was able to get this to work with minimal modifications to rails is to run bin/dev like this:

RUBY_DEBUG_SOCK_PATH=/tmp/ruby-lsp-debug-sockets/ruby-debug-$(printf '%q\n' "${PWD##*/}")-0.sock bin/dev

That creates a debug socket where ruby_lsp will look for it.

Ensure you have a launch configuration like this in your launch.json:

        {
            "type": "ruby_lsp",
            "name": "Attach debugger",
            "request": "attach"
        },

Then run that configuration to attach to your debugger.

@st0012 st0012 added the transferred This issue was transferred from vscode-ruby-lsp label Mar 18, 2024
@st0012 st0012 transferred this issue from Shopify/vscode-ruby-lsp Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pinned This issue or pull request is pinned and won't be marked as stale transferred This issue was transferred from vscode-ruby-lsp
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants