lockfileDependencyIssues static method

List<String> lockfileDependencyIssues(
  1. File lockfile, {
  2. required SupportedDartSdkPolicy? supportedSdkPolicy,
})

Validates the Dart SDK constraint recorded in lockfile.

supportedSdkPolicy is the Dart SDK version policy of Serverpod Cloud, fetched from the server. If it is null the constraint is only checked for valid syntax - the server enforces the policy when the deployment is created.

Returns an empty list if the lockfile is missing, has no Dart SDK constraint, or the constraint is supported. Otherwise returns error messages.

Implementation

static List<String> lockfileDependencyIssues(
  final File lockfile, {
  required final SupportedDartSdkPolicy? supportedSdkPolicy,
}) {
  if (!lockfile.existsSync()) {
    return const [];
  }

  final String rawContent;
  try {
    rawContent = lockfile.readAsStringSync();
  } catch (e) {
    return ['Failed to read pubspec.lock: ${e.toString()}'];
  }

  final YamlNode document;
  try {
    document = loadYamlNode(rawContent);
  } catch (e) {
    return ['Failed to parse pubspec.lock: ${e.toString()}'];
  }

  if (document is! YamlMap) {
    return ['Failed to parse pubspec.lock: expected a YAML map'];
  }

  final sdks = document.value['sdks'];
  if (sdks is! YamlMap) {
    return const [];
  }

  final dartSdk = sdks.value['dart'];
  if (dartSdk == null) {
    return const [];
  }

  final sdkConstraintText = dartSdk.toString().trim();
  if (sdkConstraintText.isEmpty) {
    return const [];
  }

  final VersionConstraint sdkConstraint;
  try {
    sdkConstraint = VersionConstraint.parse(sdkConstraintText);
  } on FormatException {
    return [
      'Invalid Dart SDK version constraint in pubspec.lock: '
          '"$sdkConstraintText".',
    ];
  }

  if (supportedSdkPolicy != null &&
      !supportedSdkPolicy.supportedRange.allowsAny(sdkConstraint)) {
    return [
      'Unsupported sdk version constraint in pubspec.lock: $sdkConstraintText'
          ' (must accept: ${supportedSdkPolicy.supportedRange})\n'
          '${supportedSdkPolicy.availabilityDescription}',
    ];
  }

  return const [];
}