astashov / vim-ruby-debugger

Vim plugin for debugging Ruby applications (using ruby-debug-ide gem)
435 stars 36 forks source link

Debugging Rack apps #40

Closed constfun closed 13 years ago

constfun commented 13 years ago

Is it possible to visit a URL of any Rack app (ex. Rails, Sinatra, Rack middleware) and have Rdebugger hit the breakpoints?

Please forgive my ignorance. It seems that if its possible to debug Rails apps it should be trivial to debug any Rack app, so I must be doing something wrong. I've installed the debugger and I can run and hit breakpoints in individual files of my Sinatra app (:Rdebugger somefile.rb), but not by visiting pages in the browser.

I've got Passenger running over nginx and its serving the app correctly. I'd like to run :Rdebugger in root of my Rack app, visit the page in the browser, and have it break in vim (per the docs thats exactly what happens for a Rails app). Does Rdebugger listen on a specific port that I have to visit (ie http://devsite.local:39767)? Or is the debugger just tailored for Rails only and won't work with other Rack apps?

Either way, the tool is irreplaceable. Thank you!

astashov commented 13 years ago

Sure, it is possible. You can debug any Rack application, including Sinatra and Rails. For now, AFAIK there is no way to use Passenger for debugging Rack applications. I use Webrat or Mongrel instead, by the following algorithm:

  1. Open Vim (or gVim/MacVim) in the root dir of the app
  2. Run debugger by:

    :Rdebugger (for Rails 2.*)
    :Rdebugger 'script/rails start' (for Rails 3.*)
    :Rdebugger '/usr/bin/rackup config.ru' (for any other Rack app, e.g. Sinatra or just plain Rack app). 

Note that you have to use the full absolute path to rackup, usually it is /usr/bin. Also, specify the path to config.ru as the relative path from the app's root dir. I.e. if config.ru is placed right in the root path of the app, you can specify it just as 'config.ru'

This command will start rdebug-ide, which will start app server like Webrat and Mongrel on some port (see below).

  1. Put breakpoint e.g. somewhere in your controller.
  2. Open a route for that controller in some browser, e.g. http://localhost:3000/posts Rails usually starts itself on the 3000 port, 'rackup' usually starts Rack apps on the 9292 port.
  3. After that, debugger should stop execution on the breakpoint you specified and you should be able to see variables there.

Ports 39767 and 39768 are inner ports used only by vim-ruby-debugger for passing messages from and to rdebug-ide, and you don't need to use them at all.

Please keep me posted if you can or can't run it successfully. I'm sure we'll be able to make it work in any case.

Thanks for using it. :)

constfun commented 13 years ago

Thank you for the quick and detailed reply.

I had to install the pre-release version of mongrel since I'm using the current version of ruby (1.9.2) as per: http://stackoverflow.com/questions/1073841/gem-install-mongrel-fails-with-ruby-1-9-1/2892360#2892360

I also installed and tried Thin.

I can run the following commands from the terminal and in root of my Sinatra app and they all launch the server and allow me to navigate to http://localhost:9292 to view the app:

$ rackup
127.0.0.1 - - [09/Jan/2011 15:48:10] "GET / HTTP/1.1" 200 4 0.0164
127.0.0.1 - - [09/Jan/2011 15:48:10] "GET /favicon.ico HTTP/1.1" 200 28 0.0009

- or -

$ rackup -s thin
>> Thin web server (v1.2.7 codename No Hup)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
127.0.0.1 - - [09/Jan/2011 15:51:41] "GET / HTTP/1.1" 200 4 0.0319
127.0.0.1 - - [09/Jan/2011 15:51:41] "GET /favicon.ico HTTP/1.1" 200 28 0.0011

- or -

$ rackup -s webrick
[2011-01-09 15:52:16] INFO  WEBrick 1.3.1
[2011-01-09 15:52:16] INFO  ruby 1.9.2 (2010-12-25) [i686-linux]
[2011-01-09 15:52:16] INFO  WEBrick::HTTPServer#start: pid=5442 port=9292
127.0.0.1 - - [09/Jan/2011 15:52:22] "GET / HTTP/1.1" 200 4 0.0031
127.0.0.1 - - [09/Jan/2011 15:52:22] "GET /favicon.ico HTTP/1.1" 200 28 0.0008

- or explicitly (mongrel seems to be default with just 'rackup') -

$ rackup -s mongrel
127.0.0.1 - - [09/Jan/2011 15:53:48] "GET / HTTP/1.1" 200 4 0.0146
127.0.0.1 - - [09/Jan/2011 15:53:48] "GET /favicon.ico HTTP/1.1" 200 28 0.0009

However, when in vim I do:

:Rdebugger '/usr/local/bin/rackup config.ru'
or :Rdebugger '/usr/local/bin/rackup -s mongrel config.ru'
or :Rdebugger '/usr/local/bin/rackup -s thin config.ru'
etc.

Rdebugger reports that the debugger has been started, but I get nothing in the browser at http://localhost:9292

astashov commented 13 years ago

Looks like you use Ruby 1.9.2 as default Ruby installed system-wide, and without RVM, right? Please do these things:

  1. Update vim-ruby-debugger to the latest version
  2. Add 'let g:ruby_debugger_debug_mode = 1' to your .vimrc
  3. Go to the Sinatra app's dir and run Vim
  4. Execute :Rdebugger /usr/local/bin/rackup
  5. Send me log files: ~/.vim/tmp/ruby_debugger ~/.vim/tmp/ruby_debugger_log ~/.vim/tmp/ruby_debugger_output

Thanks!

constfun commented 13 years ago

Pulling your latest changes from today fixed the problem!

Before pulling latest. This was in ruby_debugger_output: Fast Debugger (ruby-debug-ide 0.4.16, ruby-debug-base 0.11) listens on 127.0.0.1:39767 /usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug-ide-0.4.16/lib/ruby-debug-ide.rb:112:in debug_load' /usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug-ide-0.4.16/lib/ruby-debug-ide.rb:112:indebug_program' /usr/local/lib/ruby/gems/1.9.1/gems/ruby-debug-ide-0.4.16/bin/rdebug-ide:87:in <top (required)>' /usr/local/bin/rdebug-ide:19:inload' /usr/local/bin/rdebug-ide:19:in `

' Uncaught exception: no such file to load -- /home/nick/workspace/test_project//usr/local/bin/rackup

As a sanity check I ran: Rdebugger '../../../../usr/local/bin/rackup'

This worked! Again, this is prior to pulling latest.

Then I pulled your latest changes and it now works with the absolute path as well: Rdebugger '/usr/local/bin/rackup'

It appears that somewhere in astashov/vim-ruby-debugger@b49005f80705f0e849cf319a99c92eebf55b8bc4 you fixed the problem.

And yes, you're exactly right. 1.9.2 and no RVM

Everything is working perfectly now (breaks, variable browser). Thank you!

astashov commented 13 years ago

Great! Yes, I've fixed path resolving, and now you can use just ':Rdebugger rackup' if /usr/local/bin is presented in your PATH environment variable.