resolutionProblems function
What is wrong with projectPath's .dart_tool/package_config.json, as
read from disk — without running pub. Empty means every package the config
names is present.
It reports a missing or unreadable config, a package directory that no
longer exists (the cache lost it, or a path dependency moved), and a
directory that exists without a pubspec.yaml — the one damage dart pub get does not repair.
Implementation
List<String> resolutionProblems(String projectPath) {
final configFile = File(
p.join(projectPath, '.dart_tool', 'package_config.json'),
);
if (!configFile.existsSync()) {
return ['no .dart_tool/package_config.json — never resolved here'];
}
final Object? decoded;
try {
decoded = jsonDecode(configFile.readAsStringSync());
} on FormatException catch (e) {
return ['.dart_tool/package_config.json is not valid JSON: ${e.message}'];
}
final packages = decoded is Map ? decoded['packages'] : null;
if (packages is! List) {
return ['.dart_tool/package_config.json has no "packages" list'];
}
final configDir = configFile.parent.uri;
final problems = <String>[];
for (final entry in packages) {
if (entry is! Map) continue;
final name = entry['name'];
final rootUri = entry['rootUri'];
if (name is! String || rootUri is! String) continue;
// Relative rootUris resolve against the config file's own directory, per
// the package_config specification; absolute ones are `file:` URIs.
final root = p.fromUri(configDir.resolve(rootUri));
if (!Directory(root).existsSync()) {
problems.add('$name: $root no longer exists');
} else if (!File(p.join(root, 'pubspec.yaml')).existsSync()) {
problems.add(
'$name: $root has no pubspec.yaml — delete that directory, then '
'`dart pub get` (pub treats its existence as installed)',
);
}
}
return problems;
}