Support for google3 locations in 'packageRoot' and KernelCompilationService.

Change-Id: I90910bc3c966f648ade107d10d4c7f593c67c7a4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242162
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/summary2/kernel_compilation_service.dart b/pkg/analyzer/lib/src/summary2/kernel_compilation_service.dart
index e7ed68c..36caada 100644
--- a/pkg/analyzer/lib/src/summary2/kernel_compilation_service.dart
+++ b/pkg/analyzer/lib/src/summary2/kernel_compilation_service.dart
@@ -36,11 +36,7 @@
     }
 
     final executablePath = io.Platform.resolvedExecutable;
-    final binPath = package_path.dirname(executablePath);
-    final sdkPath = package_path.dirname(binPath);
-
-    final frontEndSnapshotPath = package_path.join(
-        binPath, 'snapshots', 'frontend_server.dart.snapshot');
+    final sdkPaths = _computeSdkPaths();
 
     final socketCompleter = Completer<io.Socket>();
     final serverSocket = await _loopbackServerSocket();
@@ -52,7 +48,7 @@
     final addressStr = '$host:${serverSocket.port}';
 
     final process = await io.Process.start(executablePath, [
-      frontEndSnapshotPath,
+      sdkPaths.frontEndSnapshot,
       '--binary-protocol-address=$addressStr',
     ]);
 
@@ -66,8 +62,7 @@
     final requestChannel = RequestChannel(socket);
 
     // Put the platform dill.
-    final platformDillPath = package_path.join(
-        sdkPath, 'lib', '_internal', 'vm_platform_strong.dill');
+    final platformDillPath = sdkPaths.platformDill;
     final platformDillBytes = io.File(platformDillPath).readAsBytesSync();
     await requestChannel.sendRequest<void>('dill.put', {
       'uri': 'dill:vm',
@@ -149,6 +144,30 @@
     });
   }
 
+  static _SdkPaths _computeSdkPaths() {
+    // Check for google3.
+    final runFiles = io.Platform.environment['RUNFILES'];
+    if (runFiles != null) {
+      final frontServerPath = io.Platform.environment['FRONTEND_SERVER_PATH']!;
+      final platformDillPath = io.Platform.environment['PLATFORM_DILL_PATH']!;
+      return _SdkPaths(
+        frontEndSnapshot: package_path.join(runFiles, frontServerPath),
+        platformDill: package_path.join(runFiles, platformDillPath),
+      );
+    }
+
+    final executablePath = io.Platform.resolvedExecutable;
+    final binPath = package_path.dirname(executablePath);
+    final sdkPath = package_path.dirname(binPath);
+
+    return _SdkPaths(
+      frontEndSnapshot: package_path.join(
+          binPath, 'snapshots', 'frontend_server.dart.snapshot'),
+      platformDill: package_path.join(
+          sdkPath, 'lib', '_internal', 'vm_platform_strong.dill'),
+    );
+  }
+
   static Future<io.ServerSocket> _loopbackServerSocket() async {
     try {
       return await io.ServerSocket.bind(io.InternetAddress.loopbackIPv6, 0);
@@ -199,3 +218,13 @@
     }
   }
 }
+
+class _SdkPaths {
+  final String frontEndSnapshot;
+  final String platformDill;
+
+  _SdkPaths({
+    required this.frontEndSnapshot,
+    required this.platformDill,
+  });
+}
diff --git a/pkg/analyzer_utilities/lib/package_root.dart b/pkg/analyzer_utilities/lib/package_root.dart
index aa3d27c..61a3c96 100644
--- a/pkg/analyzer_utilities/lib/package_root.dart
+++ b/pkg/analyzer_utilities/lib/package_root.dart
@@ -25,5 +25,13 @@
   if (pkgIndex != -1) {
     return pathos.joinAll(parts.sublist(0, pkgIndex + 1)) + pathos.separator;
   }
+  // Try google3 environment. We expect that all packages that will be
+  // accessed via this root are configured in the BUILD file, and located
+  // inside this single root.
+  final runFiles = Platform.environment['RUNFILES'];
+  final analyzerPackagesRoot = Platform.environment['ANALYZER_PACKAGES_ROOT'];
+  if (runFiles != null && analyzerPackagesRoot != null) {
+    return pathos.join(runFiles, analyzerPackagesRoot);
+  }
   throw StateError('Unable to find sdk/pkg/ in $scriptPath');
 }