<commit_before>import 'dart:async' show StreamController;
import 'dart:convert' show utf8, LineSplitter;
import 'dart:io' show Directory, File, FileSystemEntity, IOSink, exitCode;

import 'package:kernel/ast.dart' show Component;
import 'package:kernel/kernel.dart' show loadComponentFromBytes;
import 'package:kernel/verifier.dart' show verifyComponent;

import '../lib/frontend_server.dart';

main(List<String> args) async {
  String flutterDir;
  String flutterPlatformDir;
  for (String arg in args) {
    if (arg.startsWith("--flutterDir=")) {
      flutterDir = arg.substring(13);
    } else if (arg.startsWith("--flutterPlatformDir=")) {
      flutterPlatformDir = arg.substring(21);
    }
  }

  await compileTests(flutterDir, flutterPlatformDir, new StdoutLogger());
}

Future compileTests(String flutterDir, String flutterPlatformDir, Logger logger,
    {String filter}) async {
  if (flutterDir == null || !(new Directory(flutterDir).existsSync())) {
    throw "Didn't get a valid flutter directory to work with.";
  }
  Directory flutterDirectory = new Directory(flutterDir);
  // Ensure the path ends in a slash.
  flutterDirectory = new Directory.fromUri(flutterDirectory.uri);

  List<FileSystemEntity> allFlutterFiles =
      flutterDirectory.listSync(recursive: true, followLinks: false);
  Directory flutterPlatformDirectory;

  if (flutterPlatformDir == null) {
    List<File> platformFiles = new List<File>.from(allFlutterFiles.where((f) =>
        f.uri
            .toString()
            .endsWith("/flutter_patched_sdk/platform_strong.dill")));
    if (platformFiles.length < 1) {
      throw "Expected to find a flutter platform file but didn't.";
    }
    flutterPlatformDirectory = platformFiles.first.parent;
  } else {
    flutterPlatformDirectory = Directory(flutterPlatformDir);
  }
  if (!flutterPlatformDirectory.existsSync()) {
    throw "$flutterPlatformDirectory doesn't exist.";
  }
  // Ensure the path ends in a slash.
  flutterPlatformDirectory =
      new Directory.fromUri(flutterPlatformDirectory.uri);

  if (!new File.fromUri(
          flutterPlatformDirectory.uri.resolve("platform_strong.dill"))
      .existsSync()) {
    throw "$flutterPlatformDirectory doesn't contain a "
        "platform_strong.dill file.";
  }
  logger.notice("Using $flutterPlatformDirectory as platform directory.");
  List<File> dotPackagesFiles = new List<File>.from(allFlutterFiles.where((f) =>
      (f.uri.toString().contains("/examples/") ||
          f.uri.toString().contains("/packages/")) &&
      f.uri.toString().endsWith("/.packages")));

  List<String> allCompilationErrors = [];
  for (File dotPackage in dotPackagesFiles) {
    Directory tempDir;
    Directory systemTempDir = Directory.systemTemp;
    tempDir = systemTempDir.createTempSync('flutter_frontend_test');
    try {
      Directory testDir =
          new Directory.fromUri(dotPackage.parent.uri.resolve("test/"));
      if (!testDir.existsSync()) continue;
      if (testDir.toString().contains("packages/flutter_web_plugins/test/")) {
        // TODO(jensj): Figure out which tests are web-tests, and compile those
        // in a setup that can handle that.
        continue;
      }
      logger.notice("Go for $testDir");
      List<String> compilationErrors = await attemptStuff(
          tempDir,
          flutterPlatformDirectory,
          dotPackage,
          testDir,
          flutterDirectory,
          logger,
          filter);
      if (compilationErrors.isNotEmpty) {
        logger.notice("Notice that we had ${compilationErrors.length} "
            "compilation errors for $testDir");
        allCompilationErrors.addAll(compilationErrors);
      }
    } finally {
      tempDir.delete(recursive: true);
    }
  }
  if (allCompilationErrors.isNotEmpty) {
    logger.notice(
        "Had a total of ${allCompilationErrors.length} compilation errors:");
    allCompilationErrors.forEach(logger.notice);
    exitCode = 1;
  }
}

