Skip to content

Commit 071ec5f

Browse files
committed
Prune package graph to actual deps.
1 parent 7cf516e commit 071ec5f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

build_runner/lib/src/build_plan/package_graph.dart

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:yaml/yaml.dart';
1212

1313
import '../constants.dart';
1414
import '../io/asset_path_provider.dart';
15+
import '../logging/build_log.dart';
1516

1617
/// The SDK package, we filter this to the core libs and dev compiler
1718
/// resources.
@@ -93,6 +94,7 @@ class PackageGraph implements AssetPathProvider {
9394
// where a package config was found. It doesn't seem possible to obtain this
9495
// directly with package:package_config.
9596
while (true) {
97+
//buildLog.debug('!!!! $rootDir');
9698
packageConfig = await findPackageConfig(
9799
Directory(rootDir),
98100
recurse: false,
@@ -113,6 +115,10 @@ class PackageGraph implements AssetPathProvider {
113115
'Unable to find package config for package at $packagePath.',
114116
);
115117
}
118+
/*buildLog.debug(
119+
'loading dependency types from $rootDir, vs ''
120+
${packageConfig.packages.map((p) => p.name).toList()..sort()}',
121+
);*/
116122
final dependencyTypes = _parseDependencyTypes(rootDir);
117123

118124
final nodes = <String, PackageNode>{};
@@ -157,14 +163,37 @@ class PackageGraph implements AssetPathProvider {
157163
final packageDependencies = _parsePackageDependencies(
158164
packageConfig.packages.where((p) => p.name != rootPackageName),
159165
);
166+
// print('packageDependencies: $packageDependencies');s
160167
for (final packageName in packageDependencies.keys) {
161168
packageNode(packageName).dependencies.addAll(
162169
packageDependencies[packageName]!.map(
163170
(n) => packageNode(n, parent: packageName),
164171
),
165172
);
166173
}
167-
return PackageGraph._(rootNode, nodes);
174+
175+
final actuallyUsedNodes = {rootNode};
176+
var changed = true;
177+
while (changed) {
178+
changed = false;
179+
for (final node in actuallyUsedNodes.toList()) {
180+
for (final dep in node.dependencies) {
181+
if (!actuallyUsedNodes.contains(dep)) {
182+
actuallyUsedNodes.add(dep);
183+
changed = true;
184+
}
185+
}
186+
}
187+
}
188+
189+
// print('Now we have ${actuallyUsedNodes.map((n) => n.name).toList()}');
190+
191+
return PackageGraph._(
192+
rootNode,
193+
Map.fromEntries(
194+
nodes.entries.where((e) => actuallyUsedNodes.contains(e.value)),
195+
),
196+
);
168197
}
169198

170199
/// Creates a [PackageGraph] for the package in which you are currently

0 commit comments

Comments
 (0)