Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gem Load Error is: undefined local variable or method `dotenv_files' #297

Closed
heaven opened this issue Apr 12, 2017 · 8 comments · Fixed by #391
Closed

Gem Load Error is: undefined local variable or method `dotenv_files' #297

heaven opened this issue Apr 12, 2017 · 8 comments · Fixed by #391
Labels

Comments

@heaven
Copy link

heaven commented Apr 12, 2017

Getting this with 2.2.0, 2.1.2 does work.

runtime.rb:94:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'dotenv/rails-now'. (Bundler::GemRequireError)
Gem Load Error is: undefined local variable or method `dotenv_files' for #<Dotenv::Railtie:0x007fd3f94eac30>
Backtrace for gem load error is:
/Users/heaven/.rvm/gems/ruby-2.3.1@crm/gems/dotenv-rails-2.2.0/lib/dotenv/rails.rb:38:in `load'
/Users/heaven/.rvm/gems/ruby-2.3.1@crm/gems/dotenv-rails-2.2.0/lib/dotenv/rails.rb:51:in `load'
@n0nick
Copy link

n0nick commented May 23, 2017

FWIW a hacky way to avoid this error is to add the following to config/application.rb (before the Bundler.require call):

module Dotenv
  class Railtie < Rails::Railtie
    def dotenv_files; end
  end
end

Note that the method is empty but it later gets overridden and the gem's behavior remains unaffected. I was not able to pinpoint what exactly causes the issue with this version.

@softmonkeyjapan
Copy link

softmonkeyjapan commented Jul 12, 2017

I'm having a similar issue on my staging server. The server is using the following:

Ruby 2.2.5
Rails 5.1.1
dotenv 2.2.1
unicorn 5.3.0

When starting my unicorn server, the log shows the following error message:

I, [2017-07-12T00:01:23.110852 #30507]  INFO -- : Refreshing Gem list
E, [2017-07-12T00:01:23.132283 #30495] ERROR -- : There was an error while trying to load the gem 'dotenv/rails-now'.
Gem Load Error is: couldn't find HOME environment -- expanding `~'
Backtrace for gem load error is:
/usr/local/rvm/gems/ruby-2.2.5/gems/spring-2.0.2/lib/spring/commands.rb:28:in `expand_path'
/usr/local/rvm/gems/ruby-2.2.5/gems/spring-2.0.2/lib/spring/commands.rb:28:in `<module:Spring>'
/usr/local/rvm/gems/ruby-2.2.5/gems/spring-2.0.2/lib/spring/commands.rb:4:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.5/gems/dotenv-rails-2.2.1/lib/dotenv/rails.rb:20:in `require'
/usr/local/rvm/gems/ruby-2.2.5/gems/dotenv-rails-2.2.1/lib/dotenv/rails.rb:20:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.5/gems/dotenv-rails-2.2.1/lib/dotenv/rails-now.rb:9:in `require'
/usr/local/rvm/gems/ruby-2.2.5/gems/dotenv-rails-2.2.1/lib/dotenv/rails-now.rb:9:in `<top (required)>'

However, when I manually downgrade dotenv to version 2.1.x, everything is working as expected. The problem is that I cannot use dotenv 2.1.x as Rails 5.1 is not supported on that version.

Any idea of why this is happening?

Update

It seems that in my case, the break comes from this commit #246. Version 2.1.0 does work but not the above (from 2.1.1).

@stale stale bot added the wontfix label Sep 10, 2017
@stale
Copy link

stale bot commented Sep 10, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Sep 17, 2017
@n0nick
Copy link

n0nick commented Sep 17, 2017

Can we please reopen this? This bug was reported for version 2.2.1 which is the latest, so I think it's still relevant.

@jaydorsey
Copy link
Contributor

jaydorsey commented Sep 26, 2017

I'm running into a similar error w/ 2.2.1 (latest), minus the rails-now part.

I'm deploying to an elastic beanstalk environment on AWS, and the error occurs when running a bundle exec command during deployment. There may be other issues at hand (permissions, context, AWS specific things, etc.) but I do seem to be able to reproduce this error (at least with my production code). Happy to provide more detail if necessary.

Things that didn't work:

  • Setting the DISABLE_SPRING environment variable (spring isn't even bundled or on the system)
  • Including an empty .env file
  • Removing BUNDLE_WITHOUT

Things that do work:

  • SSH'ing into the instance and running the command manually
  • Downgrading to 2.1.1
  • Prepending HOME=/var/app/ondeck to my bundle exec rake command (this is what I'm using to deploy)

Assuming it's got something to do with the scripts running as root and not having a HOME environment variable set (just like the error message says)

Environment:

Ruby 2.3.4
Rails 4.2.7.1
Bundler 1.15.4
Rake 12.1.0

Backtrace

rake aborted!
  Bundler::GemRequireError: There was an error while trying to load the gem 'dotenv-rails'.
  Gem Load Error is: couldn't find HOME environment -- expanding `~'
  Backtrace for gem load error is:
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/commands.rb:28:in `expand_path'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/commands.rb:28:in `<module:Spring>'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/spring-2.0.2/lib/spring/commands.rb:4:in `<top (required)>'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/dotenv-rails-2.2.1/lib/dotenv/rails.rb:20:in `require'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/dotenv-rails-2.2.1/lib/dotenv/rails.rb:20:in `<top (required)>'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/dotenv-rails-2.2.1/lib/dotenv-rails.rb:1:in `require'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/dotenv-rails-2.2.1/lib/dotenv-rails.rb:1:in `<top (required)>'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:82:in `require'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:82:in `block (2 levels) in require'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:77:in `each'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:77:in `block in require'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:66:in `each'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:66:in `require'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler.rb:108:in `require'
  /var/app/ondeck/config/application.rb:15:in `<top (required)>'
  /var/app/ondeck/Rakefile:4:in `require'
  /var/app/ondeck/Rakefile:4:in `<top (required)>'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/rake_module.rb:29:in `load'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/rake_module.rb:29:in `load_rakefile'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:688:in `raw_load_rakefile'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:97:in `block in load_rakefile'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:179:in `standard_exception_handling'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:96:in `load_rakefile'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:80:in `block in run'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:179:in `standard_exception_handling'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/lib/rake/application.rb:78:in `run'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/bin/rake:23:in `load'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/bin/rake:23:in `<top (required)>'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:74:in `load'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:74:in `kernel_load'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:27:in `run'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/cli.rb:362:in `exec'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/cli.rb:22:in `dispatch'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/cli.rb:13:in `start'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/exe/bundle:30:in `block in <top (required)>'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
  /opt/rubies/ruby-2.3.4/lib/ruby/gems/2.3.0/gems/bundler-1.15.4/exe/bundle:22:in `<top (required)>'
  /opt/rubies/ruby-2.3.4/bin/bundle:23:in `load'
  /opt/rubies/ruby-2.3.4/bin/bundle:23:in `<main>'
  Bundler Error Backtrace:
  /var/app/ondeck/config/application.rb:15:in `<top (required)>'
  /var/app/ondeck/Rakefile:4:in `require'
  /var/app/ondeck/Rakefile:4:in `<top (required)>'
  /var/app/ondeck/vendor/bundle/ruby/2.3.0/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
  /opt/rubies/ruby-2.3.4/bin/bundle:23:in `load'
  /opt/rubies/ruby-2.3.4/bin/bundle:23:in `<main>'
  ArgumentError: couldn't find HOME environment -- expanding `~'

