Browse files

Searching for rails executable correctly

 * Current logic of finding Rails executable in parent directory is
   not returning full path of executable if it is found in one of the
   parent directories
 * To compensate for this, we have to call exec_app_rails recursively
   until the executable is found or we cant do 'chdir' anymore
 * This solution finds the correct executable path from parent
   directory(s) recursively
  • Loading branch information...
1 parent 4658b0d commit 22e5ab31b52a287705083ad6f3cad836ab96b619 @prathamesh-sonpatki prathamesh-sonpatki committed Apr 10, 2013
Showing with 14 additions and 19 deletions.
  1. +11 −16 railties/lib/rails/app_rails_loader.rb
  2. +3 −3 railties/test/app_rails_loader_test.rb
@@ -6,22 +6,22 @@ module AppRailsLoader
EXECUTABLES = ['bin/rails', 'script/rails']
def self.exec_app_rails
- cwd = Dir.pwd
- exe = find_executable
- exe ||= find_executable_in_parent_path
- return unless exe
+ cwd = Dir.pwd
+ pathname =
+ until exe = find_executable
+ # Return to working directory if root is hit without finding executable
+ Dir.chdir(cwd) and return if pathname.root?
+ # Otherwise keep moving upwards in search of executable
+ Dir.chdir("..")
+ pathname = pathname.parent
+ end
contents =
# This is the Rails executable, let's use it
if contents =~ /(APP|ENGINE)_PATH/
- exec RUBY, exe, *ARGV if find_executable
- Dir.chdir("..") do
- # Recurse in a chdir block: if the search fails we want to be sure
- # the application is generated in the original working directory.
- exec_app_rails unless cwd == Dir.pwd
- end
+ exec RUBY, exe, *ARGV
# This is a Bundler binstub. Stop and explain how to upgrade.
elsif exe =~ /bin\/rails$/ && contents =~ /This file was generated by Bundler/
@@ -59,10 +59,5 @@ def self.find_executable
EXECUTABLES.find { |exe| File.exists?(exe) }
- def self.find_executable_in_parent_path(path =
- EXECUTABLES.find do |exe|
- File.exists?(exe) || !path.root? && find_executable_in_parent_path(path.parent)
- end
- end
@@ -17,14 +17,14 @@ class AppRailsLoaderTest < ActiveSupport::TestCase
test "is not in a rails application if #{exe} exists but doesn't contain APP_PATH" do
File.stubs(:read).with(exe).returns("railties #{exe}")
- assert !Rails::AppRailsLoader.find_executable
+ assert !Rails::AppRailsLoader.exec_app_rails
test "is in a rails application if parent directory has #{exe} containing APP_PATH" do
- assert Rails::AppRailsLoader.find_executable_in_parent_path("/foo/bar"))
+ assert_equal Rails::AppRailsLoader.find_executable_in_parent_path("/foo/bar")), "/foo/#{exe}"

rubys Apr 11, 2013


Just noting that this commit updates a test case which calls a method that this very commit removed. Needless to say, this test fails.


fxn Apr 11, 2013


Yep, it was noted in the pull request.


prathamesh-sonpatki Apr 11, 2013


@rubys You are right. I started with the implementation which used this method. But with @fxn's help, we optimized it so that this method was no longer required.

I forgot to update the test before it got merged. I am waiting for some guidance from @fxn about how to test it. I have already added a comment for it on the PR page.

test "is not in a rails application if at the root directory and doesn't have #{exe}" do
@@ -36,7 +36,7 @@ class AppRailsLoaderTest < ActiveSupport::TestCase
- assert Rails::AppRailsLoader.find_executable_in_parent_path("/foo/bar"))
+ assert Rails::AppRailsLoader.find_executable_in_parent_path("/foo/bar")), "/foo/#{exe}"
test "is in a rails engine if #{exe} exists containing ENGINE_PATH" do

0 comments on commit 22e5ab3

Please sign in to comment.