Reject overrides of workspace packages (#4303)

diff --git a/lib/src/package.dart b/lib/src/package.dart
index 7a2178d..4ccab8f 100644
--- a/lib/src/package.dart
+++ b/lib/src/package.dart
@@ -393,6 +393,7 @@
 /// * 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.
+/// * A workspace package is overridden.
 void validateWorkspace(Package root) {
   if (root.workspaceChildren.isEmpty) return;
 
@@ -430,6 +431,7 @@
   }
 
   // Check that the workspace doesn't contain two overrides of the same package.
+  // Also check that workspace packages are not overridden.
   final overridesSeen = <String, Package>{};
   for (final package in root.transitiveWorkspace) {
     for (final override in package.pubspec.dependencyOverrides.keys) {
@@ -443,6 +445,14 @@
 ''');
       }
       overridesSeen[override] = package;
+
+      if (namesSeen[override] case final Package overriddenWorkspacePackage) {
+        fail('''
+Cannot override workspace packages.
+
+Package `$override` at `${overriddenWorkspacePackage.presentationDir}` is overridden in `${package.pubspecPath}`.
+''');
+      }
     }
   }
 }
diff --git a/test/workspace_test.dart b/test/workspace_test.dart
index 63cd9cd..8cbd0af 100644
--- a/test/workspace_test.dart
+++ b/test/workspace_test.dart
@@ -1378,6 +1378,37 @@
       output: contains('FOO'),
     );
   });
+
+  test('Cannot override workspace packages', () async {
+    await servePackages();
+    await dir(appPath, [
+      libPubspec(
+        'myapp',
+        '1.2.3',
+        extras: {
+          'workspace': ['pkgs/a'],
+          'dependency_overrides': {
+            'a': {'path': 'pkgs/a'},
+          },
+        },
+        sdk: '^3.5.0',
+      ),
+      dir('pkgs', [
+        dir('a', [
+          libPubspec('a', '1.1.1', resolutionWorkspace: true),
+        ]),
+      ]),
+    ]).create();
+    await pubGet(
+      environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
+      error: allOf(
+        contains('Cannot override workspace packages'),
+        contains(
+          'Package `a` at `.${s}pkgs/a` is overridden in `pubspec.yaml`.',
+        ),
+      ),
+    );
+  });
 }
 
 final s = p.separator;