AnalysisContextHelper constructor

AnalysisContextHelper({
  1. required List<String> includedPaths,
  2. String? sdkPath,
})

Creates an AnalysisContextHelper for the specified includedPaths.

If sdkPath is omitted, it will be discovered automatically via findSdkPath. Throws SdkDiscoveryException if no valid SDK is found.

Implementation

factory AnalysisContextHelper({
  required List<String> includedPaths,
  String? sdkPath,
}) {
  if (includedPaths.isEmpty) {
    throw ArgumentError.value(
      includedPaths,
      'includedPaths',
      'Must contain at least one path.',
    );
  }

  final effectiveSdkPath = sdkPath ?? findSdkPath();
  if (effectiveSdkPath == null) {
    throw const SdkDiscoveryException(
      'Cannot locate a Dart SDK for analysis. Pass --sdk-path, set the '
      'DART_SDK environment variable, or ensure a Dart SDK is on PATH.',
    );
  }

  if (!isValidSdk(effectiveSdkPath)) {
    throw SdkDiscoveryException(
      'The provided SDK path "$effectiveSdkPath" does not point to a valid '
      'Dart SDK root (missing lib/_internal).',
    );
  }

  final normalizedIncluded = includedPaths
      .map((path) => p.normalize(p.absolute(path)))
      .toList();

  final collection = AnalysisContextCollection(
    includedPaths: normalizedIncluded,
    sdkPath: effectiveSdkPath,
  );

  return AnalysisContextHelper._(
    collection: collection,
    sdkPath: effectiveSdkPath,
    includedPaths: normalizedIncluded,
  );
}