@@ -497,19 +497,9 @@ function drush_preflight() {
497497 drush_preflight_site ();
498498
499499 // Check to see if anything changed during the 'site' preflight
500- // that might allow us to find our alias record now
500+ // that might allow us to find our alias record now.
501501 if (empty ($ alias_record )) {
502502 $ alias_record = _drush_sitealias_set_context_by_name ($ target_alias );
503-
504- // If the site alias settings changed late in the preflight,
505- // then run the preflight for the root and site contexts again.
506- if (!empty ($ alias_record )) {
507- $ remote_host = drush_get_option ('remote-host ' );
508- if (!isset ($ remote_host )) {
509- drush_preflight_root ();
510- drush_preflight_site ();
511- }
512- }
513503 }
514504
515505 // Fail if we could not find the selected site alias.
@@ -544,6 +534,11 @@ function drush_preflight() {
544534function drush_preflight_root () {
545535 $ root = drush_get_option ('root ' );
546536 Drush::bootstrapManager ()->locateRoot ($ root );
537+ $ root = Drush::bootstrapManager ()->getRoot ();
538+
539+ // Load the autoload file and provide it to the bootstrap manager.
540+ $ siteAutoloader = drush_drupal_load_autoloader ($ root );
541+ Drush::bootstrapManager ()->setAutoloader ($ siteAutoloader );
547542
548543 // Load the config options from Drupal's /drush, ../drush, and sites/all/drush directories,
549544 // even prior to bootstrapping the root.
@@ -799,14 +794,21 @@ function drush_preflight_command_dispatch() {
799794 $ args = drush_get_arguments ();
800795 $ command_name = array_shift ($ args );
801796 $ root = Drush::bootstrapManager ()->getRoot ();
802- $ local_drush = drush_get_option ('drush-script ' );
803797 $ is_local = drush_get_option ('local ' );
804798 $ values = NULL ;
805- if (!empty ($ root ) && !empty ($ local_drush ) && empty ($ is_local )) {
799+
800+ $ local_drush = drush_get_option ('drush-script ' );
801+ if (!empty ($ local_drush )) {
806802 if (!drush_is_absolute_path ($ local_drush )) {
807803 $ local_drush = $ root . DIRECTORY_SEPARATOR . $ local_drush ;
808804 }
809805 $ local_drush = realpath ($ local_drush );
806+ }
807+
808+ $ drupal_root_from_alias = drush_get_option ('root ' , $ root , 'alias ' );
809+ $ shouldRedispatch = (!empty ($ root )) && ($ root != $ drupal_root_from_alias );
810+
811+ if (!empty ($ root ) && !empty ($ local_drush ) && empty ($ is_local )) {
810812 $ this_drush = drush_find_drush ();
811813 // If there is a local Drush selected, and it is not the
812814 // same Drush that is currently running, redispatch to it.
@@ -817,18 +819,23 @@ function drush_preflight_command_dispatch() {
817819 // infinite loop.
818820 if (file_exists ($ local_drush ) && !drush_is_nested_directory (dirname ($ root ), $ this_drush )) {
819821 $ uri = drush_get_context ('DRUSH_SELECTED_URI ' );
820- $ aditional_options = array (
821- 'root ' => $ root ,
822- );
823- if (!empty ($ uri )) {
824- $ aditional_options ['uri ' ] = $ uri ;
825- }
826- // We need to chdir to the Drupal root here, for the
827- // benefit of the Drush wrapper.
828- chdir ($ root );
829- $ values = drush_do_command_redispatch (is_array ($ command ) ? $ command : $ command_name , $ args , NULL , NULL , $ local_drush , TRUE , $ aditional_options );
822+ $ shouldRedispatch = true ;
830823 }
831824 }
825+
826+ if ($ shouldRedispatch ) {
827+ $ aditional_options = [
828+ 'root ' => $ drupal_root_from_alias ,
829+ ];
830+ if (!empty ($ uri )) {
831+ $ aditional_options ['uri ' ] = $ uri ;
832+ }
833+ // We need to chdir to the Drupal root here, for the
834+ // benefit of the Drush wrapper.
835+ chdir ($ root );
836+ $ values = drush_do_command_redispatch (is_array ($ command ) ? $ command : $ command_name , $ args , NULL , NULL , $ local_drush , TRUE , $ aditional_options );
837+ }
838+
832839 // If the command sets the 'handle-remote-commands' flag, then we will short-circuit
833840 // remote command dispatching and site-list command dispatching, and always let
834841 // the command handler run on the local machine.
0 commit comments