tupl-tufts / rdl

Types, type checking, and contracts for Ruby
BSD 3-Clause "New" or "Revised" License
602 stars 38 forks source link

Support partial Rails environments? #59

Open rmosolgo opened 7 years ago

rmosolgo commented 7 years ago

Hi, I started to try RDL in my project, but I hit a runtime error:

/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:85:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'rdl'. (Bundler::GemRequireError)
Gem Load Error is: uninitialized constant ActiveRecord
Backtrace for gem load error is:
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rdl-2.1.0/lib/types/rails/active_record/associations.rb:63:in `<top (required)>'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rdl-2.1.0/lib/rdl/boot_rails.rb:6:in `block in <top (required)>'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rdl-2.1.0/lib/rdl/boot_rails.rb:6:in `each'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rdl-2.1.0/lib/rdl/boot_rails.rb:6:in `<top (required)>'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rdl-2.1.0/lib/rdl.rb:3:in `require'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rdl-2.1.0/lib/rdl.rb:3:in `<top (required)>'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:82:in `require'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:82:in `block (2 levels) in require'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `each'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `block in require'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `each'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `require'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler.rb:108:in `require'
/Users/rmosolgo/code/graphql-ruby/tmp/dummy/config/application.rb:17:in `<top (required)>'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/actions.rb:15:in `require'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/generate/generate_command.rb:19:in `perform'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
/Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Bundler Error Backtrace:
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `each'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in `block in require'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `each'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in `require'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.15.3/lib/bundler.rb:108:in `require'
    from /Users/rmosolgo/code/graphql-ruby/tmp/dummy/config/application.rb:17:in `<top (required)>'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/actions.rb:15:in `require'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands/generate/generate_command.rb:19:in `perform'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
    from /Users/rmosolgo/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

I applied a patch locally so that I could keep going:

    Dir[File.dirname(__FILE__) + "/../types/rails/**/*.rb"].each { |f|
-     require f    
+       if f =~ /active_record/ && !defined?(ActiveRecord)
+         # Some parts of Rails are present, but ActiveRecord is not
+         next
+       else
+         require(f)
+       end
    }

It happened because my dummy Rails app for unit tests doesn't have ActiveRecord. I've heard of setups like this before, for example, in react-rails, we reduced the dependency from rails to railties, to support this kind of partial environment.

Are you interested in supporting environments like this? If so, I'd be happy to clean up my patch and open a PR.