1

While deploying my Rails API app to Heroku, my build is failing with the error below:

-----> Detecting rake tasks
sh: 2: Syntax error: Unterminated quoted string
sh: 2: Syntax error: Unterminated quoted string
 !
 !     Could not detect rake tasks
 !     ensure you can run `$ bundle exec rake -P` against your app
 !     and using the production group of your Gemfile.
 !     rake aborted!
 !     NameError: uninitialized constant ApplicationPolicy
/tmp/build_64dbe116b1bd38f520ae98d49690e476/sdotapi-86210153dc2b588aa7b0dc9a60799f5090c76f46/app/policies/user_setting_policy.rb:1:in `<top (required)>'

Now, the problem is that I don't see this NameError error on my local development box. It would seem to suggest that somehow the path for my Pundit policies isn't getting loaded on Heroku but are happening on local box. But I am not sure why:

Here is the relevant part of my application.rb looks like:

config.autoload_paths << Rails.root.join('lib')
Dir[Rails.root.join('app/policies/*.rb')].each &method(:require)

The line where the error was:

class UserSettingPolicy < ApplicationPolicy

Both the policy files live in app/policies.

Any ideas on what might be going on would be super appreciated!

geoboy
  • 1,172
  • 1
  • 11
  • 25
  • check that line in your github repository, you sure that you did not commit to git before pushing to heroku something different? – Fabrizio Bertoglio Sep 17 '17 at 02:03
  • @FabrizioBertoglio Just did, the versions are the same. Github pushes trigger the Heroku deployment, so same files are being used. What do you think might be going on? – geoboy Sep 17 '17 at 02:06
  • where is `ApplicationPolicy` defined? Looks like `rails` does not find it. It is the `super` class you are setting for `UserSettingPolicy`, so rails will need to import all the methods for that class, but it can't find the class. `ApplicationPolicy` has something to do with `Pundit`? – Fabrizio Bertoglio Sep 17 '17 at 02:31
  • It is in `app/policies/application_policy.rb`. What is weird is that this error doesn't happen on my local box though. – geoboy Sep 17 '17 at 02:32
  • your github profile does have `app/policies/application_policy.rb`? Y generate the application policy following the guide? Did you try to restart the heroku server with `heroku restart` or maybe a bad solution is trying `heroku run rails g pundit:install`? https://github.com/elabs/pundit#installation How does your production gemfile looks like? – Fabrizio Bertoglio Sep 17 '17 at 02:36

1 Answers1

1

Found the problem finally. The policy files were getting loaded one by one in the above code and hence had to change it to below for Rails to load entire directory at once it seems.

config.autoload_paths += Dir[Rails.root.join('app', 'policies', '*.rb')]
# earlier it was Dir[Rails.root.join('app/policies/*.rb')].each &method(:require)

On a different note, the above lines in config/application.rb ended up being:

config.autoload_paths += Dir[
    Rails.root.join('app', 'policies', '*.rb'),
    Rails.root.join('app', 'lib', '*.rb')
]

Notice the change for lib, due to changes in autoloading in Rails 5. See here for more details: https://stackoverflow.com/a/40019108

geoboy
  • 1,172
  • 1
  • 11
  • 25