resolvedPackagesOf function
Package name → its root directory in the graph pub get resolved for the
package at packageDir, read from .dart_tool/package_config.json. Null
when the package has not been resolved.
This is how a walk follows hosted dependencies: a pubspec names a hosted package by version constraint only, and only the resolution says which copy — in the pub cache — is the one a build links, so it is that copy's pubspec the walk reads.
Implementation
Map<String, String>? resolvedPackagesOf(String packageDir) {
final config = File(p.join(packageDir, '.dart_tool', 'package_config.json'));
if (!config.existsSync()) return null;
final Object? decoded;
try {
decoded = jsonDecode(config.readAsStringSync());
} on FormatException {
return null;
}
if (decoded is! Map || decoded['packages'] is! List) return null;
final base = Uri.file(config.path);
return {
for (final entry in decoded['packages'] as List)
if (entry is Map && entry['name'] is String && entry['rootUri'] is String)
entry['name'] as String: p.normalize(
base.resolve(entry['rootUri'] as String).toFilePath(),
),
};
}