Skip to content

Commit 22e3e96

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] [linter] Fix some additional tests when running through the test runner
`Platform.script` doesn't work when run through `dart test` (see dart-lang/test#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 <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 45fa728 commit 22e3e96

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

pkg/analysis_server/test/integration/lsp_server/integration_tests.dart

+11-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io';
8+
import 'dart:isolate';
89

910
import 'package:analysis_server/lsp_protocol/protocol.dart';
1011
import 'package:analysis_server/src/lsp/channel/lsp_byte_stream_channel.dart';
@@ -190,19 +191,6 @@ class LspServerClient {
190191
_process?.kill();
191192
}
192193

193-
/// Find the root directory of the analysis_server package by proceeding
194-
/// upward to the 'test' dir, and then going up one more directory.
195-
String findRoot(String pathname) {
196-
while (!['benchmark', 'test'].contains(basename(pathname))) {
197-
var parent = dirname(pathname);
198-
if (parent.length >= pathname.length) {
199-
throw Exception("Can't find root directory");
200-
}
201-
pathname = parent;
202-
}
203-
return dirname(pathname);
204-
}
205-
206194
Future<void> start({
207195
required String dartSdkPath,
208196
List<String>? vmArgs,
@@ -219,14 +207,21 @@ class LspServerClient {
219207
String serverPath;
220208

221209
if (useSnapshot) {
210+
// TODO(dantup): Consider changing this to "dart language_server" and
211+
// sharing this code with legacy-server integration tests.
222212
serverPath = normalize(
223213
join(dartSdkPath, 'bin', 'snapshots', 'analysis_server.dart.snapshot'),
224214
);
225215
} else {
226-
var rootDir = findRoot(
227-
Platform.script.toFilePath(windows: Platform.isWindows),
216+
// Locate the root of the analysis server package without using
217+
// `Platform.script` as it fails when run through the `dart test`.
218+
// https://github.com/dart-lang/test/issues/110
219+
var serverLibUri = await Isolate.resolvePackageUri(
220+
Uri.parse('package:analysis_server/'),
221+
);
222+
serverPath = normalize(
223+
join(serverLibUri!.toFilePath(), '..', 'bin', 'server.dart'),
228224
);
229-
serverPath = normalize(join(rootDir, 'bin', 'server.dart'));
230225
}
231226

232227
var arguments = [...?vmArgs, serverPath, '--lsp', '--suppress-analytics'];

pkg/analysis_server/test/integration/support/integration_tests.dart

+2
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ class Server {
684684
String serverPath;
685685

686686
if (useSnapshot) {
687+
// TODO(dantup): Consider changing this to "dart language_server" and
688+
// sharing this code with LSP integration tests.
687689
serverPath = path.normalize(
688690
path.join(
689691
dartSdkPath,

pkg/linter/tool/util/path_utils.dart

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:io';
6+
import 'dart:isolate';
67

78
import 'package:path/path.dart' as path;
89

910
String get linterPackageRoot => path.joinAll(_packageRoot);
1011

1112
List<String> get _packageRoot {
12-
var parts = path.split(path.dirname(path.fromUri(Platform.script.path)));
13+
// Locate the root of the package without using `Platform.script` as it fails
14+
// when run through the `dart test`.
15+
// https://github.com/dart-lang/test/issues/110
16+
var packageLibUri = Isolate.resolvePackageUriSync(
17+
Uri.parse('package:linter/'),
18+
);
19+
20+
var parts = path.split(path.dirname(packageLibUri!.toFilePath()));
1321
while (parts.last != 'linter') {
1422
parts.removeLast();
1523
if (parts.isEmpty) {

0 commit comments

Comments
 (0)