Skip to content

Commit

Permalink
Start server process in dir where cmd was called
Browse files Browse the repository at this point in the history
When a Spring server boots, it maintains its directory
for the duration of its life-cycle. This means that if you've changed
directories and run a Spring command, the process will start within the
context of the original directory.

This is problematic in the use case of engines, where an engine command
may be run from the context of the host application, and then run again
from the engine directory. In this scenario, the process will assume its
working directory is the host application instead of the engine.

The solution proposed here is to change the current directory to where
the Spring command was called from. The directory is taken from ENV['PWD']
  • Loading branch information
adrianna-chang-shopify committed Jul 23, 2020
1 parent 647b8c3 commit 20d3a5f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/spring/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ def serve(client)
end
end

# Ensure we boot the process in the directory the command was called from,
# not from the directory Spring started in
original_dir = Dir.pwd
Dir.chdir(env['PWD'] || original_dir)

pid = fork {
Process.setsid
IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") }
Expand Down Expand Up @@ -237,6 +242,7 @@ def serve(client)
# (i.e. to prevent `spring rake -T | grep db` from hanging forever),
# even when exception is raised before forking (i.e. preloading).
reset_streams
Dir.chdir(original_dir)
end

def terminate
Expand Down

0 comments on commit 20d3a5f

Please sign in to comment.