Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add better error message when the package config is out of date #3552

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build_runner_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 7.2.11-dev

- Add better error messages for incomplete package configs when creating a
package graph.

## 7.2.10

- Fix build script invalidation check for custom build scripts from other
Expand Down
31 changes: 23 additions & 8 deletions build_runner_core/lib/src/package_graph/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ class PackageGraph {
'This program must be ran from the root directory of your package.');
}
final rootPubspec = _pubspecForPath(packagePath);
final rootPackageName = rootPubspec['name'] as String;
final rootPackageName = rootPubspec['name'] as String?;
if (rootPackageName == null) {
throw StateError('The current package has no name, please add one to the '
natebosch marked this conversation as resolved.
Show resolved Hide resolved
'pubspec.yaml.');
}

final packageConfig =
await findPackageConfig(Directory(packagePath), recurse: false);
if (packageConfig == null) {
throw StateError(
'Unable to find package config for package at $packagePath');
'Unable to find package config for package at $packagePath.');
}

final dependencyTypes = _parseDependencyTypes(packagePath);
Expand All @@ -98,16 +102,27 @@ class PackageGraph {
package.languageVersion,
isRoot: isRoot);
}
final rootNode = nodes[rootPackageName]!;
rootNode.dependencies
.addAll(_depsFromYaml(rootPubspec, isRoot: true).map((n) => nodes[n]!));
PackageNode packageNode(String package, {String? parent}) {
final node = nodes[package];
if (node == null) {
throw StateError(
'Dependency $package ${parent != null ? 'of $parent ' : ''}not '
'present, please run `dart pub get` or `flutter pub get` to fetch '
'dependencies.');
}
return node;
}

final rootNode = packageNode(rootPackageName);
rootNode.dependencies.addAll(_depsFromYaml(rootPubspec, isRoot: true)
.map((n) => packageNode(n, parent: rootPackageName)));

final packageDependencies = _parsePackageDependencies(
packageConfig.packages.where((p) => p.name != rootPackageName));
for (final packageName in packageDependencies.keys) {
nodes[packageName]!
.dependencies
.addAll(packageDependencies[packageName]!.map((n) => nodes[n]!));
packageNode(packageName).dependencies.addAll(
packageDependencies[packageName]!
.map((n) => packageNode(n, parent: packageName)));
}
return PackageGraph._(rootNode, nodes);
}
Expand Down