[analysis_server] [linter] Fix some additional tests when running through the test runner

`Platform.script` doesn't work when run through `dart test` (see https://github.com/dart-lang/test/issues/110). This uses `Isolate.resolvePackageUriSync` to locate package roots instead.

Change-Id: Ieda05cd625cf07152d695daf94c99f1fad1cefe3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419720
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/analysis_server/test/integration/lsp_server/integration_tests.dart b/pkg/analysis_server/test/integration/lsp_server/integration_tests.dart
index afa8585..66d18b2 100644
--- a/pkg/analysis_server/test/integration/lsp_server/integration_tests.dart
+++ b/pkg/analysis_server/test/integration/lsp_server/integration_tests.dart
@@ -5,6 +5,7 @@
 import 'dart:async';
 import 'dart:convert';
 import 'dart:io';
+import 'dart:isolate';
 
 import 'package:analysis_server/lsp_protocol/protocol.dart';
 import 'package:analysis_server/src/lsp/channel/lsp_byte_stream_channel.dart';
@@ -190,19 +191,6 @@
     _process?.kill();
   }
 
-  /// Find the root directory of the analysis_server package by proceeding
-  /// upward to the 'test' dir, and then going up one more directory.
-  String findRoot(String pathname) {
-    while (!['benchmark', 'test'].contains(basename(pathname))) {
-      var parent = dirname(pathname);
-      if (parent.length >= pathname.length) {
-        throw Exception("Can't find root directory");
-      }
-      pathname = parent;
-    }
-    return dirname(pathname);
-  }
-
   Future<void> start({
     required String dartSdkPath,
     List<String>? vmArgs,
@@ -219,14 +207,21 @@
     String serverPath;
 
     if (useSnapshot) {
+      // TODO(dantup): Consider changing this to "dart language_server" and
+      //  sharing this code with legacy-server integration tests.
       serverPath = normalize(
         join(dartSdkPath, 'bin', 'snapshots', 'analysis_server.dart.snapshot'),
       );
     } else {
-      var rootDir = findRoot(
-        Platform.script.toFilePath(windows: Platform.isWindows),
+      // Locate the root of the analysis server package without using
+      // `Platform.script` as it fails when run through the `dart test`.
+      // https://github.com/dart-lang/test/issues/110
+      var serverLibUri = await Isolate.resolvePackageUri(
+        Uri.parse('package:analysis_server/'),
       );
-      serverPath = normalize(join(rootDir, 'bin', 'server.dart'));
+      serverPath = normalize(
+        join(serverLibUri!.toFilePath(), '..', 'bin', 'server.dart'),
+      );
     }
 
     var arguments = [...?vmArgs, serverPath, '--lsp', '--suppress-analytics'];
diff --git a/pkg/analysis_server/test/integration/support/integration_tests.dart b/pkg/analysis_server/test/integration/support/integration_tests.dart
index bb9f083..b0023e6 100644
--- a/pkg/analysis_server/test/integration/support/integration_tests.dart
+++ b/pkg/analysis_server/test/integration/support/integration_tests.dart
@@ -684,6 +684,8 @@
     String serverPath;
 
     if (useSnapshot) {
+      // TODO(dantup): Consider changing this to "dart language_server" and
+      //  sharing this code with LSP integration tests.
       serverPath = path.normalize(
         path.join(
           dartSdkPath,
diff --git a/pkg/linter/tool/util/path_utils.dart b/pkg/linter/tool/util/path_utils.dart
index 9438fce..5122f2d 100644
--- a/pkg/linter/tool/util/path_utils.dart
+++ b/pkg/linter/tool/util/path_utils.dart
@@ -3,13 +3,21 @@
 // BSD-style license that can be found in the LICENSE file.
 
 import 'dart:io';
+import 'dart:isolate';
 
 import 'package:path/path.dart' as path;
 
 String get linterPackageRoot => path.joinAll(_packageRoot);
 
 List<String> get _packageRoot {
-  var parts = path.split(path.dirname(path.fromUri(Platform.script.path)));
+  // Locate the root of the package without using `Platform.script` as it fails
+  // when run through the `dart test`.
+  // https://github.com/dart-lang/test/issues/110
+  var packageLibUri = Isolate.resolvePackageUriSync(
+    Uri.parse('package:linter/'),
+  );
+
+  var parts = path.split(path.dirname(packageLibUri!.toFilePath()));
   while (parts.last != 'linter') {
     parts.removeLast();
     if (parts.isEmpty) {