Search code examples
ruby-on-railsguard

guard-rails mysteriously restarting


I upgraded from rails-4.2.7.1 to 5.0.1, now when I run guard it restarts itself for some reason. This causes it to try to start a second instance of Rails, but dies because there's already one running.

» bundle exec guard
Expected string default value for '--listen-on'; got false (boolean)
17:57:54 - INFO - Bundle already up-to-date
17:57:54 - INFO - [Guard::Rails] will start the default web server on port 3000 in development.
17:57:54 - INFO - Starting Rails...
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from <top (required)> at /home/istrasci/dev/rails/credalizer/config/application.rb:9)
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.2 (ruby 2.3.3-p222), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
17:58:03 - INFO - Rails started, pid 9732
17:58:03 - INFO - Guard::RSpec is running
17:58:03 - INFO - [Guard::Yard] Stopping YARD Documentation Server.
17:58:03 - INFO - [Guard::Yard] Starting YARD Documentation Server.
17:58:03 - INFO - LiveReload is waiting for a browser to connect.
>> YARD 0.9.5 documentation server at http://localhost:8808
Puma starting in single mode...
* Version 3.6.2 (ruby 2.3.3-p222), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:8808
Use Ctrl-C to stop
17:58:04 - INFO - [Guard::Yard] Server successfully started.
* Restarting...     <------------------------------------------- HERE
17:58:06 - INFO - Guard is now watching at '/home/istrasci/dev/rails/credalizer'
[1] guard(main)> DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from <top (required)> at /home/istrasci/dev/rails/credalizer/config/application.rb:9)
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
A server is already running. Check /home/istrasci/dev/rails/credalizer/tmp/pids/development.pid.
Exiting

Then the instance that was running doesn't respond, so I have to manually delete this tmp/pids/development.pid file or exit from the guard prompt. My guard-rails worked fine in Rails 4. Anyone know why this is happening?


Here's my Guardfile

notification :gntp if RUBY_PLATFORM =~ /darwin/i
notification :libnotify if RUBY_PLATFORM =~ /linux/i

guard :bundler do
  watch('Gemfile')
end

guard 'rails' do
  watch('Gemfile.lock')
  watch(%r{^(config|lib)/.*})
end

guard :rspec, cmd: 'bin/rspec' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  watch('spec/rails_helper.rb')                       { "spec" }

  # FactoryGirl factory files
  begin
    require 'active_support/inflector'
    watch(%r{^spec/factories/(.+)\.rb$})      { |m| "spec/models/#{m[1].singularize}_spec.rb" }
    watch(%r{^spec/factories/document_data.rb}) { |m| "spec/models/document_data_spec.rb" }
  rescue LoadError
  end

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$})     { |m| "spec/features/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

guard 'livereload' do
  watch(%r{app/views/.+\.(erb|haml|slim)$})
  watch(%r{^app/assets/.+\.(s?css|js)})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{public/.+\.(css|js|html)})
  watch(%r{config/locales/.+\.yml})
  # Rails Assets Pipeline
  watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
end

guard 'yard' do
  watch(%r{app/.+\.rb})
  watch(%r{lib/.+\.rb})
  watch(%r{ext/.+\.c})
end

Solution

  • Hi I'm the maintainer of guard-rails. As my test, the minimal buggy Guardfile looks like:

    guard 'rails'
    guard 'yard'
    

    The issue is that, for reason unknown, guard-yard stop and restart Puma server during starting. Which is the default dev server introduced in Rails 5.0. If rails has already started, it will kill the server and cause it to restart but failed.

    What you need to do is simply swap them:

    guard 'yard'
    guard 'rails'
    

    In fact, I believe guard-yard should not kill rails server by all means.