pathDependencyRoots function

List<String> pathDependencyRoots(
  1. String packageConfigJson, {
  2. String? configDir,
})

Root directories of path (sibling-checkout / workspace) dependencies from a package_config.json. Hosted (.pub-cache) and SDK-bundled (bin/cache/pkg) packages are excluded — their contents are pinned by the config itself.

Implementation

List<String> pathDependencyRoots(String packageConfigJson,
    {String? configDir}) {
  final base = Uri.directory(configDir ?? Directory.current.path);
  final out = <String>[];
  final rootUriRe = RegExp(r'"rootUri":\s*"([^"]+)"');
  for (final m in rootUriRe.allMatches(packageConfigJson)) {
    final raw = m.group(1)!;
    final uri = Uri.parse(raw);
    final String path;
    if (uri.scheme.isEmpty) {
      path = base.resolveUri(uri).toFilePath();
    } else if (uri.scheme == 'file') {
      path = uri.toFilePath();
    } else {
      continue;
    }
    if (path.contains('/.pub-cache/') ||
        path.contains(r'\.pub-cache\') ||
        path.contains('/bin/cache/pkg/')) {
      continue;
    }
    final normalized = p.normalize(path);
    if (!out.contains(normalized)) out.add(normalized);
  }
  return out;
}