thoughtbot / factory_bot_rails

Factory Bot ♥ Rails

Home Page:https://thoughtbot.com/services/ruby-on-rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reloading Issue Triggering Resolved Rails Bug

BintLopez opened this issue · comments

Related Rails issue: rails/rails#32700

Steps to reproduce

  • Creating a new rails project with ruby 2.5.3 and rails 6.0.0.beta3
  • Add gem 'factory_bot_rails', '~> 5.0', '>= 5.0.1' to the gemfile
  • Install gems bundle install
  • boot up the rails server bundle exec rails s
  • view errors in server output

Expected behavior

Adding factory_bot_rails gem to a rails project should not trigger ** ERROR: directory is already being watched! ** when booting up a rails server or console

Actual behavior

After including the gem in my project, I'm getting the following errors in my server when running the rails server...

be rails s
=> Booting Puma
=> Rails 6.0.0.beta3 application starting in development 
=> Run `rails server --help` for more startup options
        ** ERROR: directory is already being watched! **

        Directory: /path/to/my/app/node_modules/.bin/compression-webpack-plugin

        is already being watched through: /path/to/my/app/node_modules/compression-webpack-plugin

        MORE INFO: https://github.com/guard/listen/wiki/Duplicate-directory-errors
        ** ERROR: directory is already being watched! **

        Directory: /path/to/my/app/node_modules/.bin/mini-css-extract-plugin

        is already being watched through: /path/to/my/app/node_modules/mini-css-extract-plugin

        MORE INFO: https://github.com/guard/listen/wiki/Duplicate-directory-errors
(node:33074) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
        ** ERROR: directory is already being watched! **

        Directory: /path/to/my/app/node_modules/@rails/webpacker/node_modules/.bin/compression-webpack-plugin

        is already being watched through: /path/to/my/app/node_modules/compression-webpack-plugin

        MORE INFO: https://github.com/guard/listen/wiki/Duplicate-directory-errors
        ** ERROR: directory is already being watched! **

        Directory: /path/to/my/app/node_modules/@rails/webpacker/node_modules/.bin/mini-css-extract-plugin

        is already being watched through: /path/to/my/app/node_modules/mini-css-extract-plugin

        MORE INFO: https://github.com/guard/listen/wiki/Duplicate-directory-errors
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.3-p105), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000

System configuration

Rails version: 6.0.0.beta3

Ruby version: 2.5.3

Hm, this should have been fixed by f718048, which went out in factory_bot_rails 5.0.1

Thanks for the report!

Having the same ERROR: directory is already being watched! when I'm running daemon process with daemons gem. Without factory_bot_rails everything works as expected.

@TeTiRoss what version of factory_bot_rails are you using?

@composerinteralia
factory_bot_rails 5.0.1
Ruby 2.6.1
Rails 5.2.2.1

@BintLopez I wasn't able to reproduce this with the steps you provided. Is there something I am missing?

rails new testing
cd testing
echo "gem 'factory_bot_rails', '~> 5.0', '>=5.0.1', group: [:test, :development]" >> Gemfile
bundle install
bin/rails s

I didn't see any error messages. Also, I went into the console and checked that the factory_bot_rails reloader was not actually watching any files (as expected with f718048) :

Rails.application.reloaders.first.send(:directories_to_watch)
#=> []

I am using:
factory_bot_rails 5.0.1
Ruby 2.6.1
Rails 5.2.3


I tried these steps on 6.0.0.beta3 as well and had the same results. I am especially surprised you are seeing this on 6.0.0.beta3, since rails/rails#33822 was included in that release.

commented

I also encountered this error on factory_bot 5.0.0, factory_bot_rails: 5.0.0.
But fixed it after updated to factory_bot: 5.0.2, factory_bot_rails: 5.0.1.

I ran through the repro steps with a fresh rails project (using 6.0.0.beta3) and was able to reproduce with both ruby 2.5.3 and ruby 2.6.2. This is using gem 'factory_bot_rails', '~> 5.0', '>=5.0.1.

@yazumoto that makes sense, since the fix in f718048 went out in factory_bot_rails 5.0.1. Thanks for letting me know!

@BintLopez would you be to share a link to the fresh rails project you used to reproduce this? Thanks for your help!

@composerinteralia -- here's the repo that triggers the error for me: https://github.com/BintLopez/testing_bug_repro.

What I did to reproduce was rails new, add gem 'factory_bot_rails', '~> 5.0', '>=5.0.1 to the Gemfile, bundle, and then run bundle exec rails server. The first commit is initializing the project -- booting up the server does not trigger the error. The second commit adds the gem and that's when errors start for me.

Based on the write up at the link from the error https://github.com/guard/listen/wiki/Duplicate-directory-errors ... this appears to be a bug in how listen handles symlinks. What about factory bot rails is triggering this bug in listen? Or perhaps there's something in my local env contributing to this 🤷‍♀️ ... def interested to see if my repo yields the same errors for you locally.

@BintLopez unfortunately I still am not seeing the error in your test repo. It seems like we are following exactly the same steps, so I am not sure what is different.

I would be curious to know what you get if you start a rails console and type: Rails.application.reloaders.first.send(:directories_to_watch)

Thanks for that command! Running that returns an empty array. But looking at it for all the reloaders returned the following...

Rails.application.reloaders.map{|r| r.send(:directories_to_watch) if r.respond_to?(:directories_to_watch, true)}
=> [
        [],
        [#<Pathname:/Users/path/to/my/project/config/locales>],
        nil,
        [#<Pathname:/Users/path/to/my/project/db>,
         #<Pathname:Users/path/to/my/project/app/channels>,
         #<Pathname:/Users/path/to/my/project/app/controllers>, 
         #<Pathname:/Users/path/to/my/project/app/helpers>, 
         #<Pathname:/Users/path/to/my/project/app/jobs>, 
         #<Pathname:/Users/path/to/my/project/app/mailers>, 
         #<Pathname:/Users/path/to/my/project/app/models>, 
         #<Pathname:/Users/path/to/my/project/test/mailers/previews>]
]

^^ I don't see anything in there that would be loading the js (at first glance)

Also, another data point -- a friend of mine ran the same code and was not able to reproduce the same issue. This makes me think it's something about dev env that's in the works here. I use rbenv for ruby version, nvm for node versioning, homebrew (maybe brew has something to do since it's symlinks?). Running in a mac os. At any rate, this isn't a blocker for me, and shouldn't affect production since listen is only included in dev environment. Just very noisy console output 🙉 . Likely going to do the monkey patch suggested here as a workaround -- https://github.com/guard/listen/wiki/Duplicate-directory-errors#monkey-patch

That reloader output looks right to me:

  • [] This is the factory_bot_rails reloader. It is not watching any directories. That is expected since there aren't any factory_bot definition files yet
  • Next is the I18n reloader, which is watching the locales directory
  • Then the routes reloader, which uses its own Rails::Application::RoutesReloader that doesn't have the private directories_to_watch method
  • Finally the main application loader for you models, controllers, etc.

I am stumped. Let me know if that patch helps and maybe we can work backwards from there.

@y-yagi do you have any ideas about what might be going on here?

Thank you for the mention. I think this is an issue of Rails.

I will fix this on Rails side first. But considering the old Rails version, I think it would be better to fix the factory_bot_rails side as well.

@composerinteralia I think it's better not to call FactoryBot.reload if there is no file or directory to watch. What do you think?

Aha! That makes sense. Thanks so much for your help. Yeah, I agree we should fix it on the factory_bot_rails side as well. There is no need to reload if no files or directories exist!

👍 I will create a PR later.