In playing around with a number of debugging options for Rails, I concluded that nothing that was documented was suitable for me. I want what everyone else has: a fairly powerful debugger that doubles as an IDE. I want persistent watches, breakpoints, code stepping and variable inspection. Eclipse has all these features, so I wondered why no one had gotten a rails debug setup working properly inside this environment. So I set out to do just that.
Setting up Eclipse to do in-line debugging for Rails was surprisingly easy. The way I did it is still leaves something to be desired but it seems to work. I’m hoping others will pitch in and help make the functionality more complete. If this method has already been documented, please let me know and I’ll point to that resource. I couldn’t find anyone who had setup Eclipse to do inline debugging with Rails: what I mean by inline debugging is being able to run my Rails app within Eclipse, hitting it from a web browser and being able to step through code, set breakpoints and inspect variables using the Eclipse built-in debug system.
If you’re not very comfortable with using Eclipse and Ruby together, you might also read this great, more general how-to article on running and debugging Ruby apps inside Eclipse: http://www-128.ibm.com/developerworks/opensource/library/os-rubyeclipse/
They don’t speak to Rails specifically and it’s that enhancement which this article is intended to address (also they talk about RDT and I’m using EasyEclipse which is based on RadRails, but that doesn’t seem to matter in this particular case).
Also, you might find this walk-through helpful as well. It’s essentially the same information as in this article, but with some screen shots. Kudos to Rich for helping to make this easier. Now if RadRails would just get their fast debugger going, we could finally mothball this whole subject!
First off, I did all this in Windows, so your paths will vary on other boxes. I am also using “Easy Eclipse for Rails 1.02″ version of Rails: http://www.easyeclipse.org
This app simplifies RadRails and other configuration stuff within Rails, which is great. I was also using the Webrick server configuration. Not sure how all this would work in other webserver environments.
(Update 1/2/07: See note at the end of this article for information on getting Mongrel to work with RadRails/Eclipse debugging).
First off, from within Eclipse, you need to create a new debug process item. This assumes that you have a valid rails project and it’s loaded correctly into Easy Eclipse. You should be running Eclipse and looking at the main Rails projects browser window on the left (at least).
=> Choose the “Run/Debug” menu option. This brings up the debugger definition window. Under “Ruby application” create a new item called “depot server” (or whatever you want to call it). This opens up the debug item creation screen: On the main screen of this form, I then entered the project I am using called (in my example) “depot” and entered the file: script/server (always use “script/server” here – this is how you run the Webrick server, which in turn will call your application code which is how we will catch breakpoints from within Eclipse!).
(If you don’t see a Ruby Application item in the Debug window, possibly installing RadRails will do this for you – like I said, I used Easy Eclipse for Rails 1.0.2 – without it your mileage will vary).
Here’s the important part:
In the “argument” tab of debug item creation screen, under interpreter arguments, set Rails to be included automatically when Ruby starts up by adding a line that looks like this (change to fit where your copy of rails is located):
(note no space between -I and quoted path and remember: these are Windows only instructions)
Next, under the Environment tab, the interpreter should be set to your Ruby interpreter path/file.
(If you don’t have a Ruby interpreter setup yet, go back to the main Eclipse window and choose Window/Preferences, scroll down to and open Ruby, choose “Installed Interpreters” and create a Name/Location pair that points to your Ruby.exe file).
Under the Common tab, “Allocate Console” should be checked and “Launch in Background” should be unchecked.
Before running it, make a breakpoint. From what I can tell so far, you can only set breakpoints before you run the server, which is an annoyance (see below).
Here’s an example of where you can make a breakpoint: head over to your application folder under the Rails project you’re currently using: e.g. depot/app/controller/admin_controller.rb
Open up this file and put a breakpoint on a method you can call from the browser (i.e. a public method suitable for the rails app) e.g. “ship” (You can add breakpoints by double clicking in the grey area next to any line of code).
That’s it. To run it simply click the Debug button at the bottom of the page. And WAIT (it takes about 40 seconds on my weeny 1.5ghz box for Webrick to boot). Once it’s running – you should see it’s familiar text in the console window:
ruby 1.8.5 debugger listens on port 2042 [2006-09-14 14:39:56]
INFO WEBrick 1.3.1 [2006-09-14 14:39:56]
INFO ruby 1.8.5 (2006-08-25) [i386-mswin32] [2006-09-14 14:39:56]
INFO WEBrick::HTTPServer#start: pid=3488 port=3000
Then open your web browser to the correct page. In the example I’m providing this would be http://localhost:3000/admin/ship Then WAIT. It takes a bit of time (40 seconds again?) for the server to run to your line of code. But after a minute you should get a real live Eclipse debug session up, stopped at the line of code where you put your breakpoint. Once you’re done, you can click run or press F8 to give the code back to the interpreter. You should then see an entry in the console which reflects your request interaction, e.g.:
127.0.0.1 - - [14/Sep/2006:14:43:48 Pacific Daylight Time] "GET /admin/ship HTTP/1.1" 200 1914- -> /admin/ship
Cool huh? Email me or post comments here with questions or ideas about how to make this work better. My address is: public AT DELETE_THIS misuse DOT ORG
The current big to-do here is to figure out how to get Eclipse to recognize breakpoints after the script is running. What’s weird is that Eclipse debugger does recognize changes to source code, so you can alter your app code and reenter from the browser without things getting weird (so far as I can tell). But Eclipse doesn’t seem to support adding breakpoints in Rails/Ruby once the debugger is off and running. Any ideas?
Note: Here’s some info from Noah Stern on getting RadRails/EasyEclipse setup to work with Mongrel. Thanks to Zed Shaw for some technical diznizzles on this: In paragraph 9 titled => Choose the “Run/Debug” menu option, you specify file script/server.Under RadRails, any ruby file you declare here must be located inside the project as of 0.7.2.So you create a new ruby file. I called mine mongrel_debug.rb and put in in the script folder where server.rb is located.this file contains the following:
require "C:/InstantRails/ruby/bin/mongrel_rails" start = Start.new start.run.join
In order for this to work, you must make a copy of mongrel_rails located in C:/InstantRails/ruby/bin and call it mongrel_rails.rb so that ruby can find it. Then start your mongrel server using the debugger, not the normal way.
I was able to intercept breakpoints and step through line by line. Thanks for posting this article as it gave me the incentive to try to get it working for mongrel.