Future<List<String>> attemptStuff(
    Directory tempDir,
    Directory flutterPlatformDirectory,
    File dotPackage,
    Directory testDir,
    Directory flutterDirectory,
    Logger logger,
    String filter) async {
  List<File> testFiles =
      new List<File>.from(testDir.listSync(recursive: true).where((f) {
    if (!f.path.endsWith("_test.dart")) return false;
    if (filter != null) {
      String testName = f.path.substring(flutterDirectory.path.length);
      if (!testName.startsWith(filter)) return false;
    }
    return true;
  }));
  if (testFiles.isEmpty) return [];
<commit_msg>[flutter-frontend] Fix potential flutter-frontend bot failures caused by mismatch in NnbdMode<commit_after>import 'dart:async' show StreamController;
import 'dart:convert' show utf8, LineSplitter;
import 'dart:io' show Directory, File, FileSystemEntity, IOSink, exitCode;

import 'package:kernel/ast.dart' show Component;
import 'package:kernel/kernel.dart' show loadComponentFromBytes;
import 'package:kernel/verifier.dart' show verifyComponent;

import 'package:front_end/src/api_prototype/language_version.dart'
    show uriUsesLegacyLanguageVersion;

import 'package:front_end/src/api_unstable/vm.dart'
    show CompilerOptions, NnbdMode, StandardFileSystem;

import '../lib/frontend_server.dart';

main(List<String> args) async {
  String flutterDir;
  String flutterPlatformDir;
  for (String arg in args) {
    if (arg.startsWith("--flutterDir=")) {
      flutterDir = arg.substring(13);
    } else if (arg.startsWith("--flutterPlatformDir=")) {
      flutterPlatformDir = arg.substring(21);
    }
  }

  await compileTests(flutterDir, flutterPlatformDir, new StdoutLogger());
}

Future<NnbdMode> _getNNBDMode(Uri script, Uri packagesFileUri) async {
  final CompilerOptions compilerOptions = new CompilerOptions()
    ..sdkRoot = null
    ..fileSystem = StandardFileSystem.instance
    ..packagesFileUri = packagesFileUri
    ..sdkSummary = null
    ..nnbdMode = NnbdMode.Weak;

  if (await uriUsesLegacyLanguageVersion(script, compilerOptions)) {
    return NnbdMode.Weak;
  }
  return NnbdMode.Strong;
}

Future compileTests(String flutterDir, String flutterPlatformDir, Logger logger,
    {String filter}) async {
  if (flutterDir == null || !(new Directory(flutterDir).existsSync())) {
    throw "Didn't get a valid flutter directory to work with.";
  }
  Directory flutterDirectory = new Directory(flutterDir);
  // Ensure the path ends in a slash.
  flutterDirectory = new Directory.fromUri(flutterDirectory.uri);

  List<FileSystemEntity> allFlutterFiles =
      flutterDirectory.listSync(recursive: true, followLinks: false);
  Directory flutterPlatformDirectory;

  if (flutterPlatformDir == null) {
    List<File> platformFiles = new List<File>.from(allFlutterFiles.where((f) =>
        f.uri
            .toString()
            .endsWith("/flutter_patched_sdk/platform_strong.dill")));
    if (platformFiles.length < 1) {
      throw "Expected to find a flutter platform file but didn't.";
    }
    flutterPlatformDirectory = platformFiles.first.parent;
  } else {
    flutterPlatformDirectory = Directory(flutterPlatformDir);
  }
  if (!flutterPlatformDirectory.existsSync()) {
    throw "$flutterPlatformDirectory doesn't exist.";
  }
  // Ensure the path ends in a slash.
  flutterPlatformDirectory =
      new Directory.fromUri(flutterPlatformDirectory.uri);

  if (!new File.fromUri(
          flutterPlatformDirectory.uri.resolve("platform_strong.dill"))
      .existsSync()) {
    throw "$flutterPlatformDirectory doesn't contain a "
        "platform_strong.dill file.";
  }
  logger.notice("Using $flutterPlatformDirectory as platform directory.");
  List<File> dotPackagesFiles = new List<File>.from(allFlutterFiles.where((f) =>
      (f.uri.toString().contains("/examples/") ||
          f.uri.toString().contains("/packages/")) &&
      f.uri.toString().endsWith("/.packages")));

  List<String> allCompilationErrors = [];
  for (File dotPackage in dotPackagesFiles) {
    Directory systemTempDir = Directory.systemTemp;
    Directory tempDir;
    Directory testDir =
        new Directory.fromUri(dotPackage.parent.uri.resolve("test/"));
    if (!testDir.existsSync()) continue;
    if (testDir.toString().contains("packages/flutter_web_plugins/test/")) {
      // TODO(jensj): Figure out which tests are web-tests, and compile those
      // in a setup that can handle that.
      continue;
    }
    logger.notice("Go for $testDir");
    List<File> testFiles =
        new List<File>.from(testDir.listSync(recursive: true).where((f) {
      if (!f.path.endsWith("_test.dart")) return false;
      if (filter != null) {
        String testName = f.path.substring(flutterDirectory.path.length);
        if (!testName.startsWith(filter)) return false;
      }
      return true;
    }));

    // Split into NNBD Strong and Weak so only the ones that match are
    // compiled togeher. If mixing-and-matching the first file (which could
    // be either) will setup the compiler which can lead to compilation errors
    // for another file, for instance if the first one is strong but a
    // subsequent one tries to opt out (i.e. is weak) an error is issued that
    // that's not possible.
    List<File> weak = [];
    List<File> strong = [];
    for (File file in testFiles) {
      if (await _getNNBDMode(file.uri, dotPackage.uri) == NnbdMode.Weak) {
        weak.add(file);
      } else {
        strong.add(file);
      }
    }
    for (List<File> files in [weak, strong]) {
      if (files.isEmpty) continue;
      tempDir = systemTempDir.createTempSync('flutter_frontend_test');
      try {
        List<String> compilationErrors = await attemptStuff(
            files,
            tempDir,
            flutterPlatformDirectory,
            dotPackage,
            testDir,
            flutterDirectory,
            logger,
            filter);
        if (compilationErrors.isNotEmpty) {
          logger.notice("Notice that we had ${compilationErrors.length} "
              "compilation errors for $testDir");
          allCompilationErrors.addAll(compilationErrors);
        }
      } finally {
        tempDir.delete(recursive: true);
      }
    }
  }
  if (allCompilationErrors.isNotEmpty) {
    logger.notice(
        "Had a total of ${allCompilationErrors.length} compilation errors:");
    allCompilationErrors.forEach(logger.notice);
    exitCode = 1;
  }
}

Future<List<String>> attemptStuff(
    List<File> testFiles,
    Directory tempDir,
    Directory flutterPlatformDirectory,
    File dotPackage,
    Directory testDir,
    Directory flutterDirectory,
    Logger logger,
    String filter) async {
  if (testFiles.isEmpty) return [];

{'fraction_non_alphanumeric': 0.11326070623591285, 'fraction_numerical': 0.001314800901577761, 'mean_word_length': 3.4095238095238094, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7054769', 'n_tokens_mistral': 3154, 'n_tokens_neox': 3083, 'n_words': 736}