Validate that the same package cannot be overridden twice in workspace (#4238)
diff --git a/lib/src/package.dart b/lib/src/package.dart
index 8edf823..1daf3a6 100644
--- a/lib/src/package.dart
+++ b/lib/src/package.dart
@@ -383,8 +383,11 @@
}
}
-/// Reports an error if the graph of the workspace rooted at [root] is not a
-/// tree. Or if a package name occurs twice.
+/// Reports an error if one or more of:
+///
+/// * The graph of the workspace rooted at [root] is not a tree.
+/// * If a package name occurs twice.
+/// * If two packages in the workspace override the same package name.
void validateWorkspace(Package root) {
if (root.workspaceChildren.isEmpty) return;
@@ -420,4 +423,21 @@
}
namesSeen[package.name] = package;
}
+
+ // Check that the workspace doesn't contain two overrides of the same package.
+ final overridesSeen = <String, Package>{};
+ for (final package in root.transitiveWorkspace) {
+ for (final override in package.pubspec.dependencyOverrides.keys) {
+ final collision = overridesSeen[override];
+ if (collision != null) {
+ fail('''
+The package `$override` is overridden in both:
+package `${collision.name}` at `${collision.dir}` and '${package.name}' at `${package.dir}`.
+
+Consider removing one of the overrides.
+''');
+ }
+ overridesSeen[override] = package;
+ }
+ }
}
diff --git a/test/workspace_test.dart b/test/workspace_test.dart
index 0cd47de..df00a0f 100644
--- a/test/workspace_test.dart
+++ b/test/workspace_test.dart
@@ -1224,6 +1224,44 @@
);
});
+ test('Reports error if two members of workspace override the same package',
+ () async {
+ final server = await servePackages();
+ server.serve('foo', '1.0.0');
+ await dir(appPath, [
+ libPubspec(
+ 'myapp',
+ '1.2.3',
+ deps: {'foo': 'any'},
+ extras: {
+ 'dependency_overrides': {
+ 'foo': {'path': '../foo'},
+ },
+ 'workspace': ['a'],
+ },
+ sdk: '^3.5.0',
+ ),
+ dir('a', [
+ libPubspec(
+ 'a',
+ '1.0.0',
+ resolutionWorkspace: true,
+ ),
+ pubspecOverrides({
+ 'dependency_overrides': {'foo': '2.0.0'},
+ }),
+ ]),
+ ]).create();
+ await pubGet(
+ environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
+ error: '''
+The package `foo` is overridden in both:
+package `myapp` at `.` and 'a' at `.${s}a`.
+
+Consider removing one of the overrides.''',
+ );
+ });
+
test('overrides are applied', () async {
final server = await servePackages();
server.serve('foo', '1.0.0');