function drush_preflight

8.0.x drush_preflight()
7.x drush_preflight()
master drush_preflight()

During the initialization of Drush, this is the first step where we load our configuration and commandfiles, and select the site we are going to operate on; however, we take no irreversible actions (e.g. site bootstrapping). This allows commands that are declared with no bootstrap to select a new site root and bootstrap it.

In this step we will register the shutdown function, parse the command line arguments and store them in their related contexts.

Configuration files (drushrc.php) that are a) Specified on the command line b) Stored in the root directory of drush.php c) Stored in the home directory of the system user.

Additionally the DRUSH_QUIET and DRUSH_BACKEND contexts, will be evaluated now, as they need to be set very early in the execution flow to be able to take affect.

Return value


5 calls to drush_preflight()
commandUnitCase::testCommandVersionSpecific in tests/commandUnitTest.php
Assure that matching version-specific command files are loaded and others are ignored.
commandUnitCase::testGetCommands in tests/commandUnitTest.php
Assert that $command has interesting properties. Reference command by it's alias (dl) to assure that those aliases are built as expected.
drush_main in includes/
The main Drush function.
outputFormatUnitCase::testOutputFormat in tests/outputFormatUnitTest.php
Test various output formats using php-eval with no Drupal site.
tablesUnitTest::setUp in tests/tablesUnitTest.php


includes/, line 209
Preflight, postflight and shutdown code.


function drush_preflight() {
  // Create an alias '@none' to represent no Drupal site
  _drush_sitealias_cache_alias('@none', array('root' => '', 'uri' => ''));

  // Discover terminal width for pretty output.

  // Display is tidy now that column width has been handled.
  drush_log(dt('Starting Drush preflight.'), LogLevel::PREFLIGHT);

  // Statically define a way to call drush again.
  define('DRUSH_COMMAND', drush_find_drush());

  // prime the CWD cache

  // Set up base environment for system-wide file locations.

  // Setup global alias_paths[] in context system.
  if (!drush_get_option('local')) {
  if (!drush_get_option('local')) {
    // Load a drushrc.php file in the drush.php's directory.

    // Load a drushrc.php file in the $ETC_PREFIX/etc/drush directory.

    // Load a drushrc.php file at ~/.drushrc.php.

    // Load a drushrc.php file in the ~/.drush directory.

  // Load a custom config specified with the --config option.

  // Load all the commandfiles findable from any of the
  // scopes listed above.

  // Look up the alias identifier that the user wants to use,
  // either via an arguement or via 'site-set'.
  $target_alias = drush_sitealias_check_arg_and_site_set();

  // Process the site alias that specifies which instance
  // of Drush (local or remote) this command will operate on.
  // We must do this after we load our config files (so that
  // site aliases are available), but before the rest of
  // Drush preflight and Drupal root bootstrap phase are
  // done, since site aliases may set option values that
  // affect these phases.
  $alias_record = _drush_sitealias_set_context_by_name($target_alias);

  // Find the selected site based on --root, --uri or cwd

  // Preflight the selected site, and load any configuration and commandfiles associated with it.

  // Check to see if anything changed during the 'site' preflight
  // that might allow us to find our alias record now
  if (empty($alias_record)) {
    $alias_record = _drush_sitealias_set_context_by_name($target_alias);

    // If the site alias settings changed late in the preflight,
    // then run the preflight for the root and site contexts again.
    if (!empty($alias_record)) {
      $remote_host = drush_get_option('remote-host');
      if (!isset($remote_host)) {

  // Fail if we could not find the selected site alias.
  if ($target_alias && empty($alias_record)) {
    return drush_set_error('DRUSH_BOOTSTRAP_NO_ALIAS', dt("Could not find the alias !alias", array('!alias' => $target_alias)));

  // If applicable swaps in shell alias values.

  // Copy global options to their respective contexts

  // Set environment variables based on #env-vars.

  // Select the bootstrap object and return it.
  return drush_select_bootstrap_class();