documentcloud / jammit

Industrial Strength Asset Packaging for Rails
http://documentcloud.github.com/jammit/
MIT License
1.16k stars 197 forks source link

Path constants set incorrectly when using rake from another directory #67

Closed nragaz closed 13 years ago

nragaz commented 14 years ago

With Jammit 0.5.3, Rails 3, Bundler 1.0, and Ruby 1.9.2, doing something like this:

rake -f /my/rails/app/Rakefile task:name

from a directory that is not the Rails root will cause Jammit to throw an exception - it will look for a config file relative to the current directory, rather than relative to the Rails root.

I've pondered the source and can't figure out how to resolve this - somehow the ASSET_ROOT constant is being defined before the Rails constant is defined during loading. Adding Rails as a dependency doesn't help - I think somehow Jammit's loading needs to be done in the Railtie initialization block?

jashkenas commented 14 years ago

You can solve it by passing -c or --config to the jammit command, and specifying the path to your assets.yml. config/assets.yml is just the default location.

If you're calling Jammit from the ruby side, you can pass :config_path as an option.

nragaz commented 14 years ago

Thanks!

nragaz commented 14 years ago

On thinking this over and reading it more carefully - I don't think that actually resolves the issue.

The issue arises if you're calling an unrelated Rake task, like db:migrate, from another directory, then if you have the jammit gem enabled in your app you'll get an exception and won't be able to run the Rake task.

From what I can see in jammit.rb, there is an attempt made to identify if Rails is defined and then set the ASSET_ROOT accordingly - but that isn't working properly if the current directory is not the root of your app. I presume because Bundler is loading jammit.rb before Rails...

(On the other hand, I was able to resolve this by using cd /path/to/my/app && rake my:task as needed. And my server is 100x more zippy now that Jammit is compressing and statically gzipping my assets. So Jammit FTW.)

jashkenas commented 14 years ago

Thanks for the details. Re-opening the ticket.

jashkenas commented 13 years ago

nragaz: What happens if you set the RAILS_ROOT environment variable from the command-line, before trying to use Jammit from a different directory?

nragaz commented 13 years ago

Same error.

jashkenas commented 13 years ago

So, if Rails.root is truly not defined -- then what heuristic do you think we should be using to detect it?

nragaz commented 13 years ago

My suggestion would be:

a) If no Jammit configuration path or Rails.root is present, do nothing (don't raise an exception); b) If Rails.root becomes available later in the load process, access it then.

I don't know if this is possible - but given that the Rake tasks don't involve assets at all, I hope that it would be possible to delay loading of the config file.

jashkenas commented 13 years ago

nragaz: because I can't reproduce this here -- what's the exact exception + stacktrace that you're seeing?

nragaz commented 13 years ago
    Ada:nragaz nragaz$ RAILS_ROOT=/Users/shared/gh/nragaz/hms-reports rake -f /Users/shared/gh/nragaz/hms-reports/Rakefile log:clear --trace
(in /Users/Shared/gh/nragaz)
rake aborted!
could not find the "/Users/Shared/gh/nragaz/config/assets.yml" configuration file
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/jammit-0.5.3/lib/jammit.rb:63:in `load_configuration'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/jammit-0.5.3/lib/jammit/dependencies.rb:15:in `<top (required)>'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bundler/gems/rails-e6772750682a/activesupport/lib/active_support/dependencies.rb:239:in `require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bundler/gems/rails-e6772750682a/activesupport/lib/active_support/dependencies.rb:239:in `block in require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bundler/gems/rails-e6772750682a/activesupport/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bundler/gems/rails-e6772750682a/activesupport/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bundler/gems/rails-e6772750682a/activesupport/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bundler/gems/rails-e6772750682a/activesupport/lib/active_support/dependencies.rb:239:in `require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/jammit-0.5.3/lib/jammit.rb:185:in `<top (required)>'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in `require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in `block (2 levels) in require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler/runtime.rb:62:in `each'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler/runtime.rb:62:in `block in require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler/runtime.rb:51:in `each'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler/runtime.rb:51:in `require'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@global/gems/bundler-1.0.0/lib/bundler.rb:112:in `require'
/Users/Shared/gh/nragaz/hms-reports/config/application.rb:3:in `<top (required)>'
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'
/Users/Shared/gh/nragaz/hms-reports/Rakefile:1:in `<top (required)>'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2383:in `raw_load_rakefile'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2017:in `block in load_rakefile'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2000:in `block in run'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bin/rake:19:in `load'
/Users/nragaz/.rvm/gems/ruby-1.9.2-p0@hms-apps/bin/rake:19:in `<main>'
jashkenas commented 13 years ago

I've pushed a commit to master that should help with this:

http://github.com/documentcloud/jammit/commit/cb85c4867966f9004c28b0edc9a969ebf9c2d3f7

Give that a try, and let me know if it works for you.

nragaz commented 13 years ago

I'll try it once Github's git:// is back up.

nragaz commented 13 years ago

That works great. (edit: with and without setting RAILS_ROOT in the command)