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

Only add exist paths to reloading target #317

Conversation

y-yagi
Copy link
Contributor

@y-yagi y-yagi commented Feb 5, 2019

Currently, factories, test/factories and spec/factories are specified by default as reload watching paths.
In many applications, there are directories which do not exist (perhaps do not have spec if using minitest and do not have spec if using minitest).

In Rails 5 series, if specify a path that does not exist inEventedFileUpdateChecker, its parent is added to the watching path.
As a result, node_modules is also included in the watching path, and unexpectedly many directories and files are included in listen's watching targets. Also, if symlink is included in node_modules, it also causes warning of listen.

This issue is solved in Rails 6. However, since many applications use this gem in Rails 5 and below, it is good not to add directories that do not exist on the gem side.

Ref: rails/rails#32700


describe "default definition paths" do
it "include only exist paths" do
expect(FactoryBot.definition_file_paths).to eq([Rails.root.join("factories")])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [84/80]

@y-yagi y-yagi force-pushed the only_add_exist_paths_to_reloading_target branch from 971dffb to 7e47e11 Compare February 5, 2019 07:14
@y-yagi
Copy link
Contributor Author

y-yagi commented Feb 5, 2019

The test is an error due to an incompatibility issue with Rails and the new version of sqlite3 gem.
Ref: rails/rails#35161

Copy link
Collaborator

@composerinteralia composerinteralia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch. Now I remember seeing rails/rails#33822.

Is there a scenario where this fix could cause problems? If spring is running, then after that I add my first factory to spec/factories, will I need to run spring stop to get that factory to load?

And if that is the case, would it make sense to only do this filtering out if we are running a rails version before 6.0?

@@ -31,7 +31,7 @@ class Railtie < Rails::Railtie
def definition_file_paths
config.factory_bot.definition_file_paths.map do |path|
Rails.root.join(path)
end
end.select(&:exist?)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will quite work because the FactoryBot.definition_file_paths gets used for two things, both a directory and a file. So if spec/factories is in FactoryBot.definition_file_paths we watch both the spec/factories directory and also the spec/factories.rb file. With this change we would also stop watching spec/factories.rb if spec/factories doesn't exist. We may need to move this logic into the reloader class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When checking the directory to watching, the same check is done for the parent directory of the file. Therefore, the same fix is necessary for the files.
https://github.com/rails/rails/blob/188ccf2f41b3d6bb77ba48b81343f80cf5615b75/activesupport/lib/active_support/evented_file_update_checker.rb#L126

checker = ActiveSupport::EventedFileUpdateChecker.new(%w(spec/factories.rb test/factories.rb), []) {}
checker.send(:directories_to_watch).include?(Rails.root)
#=> true 

@y-yagi
Copy link
Contributor Author

y-yagi commented Feb 6, 2019

Thanks for your review!

Yes, your concern is correct.
If spring is running with spec/factories not exist, spring must be restarted after spec/factories is created.
However, I think that this probably does not occur much. For example, if create a factory using scaffold, this will not happen because spring is restarted automatically(If you change the file under config, it will be restarted automatically).

But of course, it is possible to respond by checking the version of Rails. Should I do?

@composerinteralia
Copy link
Collaborator

composerinteralia commented Feb 6, 2019

I will trust your judgement on the scenario with spring. If an issue comes up, we can always address it at that time.

I think we will still have some failing tests once this gets rebased on master, possibly related to my point about FactoryBot.definition_file_paths (I saw failures locally relating to definition loading). Once we resolve those failures I think this will be good to merge and I will release 5.0.1.

@y-yagi y-yagi force-pushed the only_add_exist_paths_to_reloading_target branch from 7e47e11 to 78916d1 Compare February 6, 2019 22:15
Currently, `factories`, `test/factories` and `spec/factories` are
specified by default as reload watching paths.
In many applications, there are directories which do not exist (perhaps
do not have` spec` if using minitest and do not have` spec` if using
minitest).

In Rails 5 series, if specify a path that does not exist in
`EventedFileUpdateChecker`, its parent is added to the watching path.
As a result, `node_modules` is also included in the watching path, and
unexpectedly many directories and files are included in listen's watching
targets. Also, if symlink is included in `node_modules`, it also causes
warning of listen.

This issue is solved in Rails 6. However, since many applications use
this gem in Rails 5 and below, it is good not to add directories that
do not exist on the gem side.

Ref: rails/rails#32700
@y-yagi y-yagi force-pushed the only_add_exist_paths_to_reloading_target branch from 78916d1 to ceba13b Compare February 6, 2019 23:52
@y-yagi
Copy link
Contributor Author

y-yagi commented Feb 7, 2019

Oh, sorry. I misunderstood definition_file_paths behavior. I fixed file / directory existing check and test pass now.

@composerinteralia
Copy link
Collaborator

Thanks! I'll have time to push the 5.0.1 release on Friday.

@composerinteralia composerinteralia merged commit f718048 into thoughtbot:master Feb 7, 2019
@y-yagi
Copy link
Contributor Author

y-yagi commented Feb 7, 2019

Thanks!

@y-yagi y-yagi deleted the only_add_exist_paths_to_reloading_target branch February 7, 2019 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants