Skip to content

Commit bc58bee

Browse files
Start server process in dir where cmd was called
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']
1 parent 647b8c3 commit bc58bee

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/spring/application.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ def serve(client)
172172
end
173173
end
174174

175+
# Ensure we boot the process in the directory the command was called from,
176+
# not from the directory Spring started in
177+
original_dir = Dir.pwd
178+
Dir.chdir(env['PWD'] || Dir.pwd)
179+
175180
pid = fork {
176181
Process.setsid
177182
IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") }
@@ -237,6 +242,7 @@ def serve(client)
237242
# (i.e. to prevent `spring rake -T | grep db` from hanging forever),
238243
# even when exception is raised before forking (i.e. preloading).
239244
reset_streams
245+
Dir.chdir(original_dir)
240246
end
241247

242248
def terminate

0 commit comments

Comments
 (0)