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

warning: already initialized constant APP_PATH #259

Closed
janko-m opened this Issue Feb 13, 2014 · 38 comments

Comments

Projects
None yet
@janko-m
Contributor

janko-m commented Feb 13, 2014

I'm getting a warning: already initialized constant APP_PATH error, coming from bin/rails. I'm using the latest Spring and Rails 4.

@jonleighton

This comment has been minimized.

Member

jonleighton commented Feb 23, 2014

Did you regenerate your binstubs for Spring 1.1?

@janko-m

This comment has been minimized.

Contributor

janko-m commented Feb 23, 2014

Yes. I first ran it on 1.0, and then when I upgraded to 1.1 I ran it again just in case (and nothing changed).

@jonleighton

This comment has been minimized.

Member

jonleighton commented Feb 24, 2014

If nothing changed between 1.0 and 1.1 then something is wrong - the 1.1 binstubs are totally different to 1.0.

@janko-m

This comment has been minimized.

Contributor

janko-m commented Feb 24, 2014

I get this output on spring binstub --all:

* bin/rake: spring already present
* bin/rspec: spring already present
* bin/rails: spring already present

So, Spring probably saw 1.0 binstubs, and then concluded that binstubs are already present. This is my bin/rails:

#!/usr/bin/env ruby

begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'
require 'rails/commands'
@jonleighton

This comment has been minimized.

Member

jonleighton commented Feb 24, 2014

Ok, you actually have a 1.1 binstub there, which is why nothing happens. Spring upgrades 1.0 binstubs if they're present.

I'm not sure why this is happening so if you can try to investigate and provide more information that would be helpful as there's not much to go on. Two possible theories are:

  • Something else is defining APP_PATH
  • The APP_PATH line in your bin/rails is getting run twice for some reason
@janko-m

This comment has been minimized.

Contributor

janko-m commented Feb 24, 2014

Yes, I'm sorry. So, bin/rails is ran twice. I get it, for example, when I exit the rails server; upon exiting bin/rails is ran again for some reason.

@jonleighton

This comment has been minimized.

Member

jonleighton commented Feb 24, 2014

Ok, if you stick a puts caller in there we should hopefully see why...

@janko-m

This comment has been minimized.

Contributor

janko-m commented Feb 24, 2014

On exit the caller is empty. I'm not sure what that means.

@janko-m

This comment has been minimized.

Contributor

janko-m commented Feb 24, 2014

I tested that it happens with a brand new unchanged Rails 4.0 application, so I don't know why I'm the only one getting this warning.

@jonleighton

This comment has been minimized.

Member

jonleighton commented Feb 24, 2014

Aha, yes I can repro:

$ bin/rails s
=> Booting WEBrick
=> Rails 4.1.0.rc1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-02-24 08:39:22] INFO  WEBrick 1.3.1
[2014-02-24 08:39:22] INFO  ruby 2.0.0 (2013-06-27) [x86_64-linux]
[2014-02-24 08:39:22] INFO  WEBrick::HTTPServer#start: pid=4278 port=3000
^C[2014-02-24 08:39:26] INFO  going to shutdown ...
[2014-02-24 08:39:26] INFO  WEBrick::HTTPServer#start done.
Exiting
bin/rails:6: warning: already initialized constant APP_PATH
/home/turnip/Code/spring/test/apps/tmp/bin/rails:6: warning: previous definition of APP_PATH was here

Note that the rails server is supposed to bypass spring so that's probably something to do with it. (I don't use rails server so hadn't noticed this myself.)

@janko-m

This comment has been minimized.

Contributor

janko-m commented Feb 26, 2014

Thank you :)

@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented Apr 7, 2014

Btw. the same error is thrown if there is a LoadError inside config/routes.rb, which is pretty confusing.

@DammersH

This comment has been minimized.

DammersH commented Apr 12, 2014

May I add that this error will be thrown even before the server can start?

@toabui

This comment has been minimized.

toabui commented May 2, 2014

I'm having the same problem here too.

When I run rails server

I get the following error before the server stars as DammersH commented. Any help?

bin/rails:6: warning: already initialized constant APP_PATH
/Users/toabui/Sites/cms/bin/rails:6: warning: previous definition of APP_PATH was here
Usage: rails COMMAND [ARGS]

my bin/rails looks like this:

#!/usr/bin/env ruby
begin
load File.expand_path("../spring", FILE)
rescue LoadError
end
APP_PATH = File.expand_path('../../config/application', FILE)
require_relative '../config/boot'
require 'rails/commands'

@janko-m

This comment has been minimized.

Contributor

janko-m commented May 2, 2014

What version of Spring? It never happened to me again after I upgraded, and I use Spring all the time.

