test_compiler.dart 7.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:meta/meta.dart';
8
import 'package:package_config/package_config.dart';
9 10 11 12 13 14 15

import '../artifacts.dart';
import '../base/file_system.dart';
import '../build_info.dart';
import '../bundle.dart';
import '../compile.dart';
import '../dart/package_map.dart';
16
import '../globals.dart' as globals;
17 18 19 20
import '../project.dart';

/// A request to the [TestCompiler] for recompilation.
class _CompilationRequest {
21 22 23
  _CompilationRequest(this.mainUri, this.result);

  Uri mainUri;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  Completer<String> result;
}

/// A frontend_server wrapper for the flutter test runner.
///
/// This class is a wrapper around compiler that allows multiple isolates to
/// enqueue compilation requests, but ensures only one compilation at a time.
class TestCompiler {
  /// Creates a new [TestCompiler] which acts as a frontend_server proxy.
  ///
  /// [trackWidgetCreation] configures whether the kernel transform is applied
  /// to the output. This also changes the output file to include a '.track`
  /// extension.
  ///
  /// [flutterProject] is the project for which we are running tests.
  TestCompiler(
40
    this.buildInfo,
41
    this.flutterProject,
42 43 44 45 46 47 48 49 50 51
  ) : testFilePath = globals.fs.path.join(
        flutterProject.directory.path,
        getBuildDirectory(),
        'test_cache',
        getDefaultCachedKernelPath(
          trackWidgetCreation: buildInfo.trackWidgetCreation,
          nullSafetyMode: buildInfo.nullSafetyMode,
          dartDefines: buildInfo.dartDefines,
          extraFrontEndOptions: buildInfo.extraFrontEndOptions,
        )) {
52 53 54
    // Compiler maintains and updates single incremental dill file.
    // Incremental compilation requests done for each test copy that file away
    // for independent execution.
55
    final Directory outputDillDirectory = globals.fs.systemTempDirectory.createTempSync('flutter_test_compiler.');
56
    outputDill = outputDillDirectory.childFile('output.dill');
57 58
    globals.printTrace('Compiler will use the following file as its incremental dill file: ${outputDill.path}');
    globals.printTrace('Listening to compiler controller...');
59
    compilerController.stream.listen(_onCompilationRequest, onDone: () {
60
      globals.printTrace('Deleting ${outputDillDirectory.path}...');
61 62 63 64 65 66 67
      outputDillDirectory.deleteSync(recursive: true);
    });
  }

  final StreamController<_CompilationRequest> compilerController = StreamController<_CompilationRequest>();
  final List<_CompilationRequest> compilationQueue = <_CompilationRequest>[];
  final FlutterProject flutterProject;
68
  final BuildInfo buildInfo;
69 70 71 72 73 74
  final String testFilePath;


  ResidentCompiler compiler;
  File outputDill;

75
  Future<String> compile(Uri mainDart) {
76
    final Completer<String> completer = Completer<String>();
77 78 79
    if (compilerController.isClosed) {
      return null;
    }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    compilerController.add(_CompilationRequest(mainDart, completer));
    return completer.future;
  }

  Future<void> _shutdown() async {
    // Check for null in case this instance is shut down before the
    // lazily-created compiler has been created.
    if (compiler != null) {
      await compiler.shutdown();
      compiler = null;
    }
  }

  Future<void> dispose() async {
    await compilerController.close();
95
    await _shutdown();
96 97 98 99 100
  }

  /// Create the resident compiler used to compile the test.
  @visibleForTesting
  Future<ResidentCompiler> createCompiler() async {
101
    final ResidentCompiler residentCompiler = ResidentCompiler(
102
      globals.artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
103 104 105
      artifacts: globals.artifacts,
      logger: globals.logger,
      processManager: globals.processManager,
106 107
      buildMode: buildInfo.mode,
      trackWidgetCreation: buildInfo.trackWidgetCreation,
108 109
      initializeFromDill: testFilePath,
      unsafePackageSerialization: false,
110
      dartDefines: buildInfo.dartDefines,
111
      packagesPath: globalPackagesPath,
112
      extraFrontEndOptions: buildInfo.extraFrontEndOptions,
113
      platform: globals.platform,
114
      testCompilation: true,
115
    );
116
    return residentCompiler;
117 118
  }

119 120
  PackageConfig _packageConfig;

121 122 123 124 125 126 127 128 129 130
  // Handle a compilation request.
  Future<void> _onCompilationRequest(_CompilationRequest request) async {
    final bool isEmpty = compilationQueue.isEmpty;
    compilationQueue.add(request);
    // Only trigger processing if queue was empty - i.e. no other requests
    // are currently being processed. This effectively enforces "one
    // compilation request at a time".
    if (!isEmpty) {
      return;
    }
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    if (_packageConfig == null) {
      _packageConfig ??= await loadPackageConfigWithLogging(
        globals.fs.file(globalPackagesPath),
        logger: globals.logger,
      );
      // Compilation will fail if there is no flutter_test dependency, since
      // this library is imported by the generated entrypoint script.
      if (_packageConfig['flutter_test'] == null) {
        globals.printError(
          '\n'
          'Error: cannot run without a dependency on "package:flutter_test". '
          'Ensure the following lines are present in your pubspec.yaml:'
          '\n\n'
          'dev_dependencies:\n'
          '  flutter_test:\n'
          '    sdk: flutter\n',
        );
        request.result.complete(null);
        await compilerController.close();
        return;
      }
    }
153 154
    while (compilationQueue.isNotEmpty) {
      final _CompilationRequest request = compilationQueue.first;
155
      globals.printTrace('Compiling ${request.mainUri}');
156 157 158 159 160 161 162
      final Stopwatch compilerTime = Stopwatch()..start();
      bool firstCompile = false;
      if (compiler == null) {
        compiler = await createCompiler();
        firstCompile = true;
      }
      final CompilerOutput compilerOutput = await compiler.recompile(
163 164
        request.mainUri,
        <Uri>[request.mainUri],
165
        outputPath: outputDill.path,
166
        packageConfig: _packageConfig,
167 168 169 170 171 172 173
      );
      final String outputPath = compilerOutput?.outputFilename;

      // In case compiler didn't produce output or reported compilation
      // errors, pass [null] upwards to the consumer and shutdown the
      // compiler to avoid reusing compiler that might have gotten into
      // a weird state.
174
      final String path = request.mainUri.toFilePath(windows: globals.platform.isWindows);
175 176 177 178
      if (outputPath == null || compilerOutput.errorCount > 0) {
        request.result.complete(null);
        await _shutdown();
      } else {
179
        final File outputFile = globals.fs.file(outputPath);
180
        final File kernelReadyToRun = await outputFile.copy('$path.dill');
181
        final File testCache = globals.fs.file(testFilePath);
182 183 184 185
        if (firstCompile || !testCache.existsSync() || (testCache.lengthSync() < outputFile.lengthSync())) {
          // The idea is to keep the cache file up-to-date and include as
          // much as possible in an effort to re-use as many packages as
          // possible.
186 187 188
          if (!testCache.parent.existsSync()) {
            testCache.parent.createSync(recursive: true);
          }
189 190 191 192 193 194
          await outputFile.copy(testFilePath);
        }
        request.result.complete(kernelReadyToRun.path);
        compiler.accept();
        compiler.reset();
      }
195
      globals.printTrace('Compiling $path took ${compilerTime.elapsedMilliseconds}ms');
196 197 198 199 200
      // Only remove now when we finished processing the element
      compilationQueue.removeAt(0);
    }
  }
}