updateFileContexts method

Future<bool> updateFileContexts(
  1. Set<String> filePaths
)

Inform the analyzer that the provided filePaths have been updated.

Refreshes the Dart analysis context for the changed files and returns true if the analysis recognizes any of them as (or as having been) endpoint files, or if the change altered what an endpoint in another file declares, meaning code generation should run. The actual full analysis is deferred to the next analyze call.

Implementation

Future<bool> updateFileContexts(Set<String> filePaths) async {
  // Only consider files within the tracked directory.
  final relevantPaths = filePaths
      .where((f) => p.isWithin(absoluteIncludedPaths, p.absolute(f)))
      .toSet();

  final erroredFilesBefore = _erroredFiles;
  final keysBefore = _fileCache.keys.toSet();
  // Read before analyzing, which records the hashes itself when there are
  // none yet.
  final previousHashes = _definitionHashesAtLastUpdate;

  await analyze(
    collector: CodeGenerationCollector(),
    changedFiles: relevantPaths,
  );

  final erroredFilesAfter = _erroredFiles;
  final keysAfter = _fileCache.keys.toSet();

  // What a endpoint declares can change without its file changing, through
  // an alias, a base class or a type in a file it imports. Every cached file
  // was just analyzed again and hashed its definitions while it was parsed,
  // so the hashes show it. They are compared with the ones of the previous
  // update rather than with what generation left in the cache,
  // which validates against models and can differ from
  // this without anything having changed.
  final definitionHashes = _definitionHashes;
  _definitionHashesAtLastUpdate = definitionHashes;
  if (previousHashes != null &&
      definitionHashes.entries.any(
        (entry) => previousHashes[entry.key] != entry.value,
      )) {
    return true;
  }

  // Regenerate when the set of endpoint files changed, or when any file's
  // error state flipped (an error appearing or clearing changes the parsed
  // definitions). A persistently broken file, by contrast, must not turn
  // every unrelated change - such as watcher echoes of freshly generated
  // files - into another generation, or generation loops forever while an
  // error exists anywhere in the project.
  if (keysBefore.length != keysAfter.length ||
      keysAfter.difference(keysBefore).isNotEmpty ||
      erroredFilesBefore.length != erroredFilesAfter.length ||
      erroredFilesAfter.difference(erroredFilesBefore).isNotEmpty) {
    return true;
  }

  // Editing methods on an existing endpoint does not add/remove cache keys,
  // but generated protocol must still be refreshed (otherwise stale
  // generated Dart can break analysis/compile before the next generate).
  // The cache only holds files the analysis recognized as endpoint files.
  // Canonicalized once into a set rather than compared pairwise, so this
  // stays linear in the number of changed files as the project's endpoint
  // count grows.
  final cachedEndpointPaths = {
    for (final key in keysAfter) p.canonicalize(key),
  };
  return relevantPaths.any(
    (path) => cachedEndpointPaths.contains(p.canonicalize(path)),
  );
}