From 22b43bf68f6da73bb28e9db5c45f1c7db741b02f Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Wed, 20 Apr 2022 14:41:27 +0000
Subject: [PATCH] Issue #3273008 by phenaproxima, tedbow: Automatic Updates is
 incompatible with the global Drush launcher

---
 composer.json                       |  3 ++-
 package_manager/src/PathLocator.php | 24 ++++++++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/composer.json b/composer.json
index 4e4fb70d26..1d6b9da4c2 100644
--- a/composer.json
+++ b/composer.json
@@ -14,7 +14,8 @@
     "ext-json": "*",
     "drupal/core": "^9.2",
     "php-tuf/composer-stager": "0.3.0",
-    "composer/composer": "^2.2.12 || ^2.3.5"
+    "composer/composer": "^2.2.12 || ^2.3.5",
+    "composer-runtime-api": "^2.0.9"
   },
   "config": {
     "platform": {
diff --git a/package_manager/src/PathLocator.php b/package_manager/src/PathLocator.php
index 36590148b6..e381ff25c6 100644
--- a/package_manager/src/PathLocator.php
+++ b/package_manager/src/PathLocator.php
@@ -47,8 +47,28 @@ class PathLocator {
    *   The absolute path of the vendor directory.
    */
   public function getVendorDirectory(): string {
-    $reflector = new \ReflectionClass(ClassLoader::class);
-    return dirname($reflector->getFileName(), 2);
+    // There may be multiple class loaders at work.
+    // ClassLoader::getRegisteredLoaders() keeps track of them all, indexed by
+    // the path of the vendor directory they load classes from.
+    $loaders = ClassLoader::getRegisteredLoaders();
+
+    // If there's only one class loader, we don't need to search for the right
+    // one.
+    if (count($loaders) === 1) {
+      return key($loaders);
+    }
+
+    // To determine which class loader is the one for Drupal's vendor directory,
+    // look for the loader whose vendor path starts the same way as the path to
+    // this file.
+    foreach (array_keys($loaders) as $path) {
+      if (str_starts_with(__FILE__, dirname($path))) {
+        return $path;
+      }
+    }
+    // If we couldn't find a match, assume that the first registered class
+    // loader is the one we want.
+    return key($loaders);
   }
 
   /**
-- 
GitLab