rails / spring

Rails application preloader
MIT License
2.81k stars 341 forks source link

Do not reload the app after a file required during initialization is changed #720

Open GCorbel opened 5 months ago

GCorbel commented 5 months ago

During the initialization of a Rails app, some files are required and added to $LOADED_FEATURES so, when they are changed, the whole app reloads.

I want to avoid that and I this commenting the line that add them to the watcher fix my issue.

Is there a proper way to do that ? I don't understand at which step Spring::Application#preload is called but maybe it's a good idea to watch only files loaded before Rails.application.config.after_initialize or Rails.application.config.to_preload.

rafaelfranca commented 5 months ago

It happens before even the application is loaded, so we can't implement any logic that depends on what happened in the application. Unfortunately there is no way to do this. We could have a Watcher#remove method so you could configure srping to stop watching a file.

GCorbel commented 5 months ago

I agree with Watcher#remove.

I tried to do Spring.watcher.files.reject! { |k, v| $LOADED_FEATURES.include?(k) } but it's not enough because spring is running in a different thread. I'm not sure how to do.

GCorbel commented 5 months ago

I gave a closer look and I don't see how to change files watched once the server is running.

The best solution I found ATM is adding this in config/spring.rb :

module OverrideWatcher
  def add(*items)
    items.reject! { |k, _v| $LOADED_FEATURES.include?(k) }
    super
  end
end
Spring.watcher.extend(OverrideWatcher)