This project is archived and is in readonly mode.

#1851 ✓resolved
Nicholas Faiz

TMail Autoloading Issue

Reported by Nicholas Faiz | February 2nd, 2009 @ 10:48 PM

Hi,

It seems as though action mailer has an issue with locating the tmail gem.

The actionmailer-2.3.0/lib/action_mailer/vendor/tmail.rb file requires tmail, then later creates an empty TMail module, then silently sets a variable in TMail::Encoder. This blows up as a nameerror.

/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.0/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant TMail::Encoder (NameError)
        from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.0/lib/active_support/dependencies.rb:80:in `const_missing'
        from /opt/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.0/lib/action_mailer/vendor/tmail.rb:16
        from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.0/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
        from /opt/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.0/lib/action_mailer/vendor/tmail.rb:15

Note - this is within an env. where tmail is installed:

silenus:bin nick$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'tmail'
=> true
irb(main):004:0> TMail::Encoder
=> TMail::Encoder

Comments and changes to this ticket

  • talksense101

    talksense101 February 3rd, 2009 @ 12:39 PM

    • Tag set to 2.3

    I get the same error. I installed 2.3 by following instructions on the Rails weblog at rubyonrails.org

  • Marcus Mateus

    Marcus Mateus February 4th, 2009 @ 09:22 PM

    I think it may have something to do with the use of the new autoload functionality in action_mailer.rb, "autoload :TMail, 'action_mailer/vendor/tmail'"

    I changed that line to "require 'action_mailer/vendor/tmail'" and things it appear to work now... hopefully another clue to root cause. Good enough for me to continue the upgrade to 2.3 for now anyway.

  • Marcus Mateus

    Marcus Mateus February 8th, 2009 @ 05:31 AM

    • Tag changed from 2.3 to 2.3, autoload
    • Title changed from “TMail::Encoder NameError” to “TMail Autoloading Issue”

    Okay, I have spent far to much time working on this and have managed to root cause the error and write tests that replicate the issue, but I need help finding a good long-term solution.

    Root Cause:

    Steps to Reproduce

    *tmail gem version '~> 1.2.3' is present *require 'tmail' is run prior to any call using the TMail constant

    Failure Reason

    When the require is called prior to any call using the TMail constant, the autoload is actually invoked from within the require. This occurs when the gem is loading and tries to define the TMail module. At that point, the gem is only half-loaded, yet the constant is undefined so rails attempts to "autoload" it. When the autoload code runs and attempts to set a constant on the as yet undefined TMail::Encoder an exception is thrown.

    Work Around

    The best work around is for those experiencing this issue to add the following somewhere towards the top of their environment.rb (ie. before Rails::Initializer.run):

    Module TMail end

    Doing this ensures that for most people using utf-8 the MAX_LINE_LEN constant is still properly set by the autoloader, and for the rest of us the gem works. In fact someone needing both the gem and utf-8 can simply set the constant themselves later.

    Partial Patch w/ Failing Tests

    I have written tests which reproduce the error with a tmail gem present, and check that the TMail::Encoder::MAX_LINE_LEN is properly set. Unfortunately, I have not been able to come up with a way to easily ensure that both tests pass. Currently the gem is able to load fully, but the constant is not properly set. Maybe there is some way to ensure it is loaded when UTF-8 is used instead??

    If anyone, an autoload expert perhaps?, has any ideas I would really appreciate it... I'm officially stumped.

  • Marcus Mateus

    Marcus Mateus February 8th, 2009 @ 05:35 AM

    Darn formatting is all messed up (preview would be nice), but hopefully you get the idea.

    Marcus

  • Nicholas Faiz

    Nicholas Faiz February 8th, 2009 @ 05:45 AM

    Marcus,

    Thanks for posting the workaround. It's something to use in the meantime, at least. Will test it on Tuesday.

  • Marcus Mateus

    Marcus Mateus February 23rd, 2009 @ 11:39 PM

    • Assigned user set to “josh”

    After some more thought on this issue, I think the best mid-term solution is actually to simply require TMail, rather than autoloading it in action_mailer.rb... I can not think of another reliable way to avoid the re-entry condition described above.

    Joshua, assigning to you since from the commit history it looks like you were involved in a lot of the original autoloading changes. Please reassign it to the appropriate person if it should be someone else.

    If any of what I have described is not clear, please let me know and I will do my best to post a follow up.

    Thanks, Marcus

  • josh
  • Repository

    Repository February 24th, 2009 @ 04:49 PM

    • State changed from “new” to “resolved”

    (from [5c684f518feeaf38961f976b966e57abf386e248]) Just require tmail instead of autoloading it to fix issues when using the gem version [#1851 state:resolved] Signed-off-by: Joshua Peek josh@joshpeek.com http://github.com/rails/rails/co...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Tags

Referenced by

Pages