@toabui

This comment has been minimized.

toabui commented May 2, 2014

I'm running spring 1.1.2

$ spring -v
Spring version 1.1.2

I've tried deleting all the gems and reinstalling them but I'm still getting the same problem:

I am inside my app directory when I run the commands.

Toas-MacBook-Pro:cms toabui$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]
Toas-MacBook-Pro:cms toabui$ rails -v
Rails 4.1.0
Toas-MacBook-Pro:cms toabui$ spring -v
Spring version 1.1.2
Toas-MacBook-Pro:cms toabui$ mysql --version
mysql Ver 14.14 Distrib 5.6.17, for osx10.7 (x86_64) using EditLine wrapper
Toas-MacBook-Pro:cms toabui$ rails server
bin/rails:6: warning: already initialized constant APP_PATH
/Users/toabui/Sites/cms/bin/rails:6: warning: previous definition of APP_PATH was here
Usage: rails COMMAND [ARGS]

@toabui

This comment has been minimized.

toabui commented May 2, 2014

When I run spring binstub --all, says it's already present.

$ spring binstub --all

  • bin/rake: spring already present
  • bin/rails: spring already present
@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented May 2, 2014

If you are using rails 4+ and still have bundler binstubs, you need to regenerate your binstubs using the following rake command:

rake rails:update:bin
spring binstub --all

Current rails master (4.2.0) actually complains about this at startup.
After re-generating the binstubs the warning should go away.

@toabui

This comment has been minimized.

toabui commented May 3, 2014

Thank Felix,

When I go to do rake rails:update:bin, there was this error:

rails:update:bin
rake aborted!
LoadError: dlopen(/Library/Ruby/Gems/2.0.0/extensions/universal-darwin-13/2.0.0/mysql2-0.3.15/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Ruby/Gems/2.0.0/extensions/universal-darwin-13/2.0.0/mysql2-0.3.15/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/2.0.0/extensions/universal-darwin-13/2.0.0/mysql2-0.3.15/mysql2/mysql2.bundle

After searching the error, libmysqlclient.18.dylib, I came upon this solution.

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

After that I was able to run

rails:update:bin

And now rails s works. Web Brick is running.

Thanks

@archonic

This comment has been minimized.

archonic commented May 17, 2014

I'm running rails 4.10 with spring 1.1.3 and I'm still getting the APP_PATH error. Running rake rails:update:bin returns

exist  bin
identical  bin/bundle
conflict  bin/rails

Then asks if I want to overwrite. Regardless of the response it continues with the APP_PATH error after. This is what spring binstub --all returns:

* bin/rake: spring already present
* bin/rspec: spring already present
* bin/rails: spring already present

Update: I removed gem 'spring' from my Gemfile and /active_record/connection_adapters/connection_specification.rb was complaining about gem 'pg' '~> 0.17.1'. After switching to sqlite3, I could put spring back in and run the server. Not sure why...

@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented May 18, 2014

The error tends to crop up, whenever something keeps your app from starting correctly. I tend to just start rails console to see the actual error…

@koenpunt

This comment has been minimized.

koenpunt commented May 20, 2014

For me it crashed with the following output:

$ bin/rails server
=> Booting Thin
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Exiting
bin/rails:6: warning: already initialized constant APP_PATH
/private/shares/apps/groene11/bin/rails:6: warning: previous definition of APP_PATH was here
Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short-cut alias: "g")
 console     Start the Rails console (short-cut alias: "c")
 server      Start the Rails server (short-cut alias: "s")
 dbconsole   Start a console for the database specified in config/database.yml
             (short-cut alias: "db")
 new         Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

In addition to those, there are:
 application  Generate the Rails application code
 destroy      Undo code generated with "generate" (short-cut alias: "d")
 plugin new   Generates skeleton for developing a Rails plugin
 runner       Run a piece of code in the application environment (short-cut alias: "r")

All commands can be run with -h (or --help) for more information.

But when I tried to run rake rails:update:bin the culprit revealed itself:

rake rails:update:bin
/Users/koenpunt/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require': cannot load such file -- mini_magick (You may need to install the mini_magick gem) (LoadError)
    from /Users/koenpunt/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/koenpunt/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:230:in `block in load_dependency'
    from /Users/koenpunt/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:633:in `new_constants_in'
    from /Users/koenpunt/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:230:in `load_dependency'
....

When I added the gem (mini_magick) to the Gemfile it was working fine again.

Still strange it aborts with a non-descriptive error though.

@dwayne

This comment has been minimized.

dwayne commented May 20, 2014

@felixbuenemann That is indeed what happened to me. I was requiring a gem in an initializer that I did not have installed and it crashed with the same output as @koenpunt. I ran bin/rails c and immediately saw the actual error. Thanks @felixbuenemann.

@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented May 20, 2014

@jonleighton Do you have an idea how to show the real error in these cases? This is bound to cause lots of false bug reports.

Easy way to reproduce:

rails new foo
cd foo
echo 'require "foofoo"' > config/initializers/foo.rb
bin/rails server
# Error: foo/bin/rails:6: warning: previous definition of APP_PATH was here
bin/spring binstub --remove rails
bin/rails server
# Error: lib/active_support/dependencies.rb:247:in `require': cannot load such file -- foofoo (LoadError)
@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented May 20, 2014

