diff --git a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
index c088eed..0fcd077 100644
--- a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
+++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
@@ -63,6 +63,13 @@ class ExtensionDiscovery {
   protected $infoParser;
 
   /**
+   * Static root path, useful for invalidating static::$files when needed.
+   *
+   * @var string
+   */
+  protected static $cacheRoot;
+
+  /**
    * Previously discovered files keyed by origin directory and extension type.
    *
    * @var array
@@ -77,7 +84,7 @@ class ExtensionDiscovery {
   protected $profileDirectories;
 
   /**
-   * The app root.
+   * The app root for the current operation.
    *
    * @var string
    */
@@ -103,17 +110,27 @@ class ExtensionDiscovery {
    * @param string $root
    *   The app root.
    * @param bool $use_file_cache
-   *   Whether file cache should be used.
-   * @param string[] $profile_directories
-   *   The available profile directories
-   * @param string $site_path
-   *   The path to the site.
+   *   (optional) Whether file cache should be used. Defaults to TRUE.
+   * @param string[]|null $profile_directories
+   *   (optional) The available profile directories. Defaults to NULL, which
+   *   will cause this object to search for profile directories based on
+   *   settings.
+   * @param string|null $site_path
+   *   (optional) The site path within the Drupal installation. Defaults to
+   *   NULL, which will cause this object to determine the site based on the
+   *   request information.
    */
   public function __construct($root, $use_file_cache = TRUE, $profile_directories = NULL, $site_path = NULL) {
     $this->root = $root;
     $this->fileCache = $use_file_cache ? FileCacheFactory::get('extension_discovery') : NULL;
     $this->profileDirectories = $profile_directories;
     $this->sitePath = $site_path;
+    // Invalidate the cache if we're using a new root directory. We must do this
+    // since $files is static.
+    if ($this->root != static::$cacheRoot) {
+      static::$files = [];
+      static::$cacheRoot = $this->root;
+    }
   }
 
   /**
@@ -152,8 +169,9 @@ public function __construct($root, $use_file_cache = TRUE, $profile_directories
   public function scan($type, $include_tests = NULL) {
     // Determine the installation profile directories to scan for extensions,
     // unless explicit profile directories have been set. Exclude profiles as we
-    // cannot have profiles within profiles.
-    if (!isset($this->profileDirectories) && $type != 'profile') {
+    // cannot have profiles within profiles. If no profiles are specified in the
+    // constructor, profileDirectories will be NULL.
+    if (($this->profileDirectories === NULL) && $type != 'profile') {
       $this->setProfileDirectoriesFromSettings();
     }
 
@@ -187,7 +205,7 @@ public function scan($type, $include_tests = NULL) {
       $searchdirs[static::ORIGIN_SITE] = \Drupal::service('site.path');
     }
     else {
-      $searchdirs[static::ORIGIN_SITE] = $this->sitePath ?: DrupalKernel::findSitePath(Request::createFromGlobals());
+      $searchdirs[static::ORIGIN_SITE] = $this->sitePath ? : DrupalKernel::findSitePath(Request::createFromGlobals());
     }
 
     // Unless an explicit value has been passed, manually check whether we are
@@ -198,6 +216,7 @@ public function scan($type, $include_tests = NULL) {
       $include_tests = Settings::get('extension_discovery_scan_tests') || drupal_valid_test_ua();
     }
 
+    // Scan for extensions.
     $files = array();
     foreach ($searchdirs as $dir) {
       // Discover all extensions in the directory, unless we did already.
