spring icon indicating copy to clipboard operation
spring copied to clipboard

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

Open GCorbel opened this issue 1 year ago • 3 comments

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.

GCorbel avatar May 29 '24 21:05 GCorbel

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.

rafaelfranca avatar May 29 '24 21:05 rafaelfranca

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 avatar May 30 '24 13:05 GCorbel

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)

GCorbel avatar May 31 '24 22:05 GCorbel