Skip to content

config/spring.rb loaded after Spring.application_root_path is memoized #582

Open
@petekinnecom

Description

@petekinnecom

Hi,

We have run into an issue with our usage of Spring (I believe it is the same issue that folks are running in to here: #323). In our case, we have engines inside of an application but we only use a single top-level Gemfile. This has exposed an issue with the order of events when loading of the config/spring.rb.

For us, our config/spring.rb loaded after Spring.application_root_path is memoized and so our config/spring.rb does not have the correct effect.

Here is the issue as I have been able to track it down:

Consider running the command:

bundle exec spring rails runner 'puts :ok'

Spring loads the client/ files and decides it needs to start a server. That eventually leads to spring/commands getting loaded, which loads the config/spring.rb:

config = File.expand_path("./config/spring.rb")

Spring then decides to spawn a server in the background:

@pid = Process.spawn(

This process spawns and loads this file (note it is in a new process, so it will again need to read the config/spring.rb):

https://github.com/rails/spring/blob/1f44a267a2476658af86b0bc1afb94588a36ed04/lib/spring/application/boot.rb

The app.run on line 19 will lead to the server getting loaded and leads to the spring/commands.rb getting loaded which loads the config/spring.rb See:

BUT, here is the problem, line 15:

"spring app | #{app.app_name} | started #{distance} ago | #{app.app_env} mode"

This calls app.app_name which traces all the way down to the line:

@application_root_path ||= begin

This method then hits the default behavior BEFORE Spring has loaded the config/spring.rb. This value is memoized, by the time the config/spring.rb is loaded, its changes to Spring.application_root have no effect.


So, requiring spring/commands somewhere before hitting this line solves the issue:

"spring app | #{app.app_name} | started #{distance} ago | #{app.app_env} mode"

However, based on this line, it seems that we want that to be lazily required, so I'm not sure where exactly the best place to put it would be:


For those looking for a workaround in the meantime, adding the following to config/spring.rb will fix the issue and make Spring respect your local config/spring.rb in engine dummy apps:

Spring.instance_variable_set(:@application_root_path, nil)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions