rails / spring

Rails application preloader
MIT License
2.81k stars 341 forks source link

config/spring.rb not loaded if used within engine #144

Closed robinboening closed 10 years ago

robinboening commented 11 years ago

In contrast to #37 the config/spring.rb is not loaded within an engine.

I followed the instructions from the readme and created the config/spring.rb.

The engine´s dummy application is located in spec/dummy, so I configured the application_root like that:

Spring.application_root = './spec/dummy'

Unfortunately without success and I got the error notice:

Spring was unable to find your config/application.rb file. Your project root was detected at /Users/robin/code/ruby/gems/alchemy_cms, so spring looked for /Users/robin/code/ruby/gems/alchemy_cms/config/application.rb but it doesn't exist. You can configure the root of your application by setting Spring.application_root in config/spring.rb.

Did I miss something? If so, it should be mentioned in the readme.

tvdeyen commented 11 years ago

Some for me. Using spring 0.0.8 from rubygems.org. And even tried to clone the github repo and installed the gem locally. Same issue.

I put the spring.rb in GEM_ROOT/config/spring.rb and in GEM_ROOT/spec/dummy/config/spring.rb

Neither works.

So what are we missing? Maybe @senny can clarify?

senny commented 11 years ago

From the error it looks like your configuration file is not found and therefore the application_root option is not specified. The code to load the config file is at: https://github.com/jonleighton/spring/blob/master/lib/spring/commands.rb#L241-L242

How are you executing spring? From what pwd?

senny commented 11 years ago

As a side note, the config file goes in:

<engine_root>/config/spring.rb

tvdeyen commented 11 years ago

Yes, from the gem root. In my case:

/Users/tvd/code/ruby/gems/alchemy_cms

tvdeyen commented 11 years ago

Yep, and this doesn't work.

tvdeyen commented 11 years ago

Github mail parsing.... :(

senny commented 11 years ago

@tvdeyen please also post the command you are executing...

tvdeyen commented 11 years ago

spring rspec

robinboening commented 11 years ago

But also just spring will throw the message.

tvdeyen commented 11 years ago

I puts'ed the config = File.expand_path("./config/spring.rb") from https://github.com/jonleighton/spring/blob/master/lib/spring/commands.rb#L241 and it does not was printed.

So I guess, the MissingApplication error gets raised to soon. Before the config file is even evaluated.

tvdeyen commented 11 years ago

@senny is it working for you? In an gem engine? Can you post an example of the usage?

senny commented 11 years ago

thanks. I'm at work right now but I'll take a look when I have some spare time.

tvdeyen commented 11 years ago

I am also at work right now ;) And I want to write some tests...

Have you an working example of your implementation?

tvdeyen commented 11 years ago

My guess is it that while instantiating Spring.Application.new the problem starts, because it's constructor parameter defaults to Spring.watcher and that calls the Spring.application_root_path method.

https://github.com/jonleighton/spring/blob/master/lib/spring/application.rb#L11

https://github.com/jonleighton/spring/blob/master/lib/spring/watcher.rb#L14

Maybe the config.rb file gets evaluated to late. It has to be loaded before Spring::Server starts.

senny commented 11 years ago

I finally had time to take a look at this one. It also didn't work for me with the released spring 0.0.8 but it works if you use the current master version from GitHub.

@jonleighton is there a new release planned?

tvdeyen commented 11 years ago

Unfortunately it doesn't work for me if I install the gem locally from cloned github master. Neither it works from bundler with local path option.

Maybe I miss something?

Thanks for looking into it.

senny commented 11 years ago

@tvdeyen can you make a fresh empty engine and wire up spring and upload it to github?

tvdeyen commented 11 years ago

Sorry, not at work.

But this is easy as 1,2,3:

$ rails new plugin spring_test --mountable --dummy-path=spec/dummy -T
$ spring rspec
senny commented 11 years ago

@tvdeyen ping me when you uploaded an application. I tried to reproduce locally and it worked.

tvdeyen commented 11 years ago

Please clone:

https://github.com/magiclabs/alchemy_cms

Then run:

$ bundle install
$ rake alchemy:spec:prepare
$ spring rspec
senny commented 11 years ago

This application does not even have spring configured => https://github.com/magiclabs/alchemy_cms/tree/master/config

I need an isolated engine with spring configured that fails on your machine.

tvdeyen commented 11 years ago

Yeah, you are right. Sorry about that. I am managing this via mobile phone while I am on vacancy.

This isolated engine is the one failing.

Couldn't you just put in a config/spring.rb that points to spec/dummy? That would be very much appreciated.

Many thanks for your patience. Next beer is on me :)

senny commented 11 years ago

This isolated engine is the one failing.

There is no link...

Of course I can post an example config/spring.rb but it seems that the contents of this file is not the problem:

config/spring.rb

Spring.application_root = './test/dummy'
senny commented 11 years ago

My time is limited, I will continue looking into this, when someone submits an isolated (blank rails engine) with spring configured that is not working for them locally.

tvdeyen commented 11 years ago

This isolated engine is the one failing.

