Version 2.14.0-312.0.dev

Merge commit '723df3d4a47987cd35e6effe0703a441e27a6544' into 'dev'
diff --git a/pkg/analyzer/doc/tutorial/ast.md b/pkg/analyzer/doc/tutorial/ast.md
index 3e9e3fa..ed449d8 100644
--- a/pkg/analyzer/doc/tutorial/ast.md
+++ b/pkg/analyzer/doc/tutorial/ast.md
@@ -55,7 +55,7 @@
 
 ```dart
 void processFile(AnalysisSession session, String path) {
-  var result = session.getParsedUnit2(path);
+  var result = session.getParsedUnit(path);
   if (result is ParsedUnitResult) {
     CompilationUnit unit = result.unit;
   }
@@ -67,7 +67,7 @@
 
 ```dart
 void processFile(AnalysisSession session, String path) async {
-  var result = await session.getResolvedUnit2(path);
+  var result = await session.getResolvedUnit(path);
   if (result is ResolvedUnitResult) {
     CompilationUnit unit = result.unit;
   }
diff --git a/pkg/analyzer/doc/tutorial/element.md b/pkg/analyzer/doc/tutorial/element.md
index 6c5509f..912d65b 100644
--- a/pkg/analyzer/doc/tutorial/element.md
+++ b/pkg/analyzer/doc/tutorial/element.md
@@ -46,7 +46,7 @@
 
 ```dart
 void analyzeSingleFile(AnalysisSession session, String path) async {
-  var result = await session.getUnitElement2(path);
+  var result = await session.getUnitElement(path);
   if (result is UnitElementResult) {
     CompilationUnitElement element = result.element;
   }
diff --git a/pkg/analyzer/lib/src/dart/analysis/context_builder.dart b/pkg/analyzer/lib/src/dart/analysis/context_builder.dart
index 0ca758d..3e2f98b 100644
--- a/pkg/analyzer/lib/src/dart/analysis/context_builder.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/context_builder.dart
@@ -7,9 +7,9 @@
 import 'package:analyzer/dart/analysis/declared_variables.dart';
 import 'package:analyzer/file_system/file_system.dart';
 import 'package:analyzer/file_system/physical_file_system.dart';
-import 'package:analyzer/src/context/builder.dart' as old
-    show ContextBuilder, ContextBuilderOptions;
-import 'package:analyzer/src/context/context_root.dart' as old;
+import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
+import 'package:analyzer/src/context/builder.dart' show EmbedderYamlLocator;
+import 'package:analyzer/src/context/packages.dart';
 import 'package:analyzer/src/dart/analysis/byte_store.dart'
     show ByteStore, MemoryByteStore;
 import 'package:analyzer/src/dart/analysis/driver.dart'
@@ -18,8 +18,15 @@
 import 'package:analyzer/src/dart/analysis/file_content_cache.dart';
 import 'package:analyzer/src/dart/analysis/performance_logger.dart'
     show PerformanceLog;
+import 'package:analyzer/src/dart/sdk/sdk.dart';
 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
-import 'package:analyzer/src/generated/sdk.dart' show DartSdkManager;
+import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
+import 'package:analyzer/src/generated/source.dart';
+import 'package:analyzer/src/hint/sdk_constraint_extractor.dart';
+import 'package:analyzer/src/summary/package_bundle_reader.dart';
+import 'package:analyzer/src/summary/summary_sdk.dart';
+import 'package:analyzer/src/task/options.dart';
+import 'package:analyzer/src/workspace/workspace.dart';
 import 'package:cli_util/cli_util.dart';
 
 /// An implementation of a context builder.
@@ -57,46 +64,57 @@
     byteStore ??= MemoryByteStore();
     performanceLog ??= PerformanceLog(StringBuffer());
 
-    DartSdkManager sdkManager = DartSdkManager(sdkPath);
-
     if (scheduler == null) {
       scheduler = AnalysisDriverScheduler(performanceLog);
       scheduler.start();
     }
 
-    // TODO(brianwilkerson) Move the required implementation from the old
-    // ContextBuilder to this class and remove the old class.
-    old.ContextBuilderOptions options = old.ContextBuilderOptions();
-    if (declaredVariables != null) {
-      options.declaredVariables = _toMap(declaredVariables);
-    }
-    if (sdkSummaryPath != null) {
-      options.dartSdkSummaryPath = sdkSummaryPath;
-    }
+    SummaryDataStore? summaryData;
     if (librarySummaryPaths != null) {
-      options.librarySummaryPaths = librarySummaryPaths;
+      summaryData = SummaryDataStore(librarySummaryPaths);
     }
-    options.defaultAnalysisOptionsFilePath = contextRoot.optionsFile?.path;
-    options.defaultPackageFilePath = contextRoot.packagesFile?.path;
 
-    old.ContextBuilder builder =
-        old.ContextBuilder(resourceProvider, sdkManager, options: options);
-    builder.analysisDriverScheduler = scheduler;
-    builder.byteStore = byteStore;
-    builder.enableIndex = enableIndex;
-    builder.performanceLog = performanceLog;
-    builder.retainDataForTesting = retainDataForTesting;
-
-    old.ContextRoot oldContextRoot = old.ContextRoot(
-        contextRoot.root.path, contextRoot.excludedPaths.toList(),
-        pathContext: resourceProvider.pathContext);
-    AnalysisDriver driver = builder.buildDriver(
-      oldContextRoot,
-      contextRoot.workspace,
-      fileContentCache: fileContentCache,
-      updateAnalysisOptions: updateAnalysisOptions,
+    var workspace = contextRoot.workspace;
+    var sdk = _createSdk(
+      workspace: workspace,
+      sdkPath: sdkPath,
+      sdkSummaryPath: sdkSummaryPath,
     );
 
+    // TODO(scheglov) Ensure that "librarySummaryPaths" not null only
+    // when "sdkSummaryPath" is not null.
+    if (sdk is SummaryBasedDartSdk) {
+      summaryData?.addBundle(null, sdk.bundle);
+    }
+
+    var sourceFactory = workspace.createSourceFactory(sdk, summaryData);
+
+    var options = _getAnalysisOptions(contextRoot, sourceFactory);
+    if (updateAnalysisOptions != null) {
+      updateAnalysisOptions(options);
+    }
+
+    var driver = AnalysisDriver.tmp1(
+      scheduler: scheduler,
+      logger: performanceLog,
+      resourceProvider: resourceProvider,
+      byteStore: byteStore,
+      sourceFactory: sourceFactory,
+      analysisOptions: options,
+      packages: _createPackageMap(
+        contextRoot: contextRoot,
+      ),
+      enableIndex: enableIndex,
+      externalSummaries: summaryData,
+      retainDataForTesting: retainDataForTesting,
+      fileContentCache: fileContentCache,
+    );
+
+    if (declaredVariables != null) {
+      driver.declaredVariables = declaredVariables;
+      driver.configure();
+    }
+
     // AnalysisDriver reports results into streams.
     // We need to drain these streams to avoid memory leak.
     if (drainStreams) {
@@ -111,13 +129,103 @@
     return context;
   }
 
-  /// Convert the [declaredVariables] into a map for use with the old context
-  /// builder.
-  Map<String, String> _toMap(DeclaredVariables declaredVariables) {
-    Map<String, String> map = <String, String>{};
-    for (String name in declaredVariables.variableNames) {
-      map[name] = declaredVariables.get(name)!;
+  /// Return [Packages] to analyze the [contextRoot].
+  ///
+  /// TODO(scheglov) Get [Packages] from [Workspace]?
+  Packages _createPackageMap({
+    required ContextRoot contextRoot,
+  }) {
+    var configFile = contextRoot.packagesFile;
+    if (configFile != null) {
+      return parsePackagesFile(resourceProvider, configFile);
+    } else {
+      return Packages.empty;
     }
-    return map;
+  }
+
+  /// Return the SDK that that should be used to analyze code.
+  DartSdk _createSdk({
+    required Workspace workspace,
+    String? sdkPath,
+    String? sdkSummaryPath,
+  }) {
+    if (sdkSummaryPath != null) {
+      return SummaryBasedDartSdk(sdkSummaryPath, true,
+          resourceProvider: resourceProvider);
+    }
+
+    var folderSdk = FolderBasedDartSdk(
+      resourceProvider,
+      resourceProvider.getFolder(sdkPath!),
+    );
+
+    {
+      // TODO(scheglov) We already had partial SourceFactory in ContextLocatorImpl.
+      var partialSourceFactory = workspace.createSourceFactory(null, null);
+      var embedderYamlSource = partialSourceFactory.forUri(
+        'package:sky_engine/_embedder.yaml',
+      );
+      if (embedderYamlSource != null) {
+        var embedderYamlPath = embedderYamlSource.fullName;
+        var libFolder = resourceProvider.getFile(embedderYamlPath).parent2;
+        var locator = EmbedderYamlLocator.forLibFolder(libFolder);
+        var embedderMap = locator.embedderYamls;
+        if (embedderMap.isNotEmpty) {
+          return EmbedderSdk(
+            resourceProvider,
+            embedderMap,
+            languageVersion: folderSdk.languageVersion,
+          );
+        }
+      }
+    }
+
+    return folderSdk;
+  }
+
+  /// Return the `pubspec.yaml` file that should be used when analyzing code in
+  /// the [contextRoot], possibly `null`.
+  ///
+  /// TODO(scheglov) Get it from [Workspace]?
+  File? _findPubspecFile(ContextRoot contextRoot) {
+    for (var current in contextRoot.root.withAncestors) {
+      var file = current.getChildAssumingFile('pubspec.yaml');
+      if (file.exists) {
+        return file;
+      }
+    }
+  }
+
+  /// Return the analysis options that should be used to analyze code in the
+  /// [contextRoot].
+  ///
+  /// TODO(scheglov) We have already loaded it once in [ContextLocatorImpl].
+  AnalysisOptionsImpl _getAnalysisOptions(
+    ContextRoot contextRoot,
+    SourceFactory sourceFactory,
+  ) {
+    var options = AnalysisOptionsImpl();
+
+    var optionsFile = contextRoot.optionsFile;
+    if (optionsFile != null) {
+      try {
+        var provider = AnalysisOptionsProvider(sourceFactory);
+        var optionsMap = provider.getOptionsFromFile(optionsFile);
+        applyToAnalysisOptions(options, optionsMap);
+      } catch (e) {
+        // ignore
+      }
+    }
+
+    var pubspecFile = _findPubspecFile(contextRoot);
+    if (pubspecFile != null) {
+      var extractor = SdkConstraintExtractor(pubspecFile);
+      var sdkVersionConstraint = extractor.constraint();
+      if (sdkVersionConstraint != null) {
+        options.sdkVersionConstraint = sdkVersionConstraint;
+      }
+    }
+
+    return options;
   }
 }
diff --git a/pkg/dartdev/test/commands/test_test.dart b/pkg/dartdev/test/commands/test_test.dart
index b493403..bda022bb 100644
--- a/pkg/dartdev/test/commands/test_test.dart
+++ b/pkg/dartdev/test/commands/test_test.dart
@@ -30,7 +30,7 @@
     expect(result.stdout, startsWith('''
 Runs tests in this package.
 
-Usage: pub run test [files or directories...]
+Usage: dart test [files or directories...]
 '''));
     expect(result.stderr, isEmpty);
   });
diff --git a/sdk/lib/_internal/vm/lib/isolate_patch.dart b/sdk/lib/_internal/vm/lib/isolate_patch.dart
index 34ce5e4..9703179 100644
--- a/sdk/lib/_internal/vm/lib/isolate_patch.dart
+++ b/sdk/lib/_internal/vm/lib/isolate_patch.dart
@@ -160,8 +160,8 @@
   }
 
   /**** Internal implementation details ****/
-  _get_id() native "RawReceivePortImpl_get_id";
-  _get_sendport() native "RawReceivePortImpl_get_sendport";
+  int _get_id() native "RawReceivePortImpl_get_id";
+  SendPort _get_sendport() native "RawReceivePortImpl_get_sendport";
 
   // Called from the VM to retrieve the handler for a message.
   @pragma("vm:entry-point", "call")
@@ -186,7 +186,7 @@
   }
 
   // Call into the VM to close the VM maintained mappings.
-  _closeInternal() native "RawReceivePortImpl_closeInternal";
+  int _closeInternal() native "RawReceivePortImpl_closeInternal";
 
   // Set this port as active or inactive in the VM. If inactive, this port
   // will not be considered live even if it hasn't been explicitly closed.
@@ -195,7 +195,7 @@
   _setActive(bool active) native "RawReceivePortImpl_setActive";
 
   void set handler(Function? value) {
-    final id = this._get_id();
+    final int id = this._get_id();
     if (!_portMap.containsKey(id)) {
       _portMap[id] = <String, dynamic>{
         'port': this,
@@ -204,7 +204,7 @@
     _portMap[id]!['handler'] = value;
   }
 
-  static final _portMap = <dynamic, Map<String, dynamic>>{};
+  static final _portMap = <int, Map<String, dynamic>>{};
 }
 
 @pragma("vm:entry-point")
diff --git a/tools/VERSION b/tools/VERSION
index ce25a45..d136bd2 100644
--- a/tools/VERSION
+++ b/tools/VERSION
@@ -27,5 +27,5 @@
 MAJOR 2
 MINOR 14
 PATCH 0
-PRERELEASE 311
+PRERELEASE 312
 PRERELEASE_PATCH 0
\ No newline at end of file