I noticed that the LoadError gets supressed by spring in lib/spring/client/binstub.rb:17 where it rescues LoadError unconditionally.

@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented May 20, 2014

@jonleighton How about using something like this in the binstubs:

begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError => e
  raise unless e.message =~ /spring/
end
@mikeborg

This comment has been minimized.

mikeborg commented Jun 5, 2014

I got the error because i typed "require 'obstruct'" instead of "require 'ostruct'" in a config/initializer.

@craigsheen

This comment has been minimized.

craigsheen commented Jun 10, 2014

Just had this issue too.

Running ruby rake rails:update:bin revealed more in terms of the error and it turns out it was a gem causing this issue. Commented the gem out and now working fine. (The gem was debugger for those interested)

@jemminger

This comment has been minimized.

jemminger commented Jun 17, 2014

Was happening to me too, it turned out to be that I had required "pg" in my Gemfile but still had "sqlite3" in my database.yml.

I didn't get an error about the missing gem until I re-ran rake rails:update:bin WITHOUT re-running spring binstub --all, and then tried to start the server.

Updating my database.yml to "postgresql" fixed the problem.

@tylercollier

This comment has been minimized.

tylercollier commented Jun 27, 2014

Thanks @dwayne. The way to see the actual error is running rails console.

@philipgiuliani

This comment has been minimized.

philipgiuliani commented Jul 28, 2014

Thanks @felixbuenemann for your snippet. Now i finally know why its showing this message.

@ghost

This comment has been minimized.

ghost commented Jan 19, 2015

Make sure spring is not running. I had to

ps aux | grep spring

to find the spring running from a previous instance of the program, then kill the running process. Once I did that the error went away.

@jkoisch

This comment has been minimized.

jkoisch commented Feb 3, 2015

+1 for @felixbuenemann's proposal ... my error was caused by a gem I am building locally not loading dependencies correctly. I discovered the error by running

 rake rails:update:bin

which showed the real problem.

@liwei78

This comment has been minimized.

liwei78 commented Feb 6, 2015

update to Rails 4.2.0 with rake rails:update:bin solve the error already initialized constant APP_PATH

@prusswan

This comment has been minimized.

prusswan commented Mar 31, 2015

@felixbuenemann is correct. This "error" can be caused by faulty initializers or things that have nothing to with the error description at all.

@Phaze1D

This comment has been minimized.

Phaze1D commented Nov 14, 2015

rake rails:update:bin
rake aborted!
LoadError: dlopen(/Users/david/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /Users/david/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/david/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/lib/mysql2/mysql2.bundle
/Users/david/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/lib/mysql2.rb:31:in require' /Users/david/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/lib/mysql2.rb:31:in<top (required)>'
/Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in require' /Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:inblock (2 levels) in require'
/Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in each' /Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:inblock in require'
/Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in each' /Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:inrequire'
/Users/david/.rvm/gems/ruby-2.2.1/gems/bundler-1.10.6/lib/bundler.rb:134:in require' /Users/david/Projects/SPS/Devrag/DevragWeb/config/application.rb:7:in<top (required)>'
/Users/david/Projects/SPS/Devrag/DevragWeb/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)

this is caused because i update mysql and i solved it by gem uninstall mysql2 then bundle install

felixbuenemann added a commit to felixbuenemann/spring that referenced this issue Nov 16, 2015

Don't supress non-spring load errors in binstub
Without this change, the spring binstub supresses unrelated load errors,
eg. due to errors in initializers or loaded gems. This change causes the
binstub to only ignore a missing spring binary or gem.

This closes rails#370 and resolves rails#259.
@felixbuenemann

This comment has been minimized.

Contributor

felixbuenemann commented Nov 19, 2015

After upgrading to spring 1.4.4 non-spring load errors should no longer be swallowed. Don't forget to run bin/spring binstub --all to update your binstubs to the new loader code.

@ravistm

This comment has been minimized.

ravistm commented Apr 26, 2016

I got it solved this way -> Just uninstall bundler and reinstall, then it will work like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment