Always ensure TypeProvider, even with external summaries include the target library.

There is at least one case (but not many) when this happens internally.

R=brianwilkerson@google.com

Change-Id: Ic940cb836f58467d40ed7b31cb8bb43cf12eee50
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106165
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/library_context.dart b/pkg/analyzer/lib/src/dart/analysis/library_context.dart
index be5e922..87010c4 100644
--- a/pkg/analyzer/lib/src/dart/analysis/library_context.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/library_context.dart
@@ -338,8 +338,7 @@
       // We are about to load dart:core, but if we have just linked it, the
       // linker might have set the type provider. So, clear it, and recreate
       // the element factory - it is empty anyway.
-      var hasDartCoreBeforeBundle = elementFactory.hasDartCore;
-      if (!hasDartCoreBeforeBundle) {
+      if (!elementFactory.hasDartCore) {
         analysisContext.clearTypeProvider();
         _createElementFactory();
       }
@@ -362,10 +361,8 @@
         }
       }
 
-      // If the first bundle, with dart:core, create the type provider.
-      if (!hasDartCoreBeforeBundle && elementFactory.hasDartCore) {
-        _createElementFactoryTypeProvider();
-      }
+      // We might have just linked dart:core, ensure the type provider.
+      _createElementFactoryTypeProvider();
     }
 
     logger.run('Prepare linked bundles', () {
@@ -381,6 +378,11 @@
       );
     });
 
+    // There might be a rare (and wrong) situation, when the external summaries
+    // already include the [targetLibrary]. When this happens, [loadBundle]
+    // exists without doing any work. But the type provider must be created.
+    _createElementFactoryTypeProvider();
+
     timerLoad2.stop();
   }
 
@@ -408,7 +410,10 @@
     }
   }
 
+  /// Ensure that type provider is created.
   void _createElementFactoryTypeProvider() {
+    if (analysisContext.typeProvider != null) return;
+
     var dartCore = elementFactory.libraryOfUri('dart:core');
     var dartAsync = elementFactory.libraryOfUri('dart:async');
     var typeProvider = SummaryTypeProvider()