RidingTheClutch.com

Weird ruby error - undefined method 'require_gem'

Really just posting this for future Googling of this error. (Just jump to the last two paragraphs if all you care about is my solution and not any of these symptoms.)

I was working on a new app (more on that soon) and getting ready to push to my production server. When I tried to load the database schema to get it ready I got the following error:

["\n/usr/local/bin/rake:17: undefined method `require_gem' for main:Object (NoMethodError)\n"]

I figured that deep inside the massive stack of what whatever files rake was including there was some esoteric error, probably dude to different gem versions. The production server had an older version of gem so I updated that. No go. Checked the rake gem and both production and development had the same version (0.8.1). Searched online and there were other people listing this error, but no solution that helped me.

This morning I figured I would just manually create the database and see if the app worked, and it did. Not ideal, but it worked. Now came them time to start the application (two instances of mongrel balanced by Apache) and damn it, the same error! Only this time it was in the mongrel_rails executable:

["\n/usr/local/bin/mongrel_rails:17: undefined method `require_gem' for main:Object (NoMethodError)\n"]

What the hell?? So I looked at the mongrel_rails executable:

["\n#!/usr/local/bin/ruby\n#\n# This file was generated by RubyGems.\n#\n# The application 'mongrel' is installed as part of a gem, and\n# this file is here to facilitate running it. \n#\n\nrequire 'rubygems'\nversion = \"> 0\" \nif ARGV.size > 0 && ARGV[0][0]==95 && ARGV[0][-1]==95\n  if Gem::Version.correct?(ARGV[0][1..-2])\n    version = ARGV[0][1..-2] \n    ARGV.shift\n  end\nend\nrequire_gem 'mongrel', version\nload 'mongrel_rails' \n"]

Sure enough, line 17 has the require_gem method call. What does that same file look like on development?

["\n#!/usr/local/bin/ruby\n#\n# This file was generated by RubyGems.\n#\n# The application 'mongrel' is installed as part of a gem, and\n# this file is here to facilitate running it.\n#\n\nrequire 'rubygems'\n\nversion = \">= 0\" \n\nif ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then\n  version = $1\n  ARGV.shift\nend\n\ngem 'mongrel', version\nload 'mongrel_rails'\n"]

Pretty different. Checked on mongrel versions and the production server had an older one (1.0.1 versus 1.1.4). So I sudo gem update mongrel and I’m back in business—my app loads. But I still couldn’t rake my database to life…

I go back and look at the source of /usr/local/bin/rake and sure enough it’s different on my dev machine and production (pretty much the same differences as the mongrel_rails script). But rake is already up-to-date…what gives? Maybe there’s some secret gem command to update the script for rake, but I don’t know it. I just copied the rake executable from dev to production and everything was perfect! rake db:schema:load RAILS_ENV=production ran like a charm.

No idea why these scripts changed (maybe when rubygems 1.0 was released?) and why there was no process to update these scripts that depended on them…if anyone knows, please leave a comment and share!

blog comments powered by Disqus