There is no link...

https://github.com/magiclabs/alchemy_cms Of course I can post an example config/spring.rb but it seems that the contents of this file is not the problem:

config/spring.rb

Spring.application_root = './test/dummy' ./spec/dummy

senny commented 11 years ago

@tvdeyen As stated before. I'm not going to configure the engine you posted. It has a huge load of dependencies. I'm looking for a blank rails engine with Spring configured that fails on your machine. Upload that and I'll take another look.

robinboening commented 11 years ago

@senny you can use this one

$ git clone git@github.com:robinboening/spring_test.git && cd spring_test
$ spring rspec

This ends up with the error notice, while calling rspec just works. (Using manually installed spring gem from cloned github repository)

tvdeyen commented 11 years ago

<3 @robinboening

senny commented 11 years ago

@robinboening thanks for the engine. My verification against the local git used an older version, which is why it worked. Does #147 work for you?

tvdeyen commented 11 years ago

147 works with the bare engine from @robinboening, but it fails with the alchemy engine with this error:

$ spring rspec
You are using an old or stdlib version of json gem
Please upgrade to the recent version by adding this to your Gemfile:

  gem 'json', '~> 1.7.7'

/usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize': ArgumentError: prepare called on a closed database:           SELECT name (ActiveRecord::StatementInvalid)
          FROM sqlite_master
          WHERE type = 'table' AND NOT name = 'sqlite_sequence'
 AND name = "alchemy_attachments"
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `prepare'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:246:in `block in exec_query'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:335:in `tables'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:341:in `table_exists?'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/schema_cache.rb:30:in `table_exists?'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:223:in `table_exists?'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb:5:in `included'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:98:in `include'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:98:in `taggable_on'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:39:in `acts_as_taggable_on'
    from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:15:in `acts_as_taggable'
    from /Users/tvd/code/ruby/gems/alchemy_cms/app/models/alchemy/attachment.rb:6:in `<class:Attachment>'
...

Running it only with rspec it works fine.

So I guess there is an issue with gem environment while running with spring?

robinboening commented 11 years ago

Same here.

147 works with the test app, but using the alchemy_cms engine it fails with the same error as @tvdeyen said.

Am 22.05.2013 um 21:49 schrieb Thomas von Deyen notifications@github.com:

147 works with the bare engine from @robinboening, but it fails with the alchemy engine with this error:

$ spring rspec You are using an old or stdlib version of json gem Please upgrade to the recent version by adding this to your Gemfile:

gem 'json', '~> 1.7.7'

/usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in initialize': ArgumentError: prepare called on a closed database: SELECT name (ActiveRecord::StatementInvalid) FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' AND name = "alchemy_attachments" from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:innew' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in prepare' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:246:inblock in exec_query' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in block in log' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:ininstrument' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in log' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:242:inexec_query' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:335:in tables' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:341:intable_exists?' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/schema_cache.rb:30:in table_exists?' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:223:intable_exists?' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb:5:in included' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:98:ininclude' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:98:in taggable_on' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:39:inacts_as_taggable_on' from /usr/local/var/rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:15:in acts_as_taggable' from /Users/tvd/code/ruby/gems/alchemy_cms/app/models/alchemy/attachment.rb:6:inclass:Attachment' ... Running it only with rspec it works fine.

So I guess there is an issue with gem environment while running with spring?

— Reply to this email directly or view it on GitHub.

senny commented 11 years ago

This failure feels unrelated, to this issue. From the stack-trace it looks like acts-as-taggable is somehow involved. I would need to debug this. The fact that you no longer get the error reported in this ticket, means, that it in fact works for both projects.

tvdeyen commented 11 years ago

Sorry, I don't think this can be closed, because as stated above, the error only raises with spring command, not if running alone with rspec command.

Maybe this is somehow Gemfile related? According to your commit you ignore the engines Gemfile.

Hopefully I find some time tonight to look into this and will give you feedback.

Thanks so far for your help.

senny commented 11 years ago

@tvdeyen this issue is about "config/spring.rb not loaded if used within engine" with the referenced patch, this file is loaded so we are going to close it when the patch gets merged.

Please open a new issue for the exception related to acts-as-taggable it's not related to this discussion.

tvdeyen commented 11 years ago

You are right. I will open a new issue after this is merged, because I believe this was introduced by your patch.

senny commented 11 years ago

@tvdeyen why do you think it's related to this patch?

tvdeyen commented 11 years ago

So, I got it working, somehow.

This is what I did, to get this working:

  1. Installed the latest 0.0.9 version of spring gem
  2. Created a new file spring.rb in ./config/ of my engine root dir (~/code/ruby/gems/alchemy_cms)
  3. Added Spring.application_root = 'spec/dummy' into ./config/spring.rb

Calling the spring command I got the same error like before:

Spring was unable to find your config/application.rb file. Your project root was detected at /Users/tvd/code/ruby/gems/alchemy_cms, so spring looked for /Users/tvd/code/ruby/gems/alchemy_cms/config/application.rb but it doesn't exist. You can configure the root of your application by setting Spring.application_root in config/spring.rb.
  1. I made a symbolic link for spec/dummy/config/application.rb into ./config/
  2. I made a symbolic link for ./Gemfile into ./spec/dummy/
  3. I made a symbolic link for ./Gemfile.lock into ./spec/dummy/

Now it works.

PS: I fixed the You are using an old or stdlib version of json gem error with using gem 'multi_json', '1.7.2'. Like mentioned in http://stackoverflow.com/q/16543693

tvdeyen commented 11 years ago

Thanks @jonleighton this commit fixes all issues. Now it works :heart:

elbbub commented 11 years ago

I installed the latest version of spring, but still got this same error,

/home/elbbub/.rvm/gems/ruby-2.0.0-p247/bundler/gems/spring-38214cfd773d/lib/spring/configuration.rb:26:in application_root_path': Spring was unable to find your config/application.rb file. Your project root was detected at /home/elbbub/D.works/projects/_plugins/blorgh4m_ori_test, so spring looked for /home/elbbub/D.works/projects/_plugins/blorgh4m_ori_test/config/application.rb but it doesn't exist. You can configure the root of your application by setting Spring.application_root in config/spring.rb. (Spring::MissingApplication) from /home/elbbub/.rvm/gems/ruby-2.0.0-p247/bundler/gems/spring-38214cfd773d/lib/spring/configuration.rb:20:inverify_environment' . . . my ruby version is 2.0-p247, rails is 4

tvdeyen commented 11 years ago

Yep, same for me using Spring 1.0.0, Rails 4 and Ruby 2.0.0-p247 ;(

jonleighton commented 11 years ago

@elbbub @tvdeyen can you guys provide an example plugin that shows how to reproduce the problem?

tvdeyen commented 10 years ago

Sure. I made a vanilla rails 4 demo engine:

$ rails --version
4.0.2

$ rails plugin new spring_1_0_0_test

Got the same error:

Spring was unable to find your config/application.rb file.

$ gem list spring

*** LOCAL GEMS ***

spring (1.0.0)
spring-commands-rspec (1.0.1)
$ ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
$ gem --version
2.1.10
$ rspec spec/
..*.........................*.

Finished in 0.26027 seconds
30 examples, 0 failures, 2 pending
$ spring rspec
Spring was unable to find your config/application.rb file. Your project root was detected at /Users/tvd/code/ruby/gems/spring_1_0_0_test, so spring looked for /Users/tvd/code/ruby/gems/spring_1_0_0_test/config/application.rb but it doesn't exist. You can configure the root of your application by setting Spring.application_root in config/spring.rb.

See for yourself: http://github.com/tvdeyen/spring_1_0_0_test

@jonleighton could you please reopen this issue? Since spring is going to be merged into rails with 4.1 I think this really has to be fixed first ;)

jonleighton commented 10 years ago

@tvdeyen sure, I look forward to seeing your pull request

zigomir commented 10 years ago

I did a little of investigation. Attribute application_root in configuration.rb file will be null, because configuration gets executed before commands.rb where we look into ~/.spring.rb or ./config/spring.rb. So, if I just put loading of config file into configuration.rb it will work, but that's probably not OK, because this gets executed every time you run spring command...

I'm totally new to this gem and I don't really know how exactly should everything work.

atroche commented 10 years ago

Running Rails 3.2.15, everything works fine when I follow these steps:

rails plugin new spring_test
cd spring_test
echo 'gem "spring", :group => :development' >> Gemfile
bundle install
mkdir config
echo "Spring.application_root = './test/dummy'" > config/spring.rb
bundle exec spring binstub --all

But that's because binstub is explicitly requiring "spring/commands" before it gets called. It might fail with other commands. I'm going to try some others and see if I can make it break…

atroche commented 10 years ago

Correct me if I'm wrong:

In 16f5a24b776ac70e79835f9c5621d150ad5d0dc3 @jonleighton stopped application.rb from requiring "spring/commands", the file responsible for requiring the config files, because it would load in all the custom command gems whenever the client runs, and that's slow.

However, since then the config files have started being used to manually set the application_root_path, which is necessary if you're running the spring client in an engine.

When the client runs it checks to see if the server is running, and boots it if necessary. The boot_server method requires "spring/server", which requires "spring/commands", so it's fine. The problem is the unless env.server_running?, which is run before we require spring/commands, and hence before we have application_root_path.

To check if the server is running, it has to check the pidfile, which is named after the application_id, which is a hex digest of the application_root_path.

So! What are the potential solutions? We need the client to be able to check the status of the server without loading custom command gems. We could:

1) Make the pidfile not dependent on the application_root_path 2) Have separate config files (or separate sections in the same file) for loading custom commands vs. configuring options

Thoughts?

atroche commented 10 years ago

Or we could autodetect when there's a dummy app and use that as the application root path...

jonleighton commented 10 years ago

Thanks @atroche. This should be fixed by the above commit.

tvdeyen commented 10 years ago

Confirmed working \o/

Thanks @jonleighton :heart: