Skip to content
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

Issue with the upcoming Factory Bot Rails due to lacking of lazy load hooks #89

Closed
tagliala opened this issue Nov 1, 2023 · 4 comments · Fixed by #91
Closed

Issue with the upcoming Factory Bot Rails due to lacking of lazy load hooks #89

tagliala opened this issue Nov 1, 2023 · 4 comments · Fixed by #91

Comments

@tagliala
Copy link
Contributor

tagliala commented Nov 1, 2023

Hello,

I'm using database_cleaner-active_record together with factory-bot-rails in an application.

The issue is documented here: thoughtbot/factory_bot_rails#426

Here it is a reproducible test case:

# frozen_string_literal: true

require 'bundler/inline'

begin
  gemfile(true) do
    source 'https://rubygems.org'

    gem 'pg'
    gem 'rails'
    # Comment out `git` and `branch` params to use the latest stable version of factory bot and make the test pass
    gem 'factory_bot_rails', git: 'https://github.com/thoughtbot/factory_bot_rails.git', branch: 'main'
    gem 'database_cleaner-active_record'
  end

  require 'active_record'
  require 'factory_bot_rails'
  require 'database_cleaner-active_record'
rescue Gem::LoadError => e
  puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}"
rescue LoadError => e
  puts "\nError:\n#{e.backtrace.first} #{e.message}"
  exit 1
end

puts 'Ok'

Error:

~/.rvm/gems/ruby-3.2.2/gems/railties-7.1.1/lib/rails.rb:51:in `configuration': undefined method `config' for nil:NilClass (NoMethodError)

      application.config
                 ^^^^^^^
	from ~/.rvm/gems/ruby-3.2.2/bundler/gems/factory_bot_rails-fd1ca67b43f4/lib/factory_bot_rails/railtie.rb:25:in `block in <class:Railtie>'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:97:in `class_eval'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:97:in `block in execute_hook'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:87:in `with_execution_control'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:92:in `execute_hook'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:78:in `block in run_load_hooks'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:77:in `each'
	from ~/.rvm/gems/ruby-3.2.2/gems/activesupport-7.1.1/lib/active_support/lazy_load_hooks.rb:77:in `run_load_hooks'
	from ~/.rvm/gems/ruby-3.2.2/gems/activerecord-7.1.1/lib/active_record/base.rb:338:in `<module:ActiveRecord>'
	from ~/.rvm/gems/ruby-3.2.2/gems/activerecord-7.1.1/lib/active_record/base.rb:15:in `<top (required)>'
	from <internal:~/.rvm/rubies/ruby-3.2.2/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38:in `require'
	from <internal:~/.rvm/rubies/ruby-3.2.2/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38:in `require'
	from ~/.rvm/gems/ruby-3.2.2/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/truncation.rb:2:in `<top (required)>'
	from <internal:~/.rvm/rubies/ruby-3.2.2/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38:in `require'
	from <internal:~/.rvm/rubies/ruby-3.2.2/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38:in `require'
	from ~/.rvm/gems/ruby-3.2.2/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record.rb:4:in `<top (required)>'
	from <internal:~/.rvm/rubies/ruby-3.2.2/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38:in `require'
	from <internal:~/.rvm/rubies/ruby-3.2.2/lib/ruby/site_ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:38:in `require'
	from ~/.rvm/gems/ruby-3.2.2/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner-active_record.rb:1:in `<top (required)>'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/runtime.rb:60:in `require'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/runtime.rb:55:in `each'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/runtime.rb:55:in `block in require'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/runtime.rb:44:in `each'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/runtime.rb:44:in `require'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/inline.rb:66:in `block (2 levels) in gemfile'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/settings.rb:140:in `temporary'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/inline.rb:51:in `block in gemfile'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler.rb:411:in `block in with_unbundled_env'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler.rb:657:in `with_env'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler.rb:411:in `with_unbundled_env'
	from ~/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.20/lib/bundler/inline.rb:42:in `gemfile'

It is due to the fact that Database Cleaner does not use lazy hooks before using ActiveRecord. You can see a fix to the same issue with another gem here: amoeba-rb/amoeba#115

However, the fix does not appear to be so trivial with Database Cleaner, so this is why I'm opening an issue rather than submitting a PR

@tagliala tagliala changed the title Issue with the upcoming Factory Bot Rails due to Issue with the upcoming Factory Bot Rails due to lacking of lazy load hooks Nov 1, 2023
@tagliala
Copy link
Contributor Author

Hopefully this will be addressed by Factory Bot Rails itself in thoughtbot/factory_bot_rails#432

@jamesw
Copy link

jamesw commented Nov 21, 2023

@tagliala Having the same issue here, this issue happens for me regardless of factory bot rails version used and appears totally unrelated to factory bot rails

@tagliala
Copy link
Contributor Author

Please try with the branch used in thoughtbot/factory_bot_rails#432

@benoittgt
Copy link

I think this gem should follow lazy loading like other project:

benoittgt added a commit to benoittgt/database_cleaner-active_record that referenced this issue Nov 23, 2023
This is well known issue. We should prefer to code that reference active
record only when it's already lazy loaded.
It can break other gems initializing process

Related:
- rails/rails#46567
- paper-trail-gem/paper_trail@fc6c5f6
(inspiration)
- thoughtbot/factory_bot_rails#432

Fix: DatabaseCleaner#89
benoittgt added a commit to benoittgt/database_cleaner-active_record that referenced this issue Nov 23, 2023
This is well known issue. We should prefer to code that reference active
record only when it's already lazy loaded.
It can break other gems initializing process

Related:
- rails/rails#46567
- paper-trail-gem/paper_trail@fc6c5f6
(inspiration)
- thoughtbot/factory_bot_rails#432

Fix: DatabaseCleaner#89
casperisfine added a commit to casperisfine/database_cleaner-active_record that referenced this issue Nov 23, 2023
Doing this cause a slew of problems with Rails loading process, causing some configurations not to be applied, etc.

Fix: DatabaseCleaner#89
Fix: DatabaseCleaner#90
etagwerker pushed a commit that referenced this issue Nov 23, 2023
Doing this cause a slew of problems with Rails loading process, causing some configurations not to be applied, etc.

Fix: #89
Fix: #90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants