Write library units before augmentations.

This makes it consistent with how we build elements - first the
defining unit, then augmentations.

Change-Id: I1cfdca462392d8df19a864af22776759a8f9971a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/364381
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart
index d2d56a3..a1b807b2 100644
--- a/pkg/analyzer/lib/src/dart/analysis/driver.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart
@@ -95,7 +95,7 @@
 // TODO(scheglov): Clean up the list of implicitly analyzed files.
 class AnalysisDriver {
   /// The version of data format, should be incremented on every format change.
-  static const int DATA_VERSION = 357;
+  static const int DATA_VERSION = 358;
 
   /// The number of exception contexts allowed to write. Once this field is
   /// zero, we stop writing any new exception contexts in this process.
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index edc0e4a..4907e30 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -696,25 +696,35 @@
     var analysisSession = _elementFactory.analysisSession;
 
     _reader.offset = _offset;
-    var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
 
     // TODO(scheglov): https://github.com/dart-lang/sdk/issues/51855
     // This should not be needed.
     // But I have a suspicion that we attempt to read the library twice.
     _classMembersLengthsIndex = 0;
 
+    // Read enough data to create the library.
     var name = _reader.readStringReference();
     var featureSet = _readFeatureSet();
 
+    // Create the library, link to the reference.
     var libraryElement = LibraryElementImpl(
         analysisContext, analysisSession, name, -1, 0, featureSet);
     _reference.element = libraryElement;
     libraryElement.reference = _reference;
 
-    libraryElement.languageVersion = _readLanguageVersion();
-    _readLibraryOrAugmentationElement(libraryElement);
+    // Read the rest of non-resolution data for the library.
     LibraryElementFlags.read(_reader, libraryElement);
+    libraryElement.languageVersion = _readLanguageVersion();
 
+    libraryElement.exportedReferences = _reader.readTypedList(
+      _readExportedReference,
+    );
+
+    libraryElement.nameUnion = ElementNameUnion.read(
+      _reader.readUInt30List(),
+    );
+
+    // Read the library units.
     libraryElement.definingCompilationUnit = _readUnitElement(
       containerSource: librarySource,
       unitSource: librarySource,
@@ -727,13 +737,8 @@
       );
     });
 
-    libraryElement.exportedReferences = _reader.readTypedList(
-      _readExportedReference,
-    );
-
-    libraryElement.nameUnion = ElementNameUnion.read(
-      _reader.readUInt30List(),
-    );
+    var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
+    _readLibraryOrAugmentationElement(libraryElement);
 
     var accessorAugmentationsOffset = _reader.readUInt30();
 
diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
index f161344..68a956f 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
@@ -115,10 +115,21 @@
     _accessorAugmentations = [];
     _propertyAugmentations = [];
 
-    _sink.writeUInt30(_resolutionSink.offset);
+    // Write non-resolution data for the library.
     _sink._writeStringReference(libraryElement.name);
     _writeFeatureSet(libraryElement.featureSet);
+    LibraryElementFlags.write(_sink, libraryElement);
     _writeLanguageVersion(libraryElement.languageVersion);
+    _writeExportedReferences(libraryElement.exportedReferences);
+    _sink.writeUint30List(libraryElement.nameUnion.mask);
+
+    // Write the library units.
+    // This will write also resolution data, e.g. for classes.
+    _writeUnitElement(libraryElement.definingCompilationUnit);
+    _writeList(libraryElement.parts, _writePartElement);
+
+    // Write resolution data for the library.
+    _sink.writeUInt30(_resolutionSink.offset);
     _writeLibraryOrAugmentationElement(libraryElement);
     for (var partElement in libraryElement.parts) {
       _resolutionSink._writeAnnotationList(partElement.metadata);
@@ -127,13 +138,6 @@
     _resolutionSink.writeElement(libraryElement.entryPoint);
     _writeFieldNameNonPromotabilityInfo(
         libraryElement.fieldNameNonPromotabilityInfo);
-    LibraryElementFlags.write(_sink, libraryElement);
-    _writeUnitElement(libraryElement.definingCompilationUnit);
-    _writeList(libraryElement.parts, _writePartElement);
-
-    _writeExportedReferences(libraryElement.exportedReferences);
-
-    _sink.writeUint30List(libraryElement.nameUnion.mask);
 
     _writePropertyAccessorAugmentations();