NameError: uninitialized constant Dotenv while pushing rails app to heroku - ruby-on-rails

I'm using dotenv to store environment variables and ever since I include it in the gemfile I cannot push it to heroku. I'm getting the following error:-
remote: -----> Installing node-v6.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: sh: 2: Syntax error: Unterminated quoted string
remote: sh: 2: Syntax error: Unterminated quoted string
remote: !
remote: ! Could not detect rake tasks
remote: ! ensure you can run `$ bundle exec rake -P` against your app
remote: ! and using the production group of your Gemfile.
remote: ! rake aborted!
remote: ! NameError: uninitialized constant Dotenv
remote: ! /tmp/build_5437bc300afb80cfa46b1111bb960f46/config/application.rb:17:in `<top (required)>'
remote: ! /tmp/build_5437bc300afb80cfa46b1111bb960f46/Rakefile:4:in `require_relative'
remote: ! /tmp/build_5437bc300afb80cfa46b1111bb960f46/Rakefile:4:in `<top (required)>'
This is how I'm including dotenv in my gemfile:-
gem 'dotenv-rails', :require => 'dotenv/rails-now'
I have tried adding the following in the application.rb file as well:-
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']
still doesn't work.
I don't know if those two lines that say "unterminated quoted string" could be some unrelated issue leading to the dotenv not loading. I looked it up and checked heroku config to see if there was something amiss in the variables but they all seem fine. I was able to push before I added the dotenv to gemfile.
I tried running bundle install, restarting server, deleting gemfile.lock and running bundle install and I looked this issue up on here and tried solutions suggested in Can't push to Heroku because of DOTENV uninitialized constant error
Still no luck.
PS - I'm trying to implement recaptcha and it is suggested best practice to use dotenv to store the site_key and secret_key for recaptcha as env vars. Hence I'm trying to get this to work.

I faced the same issue and was able to solve it with a following.
1) Add dotenv-rails in the Gemfile only for specific environments:
# Gemfile
group :development, :test do
gem 'dotenv-rails'
end
2) And run Dotenv stuff only if the environment matches your Gemfile group:
# config/application.rb
Bundler.require(*Rails.groups)
if ['development', 'test'].include? ENV['RAILS_ENV']
Dotenv::Railtie.load
end

Try adding the gem to the production group.
group :production do
gem 'dotenv-rails'
end

Related

Heroku deployment - cannot load such file -- rake (LoadError)

I get the error:
remote: ! Could not detect rake tasks
04:36
remote: ! ensure you can run `$ bundle exec rake -P` against your app
04:36
remote: ! and using the production group of your Gemfile.
04:36
remote: ! /tmp/build_aa020f9e/bin/rake:8:in `require': cannot load such file -- rake (LoadError)
04:36
remote: ! from /tmp/build_aa020f9e/bin/rake:8:in `<main>'
when running a Heroku deployment pipeline from Codeship
/bin/rake looks like this
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
require_relative '../config/boot'
require 'rake'
Rake.application.run
so line 8 is require 'rake'
I tried adding gem rake explicitly to my gemfile
Then because production group is mentioned I tried
group :production do
gem 'rake'
end
None of this works and so my code push is rejected by Heroku
remote: ! Push rejected, failed to compile Ruby app.
04:36
remote:
04:36
remote: ! Push failed
Any ideas anyone?I see a similar post on StackOverflow rails cannot load such file -- rake (LoadError) but without a solution?
[I've edited this original response to include a solution]
I have this exact issue too, with an open ticket with Heroku. No response yet, sadly but I found an answer elsewhere via a lucky hit in Google.
Context, for future reference and search engines
remote: -----> Detecting rake tasks
remote:
remote: !
remote: ! Could not detect rake tasks
remote: ! ensure you can run `$ bundle exec rake -P` against your app
remote: ! and using the production group of your Gemfile.
remote: ! /tmp/build_b8f358ab/bin/rake:3:in `require': cannot load such file -- rake (LoadError)
remote: ! from /tmp/build_b8f358ab/bin/rake:3:in `<main>'
remote: !
remote: /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/helpers/rake_runner.rb:106:in `load_rake_tasks!': Could not detect rake tasks (LanguagePack::Helpers::RakeRunner::CannotLoadRakefileError)
remote: ensure you can run `$ bundle exec rake -P` against your app
remote: and using the production group of your Gemfile.
remote: /tmp/build_b8f358ab/bin/rake:3:in `require': cannot load such file -- rake (LoadError)
remote: from /tmp/build_b8f358ab/bin/rake:3:in `<main>'
As you've doubtless already found, the suggested bundle exec rake -P command above works fine with any Rails.env setting locally. I, too, tried adding rake to Gemfile explicitly but this made no difference, nor did precompiling assets. Other things to note:
All gems build successfully
This deploy would prompt and update from Heroku-18 to the Heroku-20 platform; does yours?
Rails 5 latest patch, but not Rails 6 yet
Ruby 2.7.2
I don't use Spring so my bin/rake is simpler:
#!/usr/bin/env ruby
require_relative '../config/boot'
require 'rake'
Rake.application.run
...and as with your more complex code, it's definitely the require 'rake' that fails.
Solution
I can't take credit - it's this answer:
https://stackoverflow.com/a/65604896
...that solved it, only you need to change from 2.1.2 to 2.1.4. Copy-pasting the above solution - again this is not my work, the credit belongs to the OP above:
gem uninstall bundler
gem install bundler --version '2.1.4' # If this doesn't work - see below
rm Gemfile.lock
bundle install
git add Gemfile.lock
git commit -m "Downgrade Bundler to match current Heroku version, so that deployments succeed"
git push heroku
This has solved the issue in my case. Seems like an utterly bizarre solution, but there you go.
If you don't seem to be unable to downgrade Bundler
In the comments on this answer, the OP notes:
Because Bundler version 2.2.10 was installed for me as the "default" I hadnt realised Gem bundler-2.1.4 cannot be uninstalled via gem uninstall bundler Using the cleanup_bundler script documented here enabled me to downgrade to v2.1.4 properly. This then did fix the rake problem.
$ gem uninstall bundler
=> Gem bundler-2.2.10 cannot be uninstalled because it is a default gem
StackOverflow prefer answers to be inline rather than linked externally as external links can break, so once again noting that credit goes to the article linked above:
From all the references I can find, the only way to remove it is to delete the bundler-2.1.4.gemspec file from the gem path. The following commands did the trick for me [...] I wrote a script for you:
#!/usr/bin/env ruby
gempaths = `gem env gempath`.split(":")
gempaths.each do |gempath|
# lookup bundler-*.gemspec files and delete them
# this is the only way to completely cleanup default bundler
# Note: the bundler gemspecs' paths are different for CRuby and JRuby
Dir.glob(gempath.strip + "/specifications/**/bundler-*.gemspec").each { |p| File.delete(p) }
end
# Remember to make this file executable
...so hopefully if you save that as a .rb file somewhere convenient, chmod u+x foo.rb and ./foo.rb - that might solve the problem.
I just had this problem and Heroku seems to have added a section to the official documentation that worked for me:
Bundler 2.2.3+
An app’s Gemfile.lock that is generated with Bundler 2.2.3 locally may not work on Heroku unless the Linux platform is explicitly “locked”:
$ bundle lock --add-platform ruby
$ bundle lock --add-platform x86_64-linux
$ bundle install
$ git add . ; git commit -m "Bundler fix"