It looks like it is related to the commit noted above, because by switching from loading watcher to commands, this line was introduced.

I'm chalking it up to an environment issue created by not having that HOME path set, because File.expand_path dies when HOME isn't set. watcher didn't have any File.expand_path calls.

Rescuing ArgumentError Bundler::GemRequireError might be a reasonable fix (in addition to LoadError, here

If @bkeepers is amenable I can fork, test, and do a PR if that resolves it.

@mokolabs
Copy link

mokolabs commented Oct 5, 2017

I just ran into this bug, FWIW.

@heaven
Copy link
Author

heaven commented Nov 2, 2017

Just wanted to warn anyone about this hack:

module Dotenv
  class Railtie < Rails::Railtie
    def dotenv_files; end
  end
end

Don't use it as it will mess your environment. This hack breaks this rule:

dotenv-rails will load the following files, starting from the bottom. The first value set (or those already defined in the environment) take precedence:

And you will end up with your development keys in production.

To load it correctly we had to do the following to the application.rb:

module Dotenv
  class Railtie < Rails::Railtie
    def dotenv_files
      [
        root.join(".env.#{Rails.env}.local"),
        (root.join(".env.local") unless Rails.env.test?),
        root.join(".env.#{Rails.env}"),
        root.join(".env")
      ].compact
    end
  end
end

if defined?(Bundler)
  Bundler.require(:default, Rails.env)
end

Dotenv::Railtie.load

@zx1986
Copy link

zx1986 commented Dec 1, 2017

Gem Load Error is: non-absolute home

jzshopping    | rake aborted!
jzshopping    | Bundler::GemRequireError: There was an error while trying to load the gem 'dotenv-rails'.
jzshopping    | Gem Load Error is: non-absolute home
jzshopping    | Backtrace for gem load error is:
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/commands.rb:28:in `expand_path'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/commands.rb:28:in `<module:Spring>'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/commands.rb:4:in `<top (required)>'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/dotenv-rails-2.2.1/lib/dotenv/rails.rb:20:in `require'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/dotenv-rails-2.2.1/lib/dotenv/rails.rb:20:in `<top (required)>'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/dotenv-rails-2.2.1/lib/dotenv-rails.rb:1:in `require'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1/gems/dotenv-rails-2.2.1/lib/dotenv-rails.rb:1:in `<top (required)>'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler/runtime.rb:86:in `require'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler/runtime.rb:86:in `block (2 levels) in require'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `each'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `block in require'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `each'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `require'
jzshopping    | /usr/local/rvm/gems/ruby-2.3.1@global/gems/bundler-1.12.5/lib/bundler.rb:102:in `require'
jzshopping    | /home/app/config/application.rb:8:in `<top (required)>'
jzshopping    | /home/app/Rakefile:4:in `require'
jzshopping    | /home/app/Rakefile:4:in `<top (required)>'
Ruby 2.3.1p112
Rails 4.2.3
rake, version 12.0.0
Bundler version 1.12.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants