Using Eclipse for inline debugging with Rails 48

Posted by scientific on September 13, 2006

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!

http://richtextblog.blogspot.com/2006/09/radrails-all-is-forgiven.html

==Setup==
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.

==Config==
(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):


-I"C:/apps/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6"

(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.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. max williams Thu, 02 Aug 2007 11:38:08 UTC

    Hi there – thanks for the very informative post!

    I’ve just got set up with radrails in eclipse, using mongrel_rails. I followed all the instructions above (i think), including the new file for mongrel_rails, and my app seems to debug ok (nice debug text scrolls through the eclipse console window when i move around the site).

    My problem is with breakpoints – i put one on the first line of a controller method, then started the debugger and called the method in the usual way (ie by using my app). But the debugger didn’t halt on the breakpoint, it just carried on.

    My debug config settings are as follows: (my app is called newspipe)

    File:
    name: newspipe_server
    file: script\mongrel_debug.rb
    arguments:
    interpreter arguments: -I”C:\code\InstantRails\ruby\lib\ruby\gems\1.8\gems\rails-1.2.3″
    program arguments: C:\code\InstantRails\ruby\bin\rubyw.exe

    Does anyone have any idea what my problem might be?
    thanks again – max

  2. science Thu, 02 Aug 2007 12:05:49 UTC

    Max:

    Just to be clear, you are running Rails from within Eclipse and then accessing the appropriate localhost URL from a web browser?

    If so, I’d say you should try to get debugging working from within Webrick first. You can do this by setting your file to “script\server webrick” (this will force server.rb to run webrick instead of mongrel).

    Also be sure you are looking at the “Console” window inside Eclipse – you’ll get lots of useful error messages there. One easy mistake is to leave a server running accidentally out on a command shell someplace (listening on port 3000 or whatever) and then attempting to run a debug instance that tries to listen on the same port – this will fail of course – but with a weird Net::HTTP error as I remember).

    Also, be sure that “allocate console” option is set (under the common tab).

    Try the file option of “script\console” as well – see if you can get this to pop up a console window – might help with figuring out what’s wrong. This is also a great technique for debugging stuff where it’s a pain to access it from a browser – I’m learning this is a very powerful technique.

    Finally, setting breakpoints themselves are kind of spooky in Eclipse via RDT. As I mention in the article, once you set a breakpoint and run, it kind of turns itself off. Not sure exactly what’s going on there but the safest thing is to shut down Eclipse, open it up again, set a breakpoint, fire up the debugger, wait for it to load completely, then hit your app from a browser..

    Post back if you’re still not getting anywhere.

  3. max williams Fri, 03 Aug 2007 06:02:53 UTC

    Thanks – i managed to fix it though. I’d set my project up wrong – i’d imported an existing project, but it was running in the default project folder – so a copy of the files were running and i was editing the originals (or maybe vice versa). Deleted the project, re-imported it and made sure the working folder was the same as the imported folder, and breakpoints seem to work fine now.

    Thanks a lot for the advice though and thanks again for this guide :)

  4. Desmond Fri, 14 Sep 2007 20:35:12 UTC

    Hey, thanks for the debugger setup instructions. I set and project that worked fine with WEBrick. I then set up two other projects with Mongrel. One worked fine. The other works, but sends my CPU usage to 99% and the rails app gets to my break point several minutes after the page request. Anyone know what the problem might be?

    Also, is it really appropriate to run the server from within Eclipse? I thought it must be feasible and more desirable to run it externally, and then connect to it in the same way that the debugger connects to a JVM. I don’t know for sure whether this is a possibility and surely wouldn’t know how to set this up.

  5. science Sun, 16 Sep 2007 16:00:13 UTC

    I haven’t tried making Mongrel work but max_willams has. I think he’s written up some stuff about on his blog: http://nubyonrails.wordpress.com/

    It may not really appropriate to run the server inside Eclipse but it seems to work and given that there are no other free in-line debugger solutions out there, it seems reasonable? I’d be very interested in any information as to how to make remote debugger connections in Eclipse. Essentially the RDT guys have made it really easy to connect Ruby instances to the Java debugger within Eclipse. I’m not sure what they’re doing, so I have no idea how to do it outside of Eclipse, but if you browsed their source code, I’m sure some ideas would come up. Keep us posted!

  6. Deirdre Lee Wed, 19 Mar 2008 03:41:52 UTC

    Hi,

    I’ve set up RadRails in Eclipse as specified in the article, but I always get a java.lang.reflect.InvocationTargetException when launching a debug. Running the same server causes no problems. I’m using webrick, rails 1.2.6, RadRails 1.0.0.2, and ruby 1.8.6.

  7. science Wed, 19 Mar 2008 07:32:49 UTC

    Deirdre:

    I have not tried the “new” RadRails system made by Aptana. The entire system above is designed for the original RadRails (0.7.2?) – which is still available on Aptana’s site I think.

    But I believe that new RadRails has its own in-line debugging system – so you might read up on Aptana’s forums for information on making it work. It’s probably not much better (ie faster) than our method but at least it’s supported by a real company!

    Post back if you want help on 0.7.2 RadRails..

  8. BarnaB Sun, 14 Jun 2009 13:20:26 UTC

    Thx a lot…
    Could not understand how i could have lived without this before.

    Works for me on linux ubuntu / eclipse

    -I”/home/BarnaB/.gem/ruby/1.8/gems/rails-2.2.2/”

    and interpreter is /usr

  9. Morris Fri, 22 Aug 2014 14:52:13 UTC

    bacteria@beaumont.respondents” rel=”nofollow”>.…

    tnx for info!…

  10. Chester Mon, 25 Aug 2014 23:40:47 UTC

    nagel@snaked.transposition” rel=”nofollow”>.…

    спс….

  11. perry Mon, 17 Nov 2014 17:06:30 UTC

    mcnamara@shaking.paradigmatic” rel=”nofollow”>.…

    good!!…

  12. Kelly Tue, 18 Nov 2014 15:42:49 UTC

    bruxelles@chump.encroach” rel=”nofollow”>.…

    ñïñ….

  13. mario Fri, 21 Nov 2014 07:57:19 UTC

    stands@knightes.franklins” rel=”nofollow”>.…

    ñïàñèáî çà èíôó….

  14. cory Sat, 22 Nov 2014 12:23:32 UTC

    municipally@overwhelm.artisans” rel=”nofollow”>.…

    áëàãîäàðþ!…

  15. Willard Sun, 23 Nov 2014 02:50:55 UTC

    runyons@drastically.bodice” rel=”nofollow”>.…

    ñïñ!!…

  16. Christian Sun, 23 Nov 2014 07:53:37 UTC

    hymen@childlike.suzanne” rel=”nofollow”>.…

    ñïñ!!…

  17. Francis Mon, 24 Nov 2014 00:43:06 UTC

    daring@ches.chiding” rel=”nofollow”>.…

    ñïñ çà èíôó….

  18. Ben Wed, 26 Nov 2014 22:38:47 UTC

    nightfall@variation.ter” rel=”nofollow”>.…

    good….

  19. Edgar Thu, 27 Nov 2014 05:58:44 UTC

    expandable@pompey.tiered” rel=”nofollow”>.…

    ñïñ!…

  20. Danny Fri, 05 Dec 2014 19:58:50 UTC

    populaire@freddy.precociously” rel=”nofollow”>.…

    tnx for info!!…

  21. Alejandro Sat, 06 Dec 2014 15:13:20 UTC

    kenilworth@burgeoning.groupings” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  22. harold Fri, 12 Dec 2014 12:53:57 UTC

    streaked@biopsy.assertions” rel=”nofollow”>.…

    thank you!!…

  23. julius Tue, 16 Dec 2014 07:05:48 UTC

    devious@yalies.exerted” rel=”nofollow”>.…

    good info….

  24. darrell Fri, 19 Dec 2014 21:17:34 UTC

    buenas@baptized.dilthey” rel=”nofollow”>.…

    ñïñ….

  25. jeffery Mon, 22 Dec 2014 02:39:28 UTC

    quirks@vitality.caskets” rel=”nofollow”>.…

    good info….

  26. albert Wed, 24 Dec 2014 14:43:56 UTC

    thickness@sherrill.ekstrohm” rel=”nofollow”>.…

    thanks!!…

  27. Dave Thu, 15 Jan 2015 10:37:10 UTC

    cobblestones@swiped.subpenaed” rel=”nofollow”>.…

    ñïñ çà èíôó….

  28. martin Fri, 16 Jan 2015 03:13:24 UTC

    poke@torquers.clandestine” rel=”nofollow”>.…

    tnx for info….

  29. jose Fri, 16 Jan 2015 03:45:57 UTC

    appendix@gamut.awful” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  30. tyrone Fri, 16 Jan 2015 05:38:46 UTC

    gesticulated@negotiating.resided” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  31. alfonso Fri, 16 Jan 2015 06:12:04 UTC

    epicure@courting.redheaded” rel=”nofollow”>.…

    thank you!…

  32. Julius Sun, 18 Jan 2015 03:41:11 UTC

    recede@brett.riverbank” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  33. louis Wed, 21 Jan 2015 06:29:52 UTC

    marenzio@referent.rundown” rel=”nofollow”>.…

    ñïñ!…

  34. Manuel Wed, 21 Jan 2015 07:48:41 UTC

    whole@complied.milt” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  35. stanley Sat, 24 Jan 2015 17:47:48 UTC

    chaves@bespeak.davao” rel=”nofollow”>.…

    ñïñ….

  36. Leslie Sat, 31 Jan 2015 09:32:50 UTC

    gravid@mediating.owes” rel=”nofollow”>.…

    ñïñ….

  37. Guy Sun, 01 Feb 2015 22:35:19 UTC

    disciplining@inwardness.danish” rel=”nofollow”>.…

    thanks for information!…

  38. eugene Mon, 02 Feb 2015 22:30:45 UTC

    direct@painteresque.boasting” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  39. jesus Mon, 02 Feb 2015 23:03:46 UTC

    booty@enlarging.butter” rel=”nofollow”>.…

    tnx for info!!…

  40. ronnie Thu, 05 Feb 2015 00:03:26 UTC

    franklin@wills.homemaster” rel=”nofollow”>.…

    good….

  41. isaac Thu, 05 Feb 2015 00:34:18 UTC

    beating@gastrocnemius.barracks” rel=”nofollow”>.…

    tnx for info!…

  42. kevin Sat, 07 Feb 2015 12:19:06 UTC

    instruments@highwayman.fare” rel=”nofollow”>.…

    áëàãîäàðþ….

  43. Ian Sat, 07 Feb 2015 12:51:16 UTC

    about@ambushes.thoroughfare” rel=”nofollow”>.…

    ñïñ!!…

  44. adrian Sat, 07 Feb 2015 13:23:27 UTC

    slackened@bestubbled.propeller” rel=”nofollow”>.…

    ñïñ çà èíôó!…

  45. Antonio Sun, 08 Feb 2015 04:48:28 UTC

    expounded@thets.vividness” rel=”nofollow”>.…

    áëàãîäàðþ!…

  46. Kevin Mon, 09 Feb 2015 07:42:50 UTC

    sombre@infinite.zamiatins” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

  47. douglas Tue, 10 Feb 2015 13:09:15 UTC

    heavens@thanks.snared” rel=”nofollow”>.…

    ñïñ çà èíôó!…

  48. Wayne Tue, 10 Feb 2015 13:48:41 UTC

    explain@lodged.syndicates” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!!…

Comments