From 27eaa02d28cc07ceb59a51529100ee4b7016a38f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 9 May 2017 20:30:02 +0200 Subject: rails.rb: Check that `Rake` is loaded before loading Rake task Was getting an exception when trying to start the development server on a Rails 5.0.1 app: $ bundle exec rails s dbshell-rails/lib/dbshell/rails/tasks/dbshell.rake:5:in `': undefined method `desc' for main:Object (NoMethodError) from .../ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load' from .../ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load' from .../ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency' from .../ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load' from dbshell-rails/lib/dbshell/rails.rb:3:in `' from .../ruby/gems/2.3.0/gems/bundler-1.14.2/lib/bundler/runtime.rb:105:in `require' from .../ruby/gems/2.3.0/gems/bundler-1.14.2/lib/bundler/runtime.rb:105:in `rescue in block in require' from .../ruby/gems/2.3.0/gems/bundler-1.14.2/lib/bundler/runtime.rb:82:in `block in require' from .../ruby/gems/2.3.0/gems/bundler-1.14.2/lib/bundler/runtime.rb:75:in `each' from .../ruby/gems/2.3.0/gems/bundler-1.14.2/lib/bundler/runtime.rb:75:in `require' from .../ruby/gems/2.3.0/gems/bundler-1.14.2/lib/bundler.rb:107:in `require' from rails-app/config/application.rb:7:in `' from .../ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:88:in `require' from .../ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:88:in `block in server' from .../ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap' from .../ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:85:in `server' from .../ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!' from .../ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `' from rails-app/bin/rails:9:in `require' from rails-app/bin/rails:9:in `' from .../ruby/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `load' from .../ruby/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `call' from .../ruby/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/command.rb:7:in `call' from .../ruby/gems/2.3.0/gems/spring-2.0.1/lib/spring/client.rb:30:in `run' from .../ruby/gems/2.3.0/gems/spring-2.0.1/bin/spring:49:in `' from .../ruby/gems/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `load' from .../ruby/gems/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `' from rails-app/bin/spring:15:in `require' from rails-app/bin/spring:15:in `' from bin/rails:3:in `load' from bin/rails:3:in `
' Looks like Rails is trying to load the task without Rake, and the interpreter doesn't know what to do with the Rake methods. Follow the pattern in https://github.com/paulelliott/fabrication/blob/b470cd817e1973c14e3253cd76606cf934942ca1/lib/fabrication.rb and guard the load with a check for whether `Rake` is defined. This makes the server start up and work as expected again. --- lib/dbshell/rails.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/dbshell/rails.rb b/lib/dbshell/rails.rb index 0e18f6b..1b60b48 100644 --- a/lib/dbshell/rails.rb +++ b/lib/dbshell/rails.rb @@ -1,6 +1,8 @@ require 'dbshell/rails/version' -load 'dbshell/rails/tasks/dbshell.rake' +if defined?(Rake) + load 'dbshell/rails/tasks/dbshell.rake' +end module DBShell module Rails -- cgit v1.2.3