Can't dup NilClass

I'm having the same issue discussed here http://groups.google.com/group/rubyonrails-core/msg/787b561d166abf53

As reported elsewhere, in 2.3.2, the "Can't dup NilClass" error pops up on the second request to a page, after a server restart. It seems esp. "buggy" considering that you can bypass the error consistently by setting config.cache_classes = true in development.rb (as in prod & test environments).

Anybody found a solution for this?

Luke

I'm having the same issue discussed here http://groups.google.com/group/rubyonrails-core/msg/787b561d166abf53

As reported elsewhere, in 2.3.2, the "Can't dup NilClass" error pops up on the second request to a page, after a server restart. It seems esp. "buggy" considering that you can bypass the error consistently by setting config.cache_classes = true in development.rb (as in prod & test environments).

Anybody found a solution for this?

Luke

>

What a coincidence (or sign that our community is big): I was fighting the same problem yesterday.

I found that I could more reliably track the root cause by leaving config.cache_classes alone and instead settting

  config.reload_plugins = true if RAILS_ENV == 'development'

***in environment.rb (not in development.rb***

I resolved my problem by cleaning up plugin code to do less "require" and more auto-loading. I moved/renamed files to match Rails' autoloading expectations.

For testing, use the console and try "reload!" I was fortunate in that I would get a shower of sparks every time I tried "reload!" until things were clean. Once cleaned, I reset reload_plugins to false (just deleted the above line actually).

Also, there is a high correlation between the "Can't dup NilClass" error and the "A copy of XX has been removed from the module tree but is still active!" In my case, solving one solved the other. And which one appeared had something to do with the config settings.

Good luck!

-Chris

Thanks, Chris. I've seen a couple of posts re: config.reload_plugins. However, I actually don't have any plugins, other than Haml, which has a single init file. Any other ideas?

Luke

I have some ideas, but I'd like to test them out first; would it be
possible for you to upload a stripped down version of your rails
project with only what's required to reproduce the problem?

I found the solution, at least in my case. I have a class defined in test/mock/development that uses 'require' vs. 'require_dependency'. Changing to require_dependency fixed the problem, which I can understand. I was not (at least it seemed) seeing the same error in test env, where I was using the same mock class. However, my test env of course does not cache classes as in dev, so my assumption was faulty.

Note: I had already downgraded to Rails 2.2 before finding the real problem. The (likely) related error I was getting in 2.2 is " NoMethodError Exception: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.include? "

Luke

I had figured the problem was something like that, and I think it highlights a bigger issue: These types of errors are not uncommon, yet the error messages are uninformative, and the solutions are not officially documented. It would seem that require_or_load, require_dependency and require_association are all meant for internal use only, yet there seem to be situations where using them is the only way to make things work.

Any ideas on how to tackle this?

How about a tutorial (better yet, a screencast) in the rails guides or wiki, by Rails version, of how the environment, plugins, and gems are loaded, and a list of common pitfalls. I've been using Rails since Jan '06 and the file loading process is still largely an unknown for me. And as you say, require_dependency and the like are not commented in the source, so you have to study the code to understand their intent. I didn't even know about require_or_load. In fact, would you mind explaining when you might want to use that method?

Luke