Ruby on Rails [heroku] git push heroku master error

I am trying to upload my web app on heroku, but the following error occurs.
(I'm coding with cloud 9 IDE)
$ git push heroku master
.....
remote: -----> Installing node-v10.14.1-linux-x64
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: Yarn executable was not detected in the system.
remote: Download Yarn at https://yarnpkg.com/en/docs/install
remote: I, [2019-05-22T02:13:47.373334 #1766] INFO -- : Writing
/tmp/build_fcc84922ee6a02bfc05a163c871d0548/public/assets/noimage-
3aa3997354b4e9c37f379deb61626f55ade493078d1b42dcefe4a3ccbed34106.jpg
remote: rake aborted!
remote: ExecJS::RuntimeError: SyntaxError: Unexpected character '`'
remote: JS_Parse_Error.get ((execjs):3538:621)
remote: (execjs):4060:48
remote: (execjs):1:102
.....
remote: !
remote: ! Precompiling assets failed.
remote: !
remote: ! Push rejected, failed to compile Ruby app.
First of all, It says that "there Yarn executable was not detected in the system", but it is properly downloaded if I check it with yarn -v.
Also, ExecJS :: RuntimeError: SyntaxError: Unexpected character '`' error seems to be that es6 is not recognized, as a solution
config.assets.js_compressor =: uglifier
config.assets.js_compressor = Uglifier.new (harmony: true)
Changed to This seems to be able to read es6. But the above error still persists.
Also, if I run 'rake assets: precompile RAILS_ENV = production' on the console, it runs normally without error.
Thanks.
Try to add this gem in your gem file.
gem 'therubyracer'
Try this link:
Syntax error when deploying Rails app to Heroku: ExecJS::RuntimeError: SyntaxError: Unexpected character
config.assets.js_compressor =: uglifier
=: uglifier
space between : and uglifier?
should be = :uglifier
and try to use precompile command without string RAILS_ENV = production
because Heroku installs production mode by default. if I'm not mistaken
Try this, Go to heroku dashboard on their website, select your app, click on the gear icon (the settings), scroll down until you see “build packs” add ruby and nodejs buildpacks, and make sure the order is that that nodejs comes first in the list, and then ruby (from top to bottom).
If that doesn’t work, can you clarify how you are triggering the compilation of your JS?
Explanation: I’ve seen this error before(with npm, not yarn), essentially you want herokus nodejs buildpack to take care of JS compiling, the one rails has built in seems to not be compatible with their system.

Why am I getting precompiling assets failing?

I typically make updates to my production Ruby on Rails application and today I updated some security vulnerabilities with gem files, pushed them to my Github repo and then did a git push heroku master and received the following errors:
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: rake aborted!
remote: Devise.secret_key was not set. Please add the following to your Devise initializer:
remote:
remote: config.secret_key = '<hash>'
remote:
remote: Please ensure you restarted your application after installing Devise or setting the key....
remote: !
remote: ! Precompiling assets failed.
remote: !
Not sure if there is a connection between actionview -v 5.1.6.2 that I recently updated to and this error.
Open config/initializers/devise.rb file
it contain line
config.secret_key = 'xxxxxxxxxxxxxx'
If you find this line is commented, uncomment in. If it don't have any secret key find your secret key by running rake secret.
Best place to put your secret_key is enviornment variables. So try change line in devise.rb file like below.
config.secret_key = Rails.application.credentials.secret_key_base
Open config/secrets.rb file
production:
secret_key_base: ENV['DEVISE_SECRET_KEY']
In Heroku you can set DEVISE_SECRET_KEY in config variables.

Rails 4 in Action: unable to push code to heroku, sh: 2: Syntax error: Unterminated quoted string

I am working through the Rails 4 In Action Book. I am on chapter 13: Deployment, page 457 where you push your code up to heroku:
git push heroku master
It appears to successfully grab all the needed gems as it gets to this part:
remote: Bundle complete! 27 Gemfile dependencies, 90 gems now installed.
remote: Gems in the groups development and test were not installed.
remote: Bundled gems are installed into ./vendor/bundle.
remote: Bundle completed (29.18s)
remote: Cleaning up the bundler cache
But then right after that it blows up and says this:
remote: -----> Detecting rake tasks
remote: sh: 2: Syntax error: Unterminated quoted string
remote: sh: 2: Syntax error: Unterminated quoted string
remote: !
remote: ! Could not detect rake tasks
remote: ! ensure you can run `$ bundle exec rake -P` against your app
remote: ! and using the production group of your Gemfile.
remote: ! rake aborted!
remote: ! LoadError: cannot load such file -- net/ssh
I did what it suggested and ran bundle exec rake -P. Here was the output from that:
rake aborted!
LoadError: cannot load such file -- net/ssh
This is the tough part when working through a tutorial book. I do not think I missed any steps. I'm not sure what to do here. Any tips would be greatly appreciated.
I figured it out and hope this helps anyone else having the same issue.
What eventually did it for me was that I had to update the fog gem. The book has you use version 1.29.0. For whatever reason: that version was not working for me.
In my Gemfile I simply put gem "fog", deleted my Gemfile.lock and ran bundle so that it would go out and grab the latest version.
Afterwards I looked inside the Gemfile.lock and noticed it grabbed the fog version of 1.38.0. Now when I ran git push heroku master it worked.
What led me to try out updating fog was this issue on the fog github page.
In case anyone was curious: I was using rails 4.2.1, as well as ruby 2.2.1 as the book tells you to.

How to fix the uninitialized constant Rake::DSL problem on Heroku?

I am getting errors similar to the ones in these questions, except mine are occuring on Heroku:
2011-05-30T09:03:29+00:00 heroku[worker.1]: Starting process with command: `rake jobs:work`
2011-05-30T09:03:30+00:00 app[worker.1]: (in /app)
2011-05-30T09:03:30+00:00 heroku[worker.1]: State changed from starting to up
2011-05-30T09:03:33+00:00 app[worker.1]: rake aborted!
2011-05-30T09:03:33+00:00 app[worker.1]: uninitialized constant Rake::DSL
2011-05-30T09:03:33+00:00 app[worker.1]: /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
The answer in those questions seems to be to specify gem 'rake', '0.8.7' because the 0.9 version causes the problem.
When I try to add gem 'rake', '0.8.7' to my gemfile and push to Heroku I get this error:
Unresolved dependencies detected; Installing...
You have modified your Gemfile in development but did not check
the resulting snapshot (Gemfile.lock) into version control
You have added to the Gemfile:
* rake (= 0.8.7)
FAILED: http://devcenter.heroku.com/articles/bundler
! Heroku push rejected, failed to install gems via Bundler
error: hooks/pre-receive exited with error code 1
To git#heroku.com:my_app.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git#heroku.com:my_app.git'
My gemfile normally works fine on Heroku. What should I do?
Put this in your Rakefile above require 'rake':
require 'rake/dsl_definition'
Any time you change your Gemfile, you need to bundle install to update your lockfile (Gemfile.lock). The error you're getting on push is not specific to changing the version of rake.
bundle install
git commit -a -m "update lockfile"
git push heroku master
Note the error message you received:
You have modified your Gemfile in development but did not check the resulting snapshot (Gemfile.lock) into version control
I solved this, finally, after a lot of mucking about. The short version of what I did, missing out the many experiments, was this:
1) change the Gemfile to specify Rake 0.8.7
#in Gemfile
gem "rake", "0.8.7"
2) Take out a hack that I had previously added to Rakefile based on Stack Overflow question Ruby on Rails and Rake problems: uninitialized constant Rake::DSL:
So, my Rakefile is now back to being the standard Rakefile for my app:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
MyApp::Application.load_tasks
3) Change Heroku to run my app in Ruby 1.9.2:
heroku stack:migrate bamboo-mri-1.9.2 --app myapp
git push heroku master
And it seems fine now - the scheduled cron task is running anyway.
EDIT: It did run fine, once, then blew up again next time I pushed something! Arrgh. I think I fixed it now, with the addition of the delayed_job gem, based on the conversation Don't know how to build task jobs:work.
Installing delayed_job doesn't seem like a great solution, but it HAS worked, and I might want to use it sometime I guess, especially with Heroku's once-per-hour cron job (which just isn't frequent enough - there are things I'll probably want to run every five minutes). After I installed the delayed_job gem I had to do the setup for it, otherwise Heroku complains about the missing delayed_jobs table:
#add to gemfile
gem 'delayed_job'
#at command line
bundle install
rails g delayed_job
rake db:migrate
git add -A
git commit -a -m "added delayed_job gem"
git push
heroku rake db:migrate --app myapp
heroku restart --app myapp
I had a Rails 3.0.11 app, which specified rake version 0.8.7 in the Gemfile to get around the version 0.9.2 Rake::DSL problem.
After I converted the app to Rails 3.2.0 (Heroku Cedar stack), I was having a problem with the worker (a rake task) crashing. I changed "gem 'rake', '0.8.7'" to "gem 'rake'", which bundled rake version 0.9.2.2. The worker stopped crashing with the new version.
Your problem is caused by not deleting the Gemfile.lock file and is not specific to Heroku. Deleting Gemfile.lock should fix this problem, but will lead you straight to another one:
To git#heroku.com:tailored-landing-pages.git
* [new branch] master -> master
manfred#painstation2:~/Desktop/projects/ror/ta/tlp307$ heroku rake db:migrate
rake aborted!
ninitialized constant Rake::DSL
/app/Rakefile:13:in `<class:Application>'
/app/Rakefile:12:in `<module:Tlp307>'
/app/Rakefile:11:in `<top (required)>'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/ruby1.9.2/bin/rake:31:in `<main>'
Unfortunately, I haven't found the solution for that problem yet, since downgrading Rake to 0.8.7 doesn't seem to work here. If somebody else has an answer, I would appreciate it very much.

Resources