@@ -7,10 +7,9 @@ import 'dart:isolate';
77
88import '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;
10+ /// The compiled version of the analysis server snapshot that should be used
11+ /// for integration tests. Set by [getAnalysisServerPath] .
12+ String ? _compiledServerPath;
1413
1514/// The path of the `pkg/analysis_server` folder, resolved using the active
1615/// `package_config.json` .
@@ -41,43 +40,58 @@ String get sdkRootPath {
4140/// Gets the path of the analysis server entry point which may be the snapshot
4241/// from the current SDK (default) or a compiled version of the source script
4342/// 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.
4743Future <String > getAnalysisServerPath (String dartSdkPath) async {
48- // Always use the "real" SDK binary for compilation, not the path provided,
49- // which might be an incomplete SDK that is the target of the test.
50- var dartBinary = Platform .resolvedExecutable;
51- var snapshotPath = path.join (
52- dartSdkPath,
53- 'bin' ,
54- 'snapshots' ,
55- 'analysis_server.dart.snapshot' ,
56- );
57- var sourcePath = path.join (analysisServerPackagePath, 'bin' , 'server.dart' );
58-
59- // Use the SDK snapshot unless this env var is set.
60- var useSnapshot = Platform .environment['TEST_SERVER_SNAPSHOT' ] != 'false' ;
61- if (useSnapshot) {
62- return path.normalize (snapshotPath);
44+ // If we have already computed the path once, use that.
45+ if (_compiledServerPath case var compiledServerPath? ) {
46+ return compiledServerPath;
6347 }
6448
65- // If we've already compiled a copy in this process, use that.
66- if (_temporaryCompiledServerPath case var temporaryCompiledServerPath? ) {
67- return temporaryCompiledServerPath;
49+ // The 'TEST_SERVER_SNAPSHOT' env variable can either be:
50+ //
51+ // - Unset, in which case the tests run from the SDK compiled snapshot
52+ // - The string 'false' which will cause the server to compiled from source.
53+ // This is a simple way to run from source using the test_all file that
54+ // runs all tests in a single isolate and will trigger a single
55+ // compilation for all integration tests.
56+ // - An path to a pre-compiled snapshot.
57+ // This allows configuring VS Code to pre-compile the snapshot once and
58+ // then use 'dart test' which will run each sweet in a separate isolate
59+ // without each test/isolate having to compile.
60+ var snapshotPath = switch (Platform .environment['TEST_SERVER_SNAPSHOT' ]) {
61+ // Compile on the fly
62+ 'false' => await _compileTemporaryServerSnapshot (),
63+ // Default, use the SDK-bundled snapshot
64+ 'true' || '' || null => path.join (
65+ dartSdkPath,
66+ 'bin' ,
67+ 'snapshots' ,
68+ 'analysis_server.dart.snapshot' ,
69+ ),
70+ // Custom path in the env variable
71+ String snapshotPath => snapshotPath,
72+ };
73+
74+ return _compiledServerPath = path.normalize (snapshotPath);
75+ }
76+
77+ /// Compiles a temporary snapshot for the analysis server into a temporary file
78+ /// and returns the full path.
79+ ///
80+ /// This function can only be called once in a single process/isolate.
81+ Future <String > _compileTemporaryServerSnapshot () async {
82+ if (_compiledServerPath != null ) {
83+ throw 'Snapshot is already compiled or being compiled' ;
6884 }
6985
70- // Otherwise, we're running from source. But to avoid having to compile the
71- // server for every test as it spawns the process, pre-compile it once here
72- // into a temp file and then use that for future invocations in this run.
86+ var dartBinary = Platform .resolvedExecutable;
7387 var tempSnapshotDirectory = Directory .systemTemp.createTempSync (
7488 'dart_analysis_server_tests' ,
7589 );
76- var tempSnapshotFilePath =
77- _temporaryCompiledServerPath = path. join (
78- tempSnapshotDirectory.path ,
79- 'analysis_server.dart.snapshot' ,
80- );
90+ var tempSnapshotFilePath = path. join (
91+ tempSnapshotDirectory.path,
92+ 'analysis_server.dart.snapshot' ,
93+ );
94+ var sourcePath = path. join (analysisServerPackagePath, 'bin' , 'server.dart' );
8195 var result = await Process .run (dartBinary, [
8296 'compile' ,
8397 'kernel' ,
0 commit comments