Skip to content

Commit a69ff18

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] When running server from source in integration tests, pre-compile server
When working on the analysis server, you can set the `TEST_SERVER_SNAPSHOT` env var to `false` to spawn the server from source (`bin/server.dart`) instead of running the snapshot, which allows easily running the integration tests (which spawn the server in a separate process) from the IDE without having to run (and wait for) builds. However running from source like this can be quite slow because every test that spawns the server results in it being compiled on-the-fly. This change instead uses `dart compile` to compile the server from source to a temp file the first time we return the path, and then subsequently returns the same path. On my Windows PC, this reduces the time to run the integration tests (with the env variable set) from 17min to 2min. Change-Id: I612d1a978d22d214dc086962d8c9eb2e7dfe8239 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434080 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent c95fde3 commit a69ff18

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class LspServerClient {
210210
}
211211

212212
var dartBinary = path.join(dartSdkPath, 'bin', 'dart');
213-
var serverPath = getAnalysisServerPath(dartSdkPath);
213+
var serverPath = await getAnalysisServerPath(dartSdkPath);
214214

215215
var arguments = [...?vmArgs, serverPath, '--lsp', '--suppress-analytics'];
216216
var process = await Process.start(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ class Server {
730730
_time.start();
731731

732732
var dartBinary = path.join(dartSdkPath, 'bin', 'dart');
733-
var serverPath = getAnalysisServerPath(dartSdkPath);
733+
var serverPath = await getAnalysisServerPath(dartSdkPath);
734734

735735
var arguments = <String>['--disable-dart-dev', '--no-dds'];
736736
//

pkg/analysis_server/test/support/sdk_paths.dart

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import 'dart:isolate';
77

88
import 'package:path/path.dart' as path;
99

10+
/// A temporary file that the server was compiled to from source by
11+
/// [getAnalysisServerPath] because `TEST_SERVER_SNAPSHOT` was set to disable
12+
/// running from the SDK snapshot.
13+
String? _temporaryCompiledServerPath;
14+
1015
/// The path of the `pkg/analysis_server` folder, resolved using the active
1116
/// `package_config.json`.
1217
String get analysisServerPackagePath {
@@ -33,10 +38,14 @@ String get sdkRootPath {
3338
return path.normalize(path.join(analysisServerPackagePath, '..', '..'));
3439
}
3540

36-
/// Gets the path of the analysis server entry point which may be a snapshot
37-
/// or the source script depending on the `TEST_SERVER_SNAPSHOT` environment
38-
/// variable.
39-
String getAnalysisServerPath(String dartSdkPath) {
41+
/// Gets the path of the analysis server entry point which may be the snapshot
42+
/// from the current SDK (default) or a compiled version of the source script
43+
/// depending on the `TEST_SERVER_SNAPSHOT` environment variable.
44+
///
45+
/// When running from source, the server will be compiled to disk for the first
46+
/// request to speed up subsequent tests in the same process.
47+
Future<String> getAnalysisServerPath(String dartSdkPath) async {
48+
var dartBinary = path.join(dartSdkPath, 'bin', 'dart');
4049
var snapshotPath = path.join(
4150
dartSdkPath,
4251
'bin',
@@ -45,9 +54,40 @@ String getAnalysisServerPath(String dartSdkPath) {
4554
);
4655
var sourcePath = path.join(analysisServerPackagePath, 'bin', 'server.dart');
4756

48-
// Setting the `TEST_SERVER_SNAPSHOT` env var to 'false' will disable the
49-
// snapshot and run from source.
57+
// Use the SDK snapshot unless this env var is set.
5058
var useSnapshot = Platform.environment['TEST_SERVER_SNAPSHOT'] != 'false';
51-
var serverPath = useSnapshot ? snapshotPath : sourcePath;
52-
return path.normalize(serverPath);
59+
if (useSnapshot) {
60+
return path.normalize(snapshotPath);
61+
}
62+
63+
// If we've already compiled a copy in this process, use that.
64+
if (_temporaryCompiledServerPath case var temporaryCompiledServerPath?) {
65+
return temporaryCompiledServerPath;
66+
}
67+
68+
// Otherwise, we're running from source. But to avoid having to compile the
69+
// server for every test as it spawns the process, pre-compile it once here
70+
// into a temp file and then use that for future invocations in this run.
71+
var tempSnapshotDirectory = Directory.systemTemp.createTempSync(
72+
'dart_analysis_server_tests',
73+
);
74+
var tempSnapshotFilePath =
75+
_temporaryCompiledServerPath = path.join(
76+
tempSnapshotDirectory.path,
77+
'analysis_server.dart.snapshot',
78+
);
79+
var result = await Process.run(dartBinary, [
80+
'compile',
81+
'kernel',
82+
sourcePath,
83+
'-o',
84+
tempSnapshotFilePath,
85+
]);
86+
if (result.exitCode != 0) {
87+
throw 'Failed to compile analysis server:\n'
88+
'Exit code: ${result.exitCode}\n'
89+
'stdout: ${result.stdout}\n'
90+
'stderr: ${result.stderr}\n';
91+
}
92+
return tempSnapshotFilePath;
5393
}

0 commit comments

Comments
 (0)