From edfd51adaa25ef0c5054fb5cd2c89a47a4269f47 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Thu, 26 Oct 2023 17:41:18 -0500 Subject: [PATCH] Refs #5785. Fix path to remote drush (#5796) --- src/Runtime/DependencyInjection.php | 3 ++- src/SiteAlias/ProcessManager.php | 27 +++++++++++++++++++-------- tests/functional/RsyncTest.php | 2 +- tests/functional/SqlSyncTest.php | 2 +- tests/unish/CommandUnishTestCase.php | 2 +- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Runtime/DependencyInjection.php b/src/Runtime/DependencyInjection.php index 997c7500bc..65cb9e5219 100644 --- a/src/Runtime/DependencyInjection.php +++ b/src/Runtime/DependencyInjection.php @@ -133,7 +133,8 @@ protected function addDrushServices($container, ClassLoader $loader, DrushDrupal Robo::addShared($container, 'tildeExpansion.hook', 'Drush\Runtime\TildeExpansionHook'); Robo::addShared($container, 'process.manager', ProcessManager::class) ->addMethodCall('setConfig', ['config']) - ->addMethodCall('setConfigRuntime', ['config.runtime']); + ->addMethodCall('setConfigRuntime', ['config.runtime']) + ->addMethodCall('setDrupalFinder', [$drupalFinder]); Robo::addShared($container, 'redispatch.hook', 'Drush\Runtime\RedispatchHook') ->addArgument('process.manager'); diff --git a/src/SiteAlias/ProcessManager.php b/src/SiteAlias/ProcessManager.php index a1335ee446..2f350158af 100644 --- a/src/SiteAlias/ProcessManager.php +++ b/src/SiteAlias/ProcessManager.php @@ -18,6 +18,18 @@ */ class ProcessManager extends ConsolidationProcessManager { + protected $drupalFinder; + + public function setDrupalFinder($drupalFinder): void + { + $this->drupalFinder = $drupalFinder; + } + + public function getDrupalFinder() + { + return $this->drupalFinder; + } + /** * Run a Drush command on a site alias (or @self). */ @@ -66,13 +78,13 @@ public function drushScript(SiteAliasInterface $siteAlias) return $siteAlias->get('paths.drush-script'); } - // If the provided site alias is for a remote site / container et. al., - // then use the 'drush' in the $PATH. + // A remote site / container et. al., if ($this->hasTransport($siteAlias)) { if ($siteAlias->hasRoot()) { return Path::join($siteAlias->root(), $this->relativePathToVendorBinDrush()); } + // Fallback to the 'drush' in the $PATH. return $defaultDrushScript; } @@ -91,15 +103,14 @@ public function drushScript(SiteAliasInterface $siteAlias) } /** - * Return the relative path to 'vendor/bin/drush' from the project root. + * Return the relative path to 'vendor/bin/drush' from the Drupal root. */ protected function relativePathToVendorBinDrush() { - $absoluteVendorBin = $_composer_bin_dir ?? Path::join($this->getConfig()->get('drush.vendor-dir'), 'bin'); - $projectRoot = $this->getConfig()->get('runtime.project'); - - $relativeVendorBin = Path::makeRelative($absoluteVendorBin, $projectRoot); - + // https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-bin-dir-from-a-binary + $vendorBin = $GLOBALS['_composer_bin_dir'] ?? Path::join($this->getDrupalFinder()->getVendorDir(), 'bin'); + $drupalRoot = $this->getDrupalFinder()->getDrupalRoot(); + $relativeVendorBin = Path::makeRelative($vendorBin, $drupalRoot); return Path::join($relativeVendorBin, 'drush'); } diff --git a/tests/functional/RsyncTest.php b/tests/functional/RsyncTest.php index 824f2369e2..27312e92ab 100644 --- a/tests/functional/RsyncTest.php +++ b/tests/functional/RsyncTest.php @@ -41,7 +41,7 @@ public function testRsyncSimulated() // the remote side, at which point they will be evaluated & any needed // injection will be done. $this->drush('rsync', ['@example.dev', '@example.stage'], $options, 'user@server/path/to/drupal#sitename'); - $expected = "[notice] Simulating: ssh -o PasswordAuthentication=no user@server '/path/to/drupal/vendor/bin/drush --no-interaction rsync @example.dev @example.stage --uri=sitename"; + $expected = "[notice] Simulating: ssh -o PasswordAuthentication=no user@server '/path/to/vendor/bin/drush --no-interaction rsync @example.dev @example.stage --uri=sitename"; $this->assertStringContainsString($expected, $this->getSimplifiedErrorOutput()); } diff --git a/tests/functional/SqlSyncTest.php b/tests/functional/SqlSyncTest.php index b279b08a81..051865f029 100644 --- a/tests/functional/SqlSyncTest.php +++ b/tests/functional/SqlSyncTest.php @@ -66,7 +66,7 @@ public function testSimulatedSqlSync() // Test simulated remote invoke with a remote runner. $this->drush(SqlSyncCommands::SYNC, ['@synctest.remote', '@synctest.local'], $options, 'user@server/path/to/drupal#sitename'); $output = $this->getSimplifiedErrorOutput(); - $this->assertStringContainsString("[notice] Simulating: ssh -o PasswordAuthentication=no user@server '/path/to/drupal/vendor/bin/drush --no-interaction sql:sync @synctest.remote @synctest.local --uri=sitename'", $output); + $this->assertStringContainsString("[notice] Simulating: ssh -o PasswordAuthentication=no user@server '/path/to/vendor/bin/drush --no-interaction sql:sync @synctest.remote @synctest.local --uri=sitename'", $output); } /** diff --git a/tests/unish/CommandUnishTestCase.php b/tests/unish/CommandUnishTestCase.php index ed3c0328f7..6b7934ea5c 100644 --- a/tests/unish/CommandUnishTestCase.php +++ b/tests/unish/CommandUnishTestCase.php @@ -189,7 +189,7 @@ protected function prepareDrushCommand(string $command, array $args = [], array $cmd[] = '2>' . $this->bitBucket(); } // Remove NULLs - $exec = @array_filter($cmd, 'strlen'); + $exec = array_filter($cmd, fn ($value) => !is_null($value)); $cmd = implode(' ', $exec); return [$cmd, $coverage_file]; }